这里使用的javassist类是mybatis所自行封装提供的类,其所提供的api和javassist所提供的api相同。
如果测试过程中代码没有问题但是执行ctClass.toClass()出现NullPointerException可以调整升级下mybatis的版本。
【1】创建一个SpringBoot项目,并添加如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
【2】创建一个测试数据表employees,建表语句如下
CREATE TABLE `employees` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
`email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
【3】创建Dao接口
public interface EmployeeDao {
Employee selectById(Long id);
void insert(Employee employee);
void update(Employee employee);
void delete(Long employee);
}
【4】创建对应的xml代码
<?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.zy.mybatis.generatedaoproxy.EmployeeDao">
<insert id="insert">
insert into employees(name,email)
values (#{name},#{email})
</insert>
<select id="selectById" resultType="com.zy.mybatis.generatedaoproxy.Employee">
select * from employees where id=#{id}
</select>
<update id="update">
update employees set name=#{name},email=#{email} where id =#{id}
</update>
<delete id="delete">
delete from employees where id =#{id}
</delete>
</mapper>
【5】创建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>
<properties>
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/EmployeeDao.xml" />
</mappers>
</configuration>
【6】创建一个SqlSessionUtil,用于获取数据库连接
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionUtil {
private SqlSessionUtil() {
}
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
}
private static ThreadLocal<SqlSession> t = new ThreadLocal<>();
public static SqlSession openSession() {
SqlSession session = t.get();
if (session == null) {
session = sqlSessionFactory.openSession(true);
t.set(session);
}
return session;
}
}
我们可以看下通过javassist操作字节码对dao接口中的方法以及XML代码中的SQL语句所生成的实现类方法内容,代码如下所示:
public void update(com.zy.mybatis.generatedaoproxy.Employee arg0) {
org.apache.ibatis.session.SqlSession session = com.zy.mybatis.generatedaoproxy.SqlSessionUtil.openSession();
return session.update("com.zy.mybatis.generatedaoproxy.EmployeeDao.update", arg0);
}
public void delete(java.lang.Long arg0) {
org.apache.ibatis.session.SqlSession session = com.zy.mybatis.generatedaoproxy.SqlSessionUtil.openSession();
session.delete("com.zy.mybatis.generatedaoproxy.EmployeeDao.delete", arg0);
}
public void insert(com.zy.mybatis.generatedaoproxy.Employee arg0) {
org.apache.ibatis.session.SqlSession session = com.zy.mybatis.generatedaoproxy.SqlSessionUtil.openSession();
session.insert("com.zy.mybatis.generatedaoproxy.EmployeeDao.insert", arg0);
}
public com.zy.mybatis.generatedaoproxy.Employee selectById(java.lang.Long arg0) {
org.apache.ibatis.session.SqlSession session = com.zy.mybatis.generatedaoproxy.SqlSessionUtil.openSession();
return (com.zy.mybatis.generatedaoproxy.Employee) session.selectOne("com.zy.mybatis.generatedaoproxy.EmployeeDao.selectById", arg0);
}