1.引入配置:zzzz.jdbc.url=jdbc:mysql://192.168.101.31:3306/zzzz?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
zzzz.jdbc.username=xxx
zzzz.jdbc.password=xxx
zzzz.jdbc.driverclass=com.mysql.jdbc.Driver
Pom.xml依赖:
<!-- mzzatis -->
<dependenzz>
<groupId>com.zz.zzwb</groupId>
<artifactId>data-service-core</artifactId>
<version>${data-service-core-version}</version>
<exclusions>
<exclusion>
<groupId>com.zz.redis</groupId>
<artifactId>zz-redis-core</artifactId>
</exclusion>
</exclusions>
</dependenzz>
<dependenzz>
<groupId>org.mzzatis</groupId>
<artifactId>mzzatis-spring</artifactId>
<version>1.3.0</version>
</dependenzz>
2.初始化mzzatis配置bean:
package com.zz.zz.bean;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.alibaba.druid.pool.DruidDataSource;
import org.mzzatis.spring.SqlSessionFactorzzean;
import org.mzzatis.spring.annotation.MapperScan;
@Configuration
@EnableTransactionManagement
@MapperScan("com.zz.data.service.core.dal.mapper")
public class MzzatisBean {
@Value("${zzzz.jdbc.driverclass}")
private String driverClass;
@Value("${zzzz.jdbc.url}")
private String zzzzJdbcUrl;
@Value("${zzzz.jdbc.username}")
private String zzzzJdbcUsername;
@Value("${zzzz.jdbc.password}")
private String zzzzJdbcPassword;
/**
* MAPPER 文件路径
*/
private static final String MAPPER_LOCATIONS = "classpath*:com/zz/data/service/mapper/**/*Mapper.xml";
@Bean
public SqlSessionFactory initzzSessionFactory() throws Exception{
SqlSessionFactorzzean sqlSessionFactorzzean = new SqlSessionFactorzzean();
sqlSessionFactorzzean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactorzzean.setMapperLocations(resolver.getResources(MAPPER_LOCATIONS));
return sqlSessionFactorzzean.getObject();
}
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(zzzzJdbcUrl);
dataSource.setDriverClassName(driverClass);
dataSource.setUsername(zzzzJdbcUsername);
dataSource.setPassword(zzzzJdbcPassword);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
3.应用:
@Autowired
private InfozzMapper infozzMapper;
@Override
public int selectCount() {
InfozzQuery query = new InfozzQuery();
query.setPageSize(1);
query.setPageNo(1);
return infozzMapper.countByQuery(query);
}
@Override
public List<InfozzDO> selectInfozzByPage(int page, int size) {
InfozzQuery query = new InfozzQuery();
query.setPageNo(page);
query.setPageSize(size);
return infozzMapper.selectByQuery(query);
}