介绍
本篇文章为大家展示了使用弹簧引导如何实现对MyBatis的整合,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
<强> 1。加入mybatis-spring-boot-stater的Maven依赖强>
& lt; dependency> & lt; groupId> org.mybatis.spring.boot & lt; artifactId> mybatis-spring-boot-starter & lt; version> 1.1.1 & lt;/dependency>
<强> 2。配置数据源强>
在src/main/资源中,应用程序。属性配置文件中,这里面添加了一些数据库连接的信息
spring.datasource.url=jdbc: mysql://localhost: 3306/测试 spring.datasource.username=根 spring.datasource.password=根 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
<强> 3。代码注入数据源强>
com . example,包 进口javax.sql.DataSource; 进口org.slf4j.Logger; 进口org.slf4j.LoggerFactory; 进口org.springframework.beans.factory.annotation.Autowired; 进口org.springframework.boot.SpringApplication; 进口org.springframework.boot.autoconfigure.SpringBootApplication; 进口org.springframework.boot.web.servlet.FilterRegistrationBean; 进口org.springframework.boot.web.servlet.ServletComponentScan; 进口org.springframework.boot.web.servlet.ServletListenerRegistrationBean; 进口org.springframework.boot.web.servlet.ServletRegistrationBean; 进口org.springframework.context.annotation.Bean; 进口org.springframework.core.env.Environment; 进口org.springframework.web.servlet.DispatcherServlet; 进口com.alibaba.druid.pool.DruidDataSource; 进口com.example.Listener.IndexListener; 进口com.example.filter.IndexFilter; 进口com.example.servlet.MyServlet; @SpringBootApplication 公开课SpringBootSimpleApplication { @ autowired 私人环境env; @ bean 公共数据源的数据源(){ DruidDataSource数据源=new DruidDataSource (); dataSource.setUrl (env.getProperty (“spring.datasource.url")); dataSource.setUsername (env.getProperty (“spring.datasource.username"));//用户名 dataSource.setPassword (env.getProperty (“spring.datasource.password"));//密码 dataSource.setDriverClassName (env.getProperty (“spring.datasource.driver-class-name")); dataSource.setInitialSize (2); dataSource.setMaxActive (20); dataSource.setMinIdle (0); dataSource.setMaxWait (60000); dataSource.setValidationQuery(“选择1“); dataSource.setTestOnBorrow(假); dataSource.setTestWhileIdle(真正的); dataSource.setPoolPreparedStatements(假); 返回数据源; } 公共静态void main (String [] args) { SpringApplication.run (SpringBootSimpleApplication.class, args); } }
<>强4。增加MyBatis的配置强>
MyBatisConfig。java类:
包com.example.mybatis; 进口javax.sql.DataSource; 进口org.apache.ibatis.session.SqlSessionFactory; 进口org.mybatis.spring.SqlSessionFactoryBean; 进口org.mybatis.spring.SqlSessionTemplate; 进口org.springframework.beans.factory.annotation.Autowired; 进口org.springframework.context.annotation.Bean; 进口org.springframework.context.annotation.Configuration; 进口org.springframework.jdbc.datasource.DataSourceTransactionManager; 进口org.springframework.transaction.PlatformTransactionManager; 进口org.springframework.transaction.annotation.EnableTransactionManagement; 进口org.springframework.transaction.annotation.TransactionManagementConfigurer; @ configuration//加上这个注解,使得支持事务 @EnableTransactionManagement 公共类MyBatisConfig实现TransactionManagementConfigurer { @ autowired 私人数据源的数据源; @Override 公共PlatformTransactionManager annotationDrivenTransactionManager () { 返回新DataSourceTransactionManager(数据源); } @ bean (name=皊qlSessionFactory") 公共SqlSessionFactory sqlSessionFactoryBean () { SqlSessionFactoryBean bean=new SqlSessionFactoryBean (); bean.setDataSource(数据源); 尝试{ 返回bean.getObject (); }捕捉(异常e) { e.printStackTrace (); 把新的RuntimeException (e); } } @ bean 公共SqlSessionTemplate SqlSessionTemplate (SqlSessionFactory SqlSessionFactory) { 返回新SqlSessionTemplate (sqlSessionFactory); } }
MyBatisMapperScannerConfig。java类:
包com.example.mybatis; 进口org.mybatis.spring.mapper.MapperScannerConfigurer; 进口org.springframework.boot.autoconfigure.AutoConfigureAfter; 进口org.springframework.context.annotation.Bean; 进口org.springframework.context.annotation.Configuration;/* * *扫描mybatis的接口 */@ configuration//因为这个对象的扫描,需要在MyBatisConfig的后面注入,所以加上下面的注解 @AutoConfigureAfter (MyBatisConfig.class) 公开课MyBatisMapperScannerConfig { @ bean 公共MapperScannerConfigurer MapperScannerConfigurer () { MapperScannerConfigurer MapperScannerConfigurer=new MapperScannerConfigurer ();//获取之前注入的beanName为sqlSessionFactory的对象 mapperScannerConfigurer.setSqlSessionFactoryBeanName (“sqlSessionFactory");//指定xml配置文件的路径 mapperScannerConfigurer.setBasePackage (“com.example.mybatis.mapper"); 返回mapperScannerConfigurer; } }使用弹簧引导如何实现对MyBatis的整合