本章介绍Mybatis与Spring整合的mybatis-spring.jar的编译,阅读本章前,现看
【Spring】Spring源码编译 和 【Mybatis】MyBatis源码编译
环境准备
- Maven:3.6.3
- Jdk:1.8.0_181
- idea
1、下载mybatis-spring源码
官网地址:https://github.com/mybatis/spring
选择需要的版本下载,本例下载的是 mybatis-spring-2.0.1,下载完后解压。打开pom.xml,查看mybatis的依赖的父工程版本
2、下载载mybatis-parent源码
选择mybatis对应的mybatis-parent版本,本例版本是 mybatis-parent-31
官网地址:https://github.com/mybatis/parent
3、源码导入Idea
在Idea中新建一个空项目,将 mybatis-spring 、 mybatis-parent 都放到空项目下,并导入模块
本例直接放在上一章(【Mybatis】MyBatis源码编译) mybatis 源码目录中
4、编译mybatis-parent源码,编译mybatis源码
1、编译mybatis-parent项目
切换mybatis-parent项目:
命令:mvn clean install
2、编译mybatis-spring项目
切换mybatis-spring项目(可以修改一下版本号,修改成自己特有的版本,方便区分):
修改mybatis-spring版本(2.0.1-MY)。避免与官网依赖相同版本
命令:mvn install -Dmaven.test
5、测试使用源码
运行mybatis-spring项中的测试类
org.mybatis.spring.SqlSessionFactoryBeanTest.java#testDefaults()
运行成功,说明已经编译好了
6、Demo使用源码
1、安装 mybatis源码 和 mybatis-spring 源码
将项目中 mybatis源码(使用自己的版本号: 3.5.1-MY) 和 mybatis-spring 源码(使用自己的版本号: 2.0.1-MY),
安装的maven仓库中,这样其他项目引用你编译的源码就能看到其中的注释
使用Maven插件:maven-source-plugin
<!-- 生成sources源码包的插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.2.1</version> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin>
使用命令:mvn install
2、在Spring源码目录中新建测试项目
项目参考:【Spring】Spring源码编译
1、spring-my-mybatis.gradle,gradle配置文件如下:引入自己编译的 mybatis源码 和 mybatis-spring 源码
description = "Spring MY MYBATIS"
apply plugin: "kotlin"
dependencies {
compile "mysql:mysql-connector-java:8.0.12"
compile "com.alibaba:druid:1.1.8"
compile "org.mybatis:mybatis:3.5.1-MY"
compile "org.mybatis:mybatis-spring:2.0.1-MY"
compile "ognl:ognl:3.2.15"
compile "org.javassist:javassist:3.27.0-GA"
compile(project(":spring-core"))
optional(project(":spring-aop"))
optional(project(":spring-beans"))
optional(project(":spring-context"))
optional(project(":spring-jdbc"))
optional(project(":spring-orm"))
optional(project(":spring-tx"))
}
2、项目配置类 SpringMyBatisConfig.java
1 package com.test.spring.mybatis.config; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import com.test.spring.mybatis.entity.Employee; 5 import org.mybatis.spring.SqlSessionFactoryBean; 6 import org.mybatis.spring.annotation.MapperScan; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.ComponentScan; 9 import org.springframework.context.annotation.Configuration; 10 import org.springframework.core.io.ClassPathResource; 11 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 12 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 13 import org.springframework.transaction.TransactionManager; 14 15 import javax.sql.DataSource; 16 import java.io.IOException; 17 18 @Configuration 19 // 扫描Service,注入Spring 20 @ComponentScan("com.test.spring.mybatis.service") 21 // 扫描Mapper 22 @MapperScan("com.test.spring.mybatis.mapper") 23 public class SpringMyBatisConfig { 24 25 // 数据源 26 @Bean("dataSource") 27 public DataSource dataSource(){ 28 System.out.println("初始化数据源 DruidDataSource "); 29 30 String url="jdbc:mysql://127.0.0.1:3306/test_mybatis?allowPublicKeyRetrieval=true&useSSL=true"; 31 String username = "root"; 32 String password = "123456"; 33 String driverClassName = "com.mysql.cj.jdbc.Driver"; 34 35 DruidDataSource druidDataSource = new DruidDataSource(); 36 druidDataSource.setUsername(username); 37 druidDataSource.setPassword(password); 38 druidDataSource.setUrl(url); 39 druidDataSource.setDriverClassName(driverClassName); 40 41 return druidDataSource; 42 } 43 44 // 事物管理器 45 @Bean 46 public TransactionManager transactionManager(){ 47 return new DataSourceTransactionManager(dataSource()); 48 } 49 50 // SqlSeesion工厂Bean 51 @Bean 52 public SqlSessionFactoryBean sqlSessionFactory() throws IOException { 53 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); 54 factoryBean.setDataSource(dataSource()); 55 // 设置 MyBatis 配置文件路径 56 factoryBean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml")); 57 // 设置 SQL 映射文件路径 58 factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml")); 59 // 这是类型别名 60 factoryBean.setTypeAliases(new Class[]{Employee.class}); 61 return factoryBean; 62 } 63 }
3、实体类 Employee.java
1 package com.test.spring.mybatis.entity; 2 3 import java.io.Serializable; 4 5 public class Employee implements Serializable { 6 7 private Integer id; 8 private String lastName; 9 private String gender; 10 private String email; 11 public Integer getId() { 12 return id; 13 } 14 public void setId(Integer id) { 15 this.id = id; 16 } 17 public String getLastName() { 18 return lastName; 19 } 20 public void setLastName(String lastName) { 21 this.lastName = lastName; 22 } 23 public String getGender() { 24 return gender; 25 } 26 public void setGender(String gender) { 27 this.gender = gender; 28 } 29 public String getEmail() { 30 return email; 31 } 32 public void setEmail(String email) { 33 this.email = email; 34 } 35 @Override 36 public String toString() { 37 return "Employee [id=" + id + ", lastName=" + lastName + ", gender=" + gender + ", email=" + email + "]"; 38 } 39 40 41 }
4、Mapper类 EmployeeMapper.java
1 package com.test.spring.mybatis.mapper; 2 3 import com.test.spring.mybatis.entity.Employee; 4 import org.apache.ibatis.annotations.Param; 5 6 public interface EmployeeMapper { 7 8 public Employee getEmployeeById(@Param("id") Integer id); 9 10 }
5、服务类 EmployeeService.java
1 package com.test.spring.mybatis.service; 2 3 import com.test.spring.mybatis.entity.Employee; 4 import com.test.spring.mybatis.mapper.EmployeeMapper; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 @Service 9 public class EmployeeService { 10 11 @Autowired 12 EmployeeMapper employeeMapper; 13 14 public Employee getEmployeeById(Integer id) { 15 return employeeMapper.getEmployeeById(id); 16 } 17 }
6、mybatis全局配置文件 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
7、Mapper配置文件 EmployeeMapper
<?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.test.spring.mybatis.mapper.EmployeeMapper"> <select id="getEmployeeById" resultType="com.test.spring.mybatis.entity.Employee"> select id, last_name lastName, gender, email from employee <where> <if test="1 > 0"> AND id = #{id} </if> </where> </select> </mapper>
8、主启动类 SpringMyBatisMainTest.java
1 package com.test.spring.mybatis; 2 3 import com.test.spring.mybatis.config.SpringMyBatisConfig; 4 import com.test.spring.mybatis.entity.Employee; 5 import com.test.spring.mybatis.service.EmployeeService; 6 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 7 8 public class SpringMyBatisMainTest { 9 10 public static void main(String[] args) { 11 AnnotationConfigApplicationContext context = 12 new AnnotationConfigApplicationContext(SpringMyBatisConfig.class); 13 14 EmployeeService employeeService = context.getBean(EmployeeService.class); 15 16 Employee employee = employeeService.getEmployeeById(1); 17 18 System.out.println(employee); 19 20 } 21 }
3、测试运行主启动类
运行成功,这样即可在项目中看到自己编译的带注释的 mybatis源码 和 mybatis-spring 源码
可能出现的问题
问题一:Javassist 依赖问题
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in com.test.spring.mybatis.config.SpringMyBatisConfig: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in com.test.spring.mybatis.config.SpringMyBatisConfig: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.
解决:添加依赖javassist,同时将ognl依赖也添加好,即可解决
compile "ognl:ognl:3.2.15"
compile "org.javassist:javassist:3.27.0-GA"