- 引入依赖
<!--mybatis-spring与mybatis使用低版本适配-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
- mybatis注解配置
package com.lv.qggz.man.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
/**
* @Author: sh
* @Description: MybatisConfig
* @Version:
* @Date: 2023/3/30
*/
@Configuration
@MapperScan("com.xxx.dao")
@EnableTransactionManagement
public class MybatisConfig {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
public static final String MYBATIS_DB = "mybatis-db";
public static final String LOG_4_J = "LOG4J";
public static final String TYPE_ALIASES_PACKAGE = "com.xxx.domain";
public static final String LOCATION_PATTERN = "classpath*:mybatis/*.xml";
public DataSource mybatisDataSource(@Qualifier("dataSource") DruidDataSource dataSource) {
//@Bean(name = "mybatisDataSource")
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(dataSource.getDriverClassName());
druidDataSource.setUrl(dataSource.getUrl());
druidDataSource.setUsername(dataSource.getUsername());
druidDataSource.setPassword(dataSource.getPassword());
druidDataSource.setName(MYBATIS_DB);
return druidDataSource;
}
@Bean(name = "mybatisDataSourceTransactionManager")
public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
return dataSourceTransactionManager;
}
@Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);
sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor()});
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
Resource[] resources = resolver.getResources(LOCATION_PATTERN);
sqlSessionFactoryBean.setMapperLocations(resources);
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
@Bean
public org.apache.ibatis.session.Configuration configuration(SqlSessionFactory sqlSessionFactory) {
org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();
configuration.setLogPrefix(LOG_4_J);
configuration.setLogImpl(Slf4jImpl.class);
return configuration;
}
public PageInterceptor pageInterceptor() {
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("reasonable", "true");
pageInterceptor.setProperties(properties);
return pageInterceptor;
}
}
- 使用