SpringBoot项目实战--mybatis

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的集成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值