五、Spring整合Mybatis
1.添加依赖
-
添加依赖
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.17.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><!-- <scope>test</scope>--></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.29</version></dependency> <!-- spring 整合 mybatis 的包--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency> <!-- mybatis 包--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency> <!-- spring 操作 jdbc 包--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.29.RELEASE</version></dependency></dependencies>
2.配置类
2.1配置Spring配置类
-
配置类
@Configuration @ComponentScan("cn.sycoder") public class SpringConfig { }
2.2配置JDBC配置类
-
配置类
public class JdbcConfig {@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Value("${jdbc.driverClassName}")private String driverClassName;@Value("${jdbc.url}")private String url;//配置连接池@Beanpublic DataSource dataSource(){DruidDataSource source = new DruidDataSource();source.setUsername(username);source.setPassword(password);source.setDriverClassName(driverClassName);source.setUrl(url);return source;} }
2.3添加数据库配置文件
-
配置文件
jdbc.username=root jdbc.password=123456 jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring
-
导入到spring 中
@Configuration @ComponentScan("cn.sycoder") @PropertySource("db.properties") @Import({JdbcConfig.class,MyBatisConfig.class}) public class SpringConfig { }
-
创建数据库和数据表
create table account (id bigint auto_incrementprimary key,money int null );
2.4创建Mybatis 配置类
-
创建配置类
public class MyBatisConfig { // 配置SqlSqlSessionFactoryBean@Beanpublic SqlSessionFactoryBean sessionFactory(DataSource dataSource){SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);//设置数据源return bean;}@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer configurer = new MapperScannerConfigurer();configurer.setBasePackage("cn.sycoder.mapper");return configurer;} }
-
配置与 xml 配置的对比
3.添加代码生成器
-
拷贝代码生成器到项目中并且配置插件
-
需要修改代码生成器的数据库连接信息
<build><plugins><!-- 引入 generator 插件--><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><configuration><verbose>true</verbose><overwrite>false</overwrite></configuration><dependencies><!-- 引入插件需要的依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.29</version></dependency></dependencies></plugin></plugins></build>
4.出现问题
-
class not find
-
解决方案
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version> </dependency>
-
解决方案
<dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.29.RELEASE</version> </dependency>
-
包之间的关系