spring-boot+mybatis接入:
- 引包
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- db -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.20</version>
</dependency>
- 配置dataSource
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = { "com.zz.mapper" }, sqlSessionFactoryRef = "zzSqlSessionFactory")
public class MtcDataSourceConfig {
/**
* Mybatis MAPPER 文件路径
*/
private static final String[] MAPPER_LOCATIONS = { "classpath*:com/xx/xml/zz/*Mapper.xml" };
@Autowired
@Qualifier("zzDataSource")
DataSource dataSource;
@Bean(name = "zzDataSource")
@ConfigurationProperties(prefix = "zz.jdbc")
public DataSource dataSource() {
return new DruidDataSource();
}
@Bean(name = "zzSqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("zzDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations(MAPPER_LOCATIONS));
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "zzTransactionManager")
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
private Resource[] resolveMapperLocations(String[] mapperLocations) {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<Resource> resources = Lists.newArrayList();
if (mapperLocations != null && mapperLocations.length > 0) {
for (String mapperLocation : mapperLocations) {
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resources.toArray(new Resource[resources.size()]);
}
}
配置连接信息:
mtc.jdbc.driverClassName=com.mysql.jdbc.Driver
mtc.jdbc.url=jdbc:mysql://vv:3306/zz?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL\=false
mtc.jdbc.username=root
mtc.jdbc.password=xx
mtc.jdbc.initialSize=3
mtc.jdbc.maxActive=20
mtc.jdbc.minIdle=3
mtc.jdbc.maxWait=3000
mtc.jdbc.validationQuery=SELECT 1
mtc.jdbc.testWhileIdle=true
mtc.jdbc.filters=config,stat
mtc.jdbc.connectionProperties=config.decrypt=true;druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
fdyb.jdbc.driverClassName=com.mysql.jdbc.Driver
fdyb.jdbc.url=jdbc:mysql://xx:3306/zz?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL\=false
fdyb.jdbc.username=root
fdyb.jdbc.password=xx
fdyb.jdbc.initialSize=3
fdyb.jdbc.maxActive=20
fdyb.jdbc.minIdle=3
fdyb.jdbc.maxWait=3000
fdyb.jdbc.validationQuery=SELECT 1
fdyb.jdbc.testWhileIdle=true
fdyb.jdbc.filters=config,stat
fdyb.jdbc.connectionProperties=config.decrypt=true;druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
- 在resources下构造mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xx.mapper.mtc.yyMapper">
<resultMap id="BaseResultMap" type="com.model.mapper.yyDO">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="idcard_no" jdbcType="VARCHAR" property="idcardNo" />
</resultMap>
<sql id="Base_Column_List">
id,idcard_no
</sql>
<select id="selectByMaxId" resultType="java.lang.Long">
select max(id) as id from table where status = 1 and is_delete = 0 and id is not null
</select>
<select id="selectByPage" resultMap="BaseResultMap">
select <include refid="Base_Column_List" /> from table where
status = 1 and is_delete = 0 and key is not null and id > #{id} order by id asc limit #{pageSize}
</select>
</mapper>
配置mapper.xml中需要取出字段的DO,需要哪几个,就构造哪几个
public class yyDO {
private Long id;
private String idcardNo;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getIdcardNo() {
return idcardNo;
}
public void setIdcardNo(String idcardNo) {
this.idcardNo = idcardNo;
}
}
- 在dal下新建mapper接口对应mapper.xml中的方法
public interface zzMapper {
public Long selectByMaxId();
public List<zzDO> selectByPage(@Param("id") Long id, @Param("pageSize") Integer pageSize);
}
- 业务端调用
@Autowired
private zzMapper zzMapper;
注意事项:
- dataSource中的
@MapperScan(basePackages = { "com.zz.dal.mapper.zz" }
这个路径等于mapper接口的包名;
- dataSource中的dataSource引入是根据配置文件中的前缀匹配
@Bean(name = "zzDataSource")
@ConfigurationProperties(prefix = "zz.jdbc")
@Primary
3.多个数据源时,配置多组dataSource,mapper.xml,mapper接口,DO实例,properties数据源配置;