SpringBoot项目实战--mybatis
2017-01-06 17:33
661 查看
1、在pom.xml文件中添加以下依赖:
2、在application.properties文件中添加数据数据源及mybatis配置信息:
详细的数据源配置信息参考配置文件类:
@ConfigurationProperties(
prefix = "spring.datasource"
)
public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAware, InitializingBean {
private ClassLoader classLoader;
private Environment environment;
private String name = "testdb";
private Class<? extends DataSource> type;
private String driverClassName;
private String url;
private String username;
private String password;
private String jndiName;
private boolean initialize = true;
private String platform = "all";
private String schema;
private String schemaUsername;
private String schemaPassword;
private String data;
private String dataUsername;
private String dataPassword;
private boolean continueOnError = false;
private String separator = ";";
private Charset sqlScriptEncoding;
private EmbeddedDatabaseConnection embeddedDatabaseConnection;
private DataSourceProperties.Xa xa;
}mybatis配置类:
由于系统中会初始化很多配置信息,所有把所有需要初始化得操作都放如一个初始类中,如下:
在Application.java入口类中添加配置信息的初始化,如下:
至此就完成了mybatis的集成。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
2、在application.properties文件中添加数据数据源及mybatis配置信息:
spring.datasource.driver-class-name= spring.datasource.url= spring.datasource.username= spring.datasource.password= spring.datasource.validationQuery= spring.datasource.testOnBorrow mybatis.mapperLocations=classpath:com/boot/common/dao/*.xml mybatis.typeAliasesPackage=com.boot.common.model在application.properties文件中添加以上配置,服务在启动时会根据配置信息自动初始化数据源及mybatis配置;这些信息还可以从第三方配置文件中读取,只需在项目启动时读取配置文件并且把内容放到作用域中就可以,服务会自动匹配。本人在实际项目中是从zookeeper中读取的配置信息,读取完成后放入到操作系统变量中,代码如下:
import java.io.ByteArrayInputStream; import java.util.Properties; /** * 数据库连接配置 */ public class DataSourceInit { public static void init() { Properties props = new Properties(); try { byte[] data = ZooKeeperInit.getDataByteArray("/url/jdbc.properties"); if (data == null) { throw new Exception("jdbc.properties is not found in zk."); } else { ByteArrayInputStream bis = new ByteArrayInputStream(data); props.load(bis); bis.close(); System.setProperty("spring.datasource.driver-class-name", props.getProperty("jdbc.operation.driver")); System.setProperty("spring.datasource.url", props.getProperty("jdbc.operation.url")); System.setProperty("spring.datasource.username", props.getProperty("jdbc.operation.username")); System.setProperty("spring.datasource.password", props.getProperty("jdbc.operation.password")); System.setProperty("spring.datasource.validationQuery", "select 1"); System.setProperty("spring.datasource.testOnBorrow", "true"); } } catch (Exception e) { e.printStackTrace(); } } }
详细的数据源配置信息参考配置文件类:
@ConfigurationProperties(
prefix = "spring.datasource"
)
public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAware, InitializingBean {
private ClassLoader classLoader;
private Environment environment;
private String name = "testdb";
private Class<? extends DataSource> type;
private String driverClassName;
private String url;
private String username;
private String password;
private String jndiName;
private boolean initialize = true;
private String platform = "all";
private String schema;
private String schemaUsername;
private String schemaPassword;
private String data;
private String dataUsername;
private String dataPassword;
private boolean continueOnError = false;
private String separator = ";";
private Charset sqlScriptEncoding;
private EmbeddedDatabaseConnection embeddedDatabaseConnection;
private DataSourceProperties.Xa xa;
}mybatis配置类:
@ConfigurationProperties( prefix = "mybatis" ) public class MybatisProperties { public static final String MYBATIS_PREFIX = "mybatis"; private String config; private Resource[] mapperLocations; private String typeAliasesPackage; private String typeHandlersPackage; private boolean checkConfigLocation = false; private ExecutorType executorType; }
由于系统中会初始化很多配置信息,所有把所有需要初始化得操作都放如一个初始类中,如下:
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; public class OperationConfigInit implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext applicationContext) { try{ ZooKeeperInit.init(); DataSourceInit.init(); AmqpInit.init(); }catch (Exception e){ e.printStackTrace(); } } }
在Application.java入口类中添加配置信息的初始化,如下:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.addInitializers(new OperationConfigInit()); app.run(args); } }在完成以上操作后,服务在启动时,会自动从zookeeper中读取配置信息,然后初始化数据源,非常方便及简洁。
至此就完成了mybatis的集成。
相关文章推荐
- CK1956-2017年最新项目实战Spring Boot视频微服务整合Mybatis
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
- 【备忘】2017年最新 项目实战 Spring Boot视频教程 微服务整合Mybatis
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(一)
- CK1956-2017年最新项目实战Spring Boot视频微服务整合Mybatis
- SpringBoot+Maven项目实战(4):集成Mybatis
- 05(maven+SSH)网上商城项目实战之spring mybatis整合
- SpringBoot+Maven项目实战(5):集成分页插件
- SpringBoot+Dubbo+Mybatis 项目升级版本(Gradle)
- 基于SpringBoot + Mybatis实现SpringMVC Web项目【原创】
- SpringMVC+Mybatis+Mysql实战项目学习--环境搭建【转】
- SpringMVC+Mybatis+Mysql实战项目学习--环境搭建【转】
- 实战企业项目--springMVC+Mybatis
- maven+spring boot+mybatis+thymeleaf实战
- SpringBoot+Maven项目实战(1):新建Maven项目
- SpringBoot+Maven项目实战(3):整合Freemark模板
- 05 网上商城项目实战之spring mybatis整合
- 网上商城项目实战之spring mybatis整合
- Spring Boot中Mybatis实战经验
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架