Mybatis源码解析1:环境搭建
- 1.项目结构
- 2.pom.xml
- 3.table.sql
- 4. mybatis-config
- 5.Blog
- 6.BlogMapper
- 7.BlogMapper.xml
- 8.MyInterceptor
- 9. InterceptorTest
1.项目结构
github老是打不开,直接贴代码吧
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lmh.example</groupId><artifactId>mybatis-plugin</artifactId><version>1.0-SNAPSHOT</version><description>Mybatis拦截器插件</description><profiles><profile><id>jdk-1.8</id><activation><activeByDefault>true</activeByDefault><jdk>1.8</jdk></activation><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion></properties></profile></profiles><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.79</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency></dependencies>
</project>
3.table.sql
-- 工厂表,每个工厂的负责人,负责维护工厂的信息
CREATE TABLE `Blog` (`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键id',`name` varchar(255) DEFAULT NULL COMMENT '工厂名称',`address` varchar(255) DEFAULT NULL COMMENT '用户信息'PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
4. mybatis-config
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><plugins><plugin interceptor="com.lmh.mybatis.plugin.interceptor.MyInterceptor"/></plugins><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><mappers><mapper resource="mapper/Blog.xml"/></mappers></configuration>
5.Blog
@Data
public class Blog {private Long id;private String name;private String address;
}
6.BlogMapper
public interface BlogMapper {Blog selectById(@Param("id") Long id);}
7.BlogMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lmh.mybatis.plugin.mapper.BlogMapper"><select id="selectById" resultType="com.lmh.mybatis.plugin.entity.Blog">select id, name, address from blog where id = #{id}</select></mapper>
8.MyInterceptor
@Slf4j
@Intercepts(@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}))
public class MyInterceptor implements Interceptor {/*** 拦截器调用执行的逻辑*/@Overridepublic Object intercept(Invocation invocation) throws Throwable {log.info("Process interceptor for query method...");return invocation.proceed();}/*** 注册拦截器,父类 已注册*/@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}/*** 设置属性*/@Overridepublic void setProperties(Properties properties) {}
}
9. InterceptorTest
/*** 测试类*/
@Slf4j
public class InterceptorTest {public static void main(String[] args) throws IOException {//获取文件配置流InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");//创建SqlSessionFactoryBuilder对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();//通过SqlSessionFactoryBuilder对象解析配置文件,config.xml和mapper.xml//解析到的mapper.xml文件会创建Statement对象,并且注册到Configuration#mappedStatementsSqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);//通过sqlSessionFactory创建SqlSession,根据解析到mapper.xml获取Sql的类型,创建执行器//这里还会对执行起来Executor进行代理,最终封装在DefaultSqlSession对象里面try(SqlSession sqlSession = sqlSessionFactory.openSession()) {//通过SqlSession获取到指定的Mapper,实际返回的是JDK代理对象BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);Blog blog = mapper.selectById(1L);log.error(JSON.toJSONString(blog));}}
}