您的位置:首页 > 编程语言 > Java开发

SpringBoot项目实战--mybatis

2017-01-06 17:33 661 查看
1、在pom.xml文件中添加以下依赖:

<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的集成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SpringBoot Mybatis