2.3.1 Mapper代理开发概述
之前我们写的代码是基本使用方式,它也存在硬编码的问题,如下:
这里调用
selectList()
方法传递的参数是映射配置文件中的 namespace.id值。这样写也不便于后期的维护。如果使用 Mapper 代理方式(如下图)则不存在硬编码问题。
通过上面的描述可以看出 Mapper 代理方式的目的:
-
解决原生方式中的硬编码
-
简化后期执行SQL
Mybatis 官网也是推荐使用 Mapper 代理的方式。下图是截止官网的图片
1.3.2 使用Mapper代理要求
使用Mapper代理方式,必须满足以下要求:
-
定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下。如下图:
设置SQL映射文件的namespace属性为Mapper接口全限定名
<mapper namespace="org.example.mapper.UserMapper">
<select id="selectAll" resultType="org.example.pojo.User">
select * from tb_user
</select>
</mapper>
在 Mapper 接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
2.3.3 案例代码实现
-
在
org.example.mapper
包下创建 UserMapper接口,代码如下:
package org.example.mapper;
import org.example.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> selectAll();
}
在 resources
下创建 org/example/mapper
目录,并在该目录下创建 UserMapper.xml 映射配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
namespace:名称空间。必须是对应接口的全限定名
-->
<mapper namespace="org.example.mapper.UserMapper">
<select id="selectAll" resultType="org.example.pojo.User">
select * from tb_user
</select>
</mapper>
在MyBatisDemo类中添加test1方法
package org.example;
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 org.example.mapper.UserMapper;
import org.example.pojo.User;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyBatisDemo {
public static void main(String[] args) throws IOException {
test1();
}
public static void test0() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> users = sqlSession.selectList("test.selectAll");
System.out.println(users);
sqlSession.close();
}
public static void test1() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectAll();
System.out.println(users);
sqlSession.close();
}
}
运行结果
D:\Java\jdk-1.8\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA 2023.2.5\lib\idea_rt.jar=50245:D:\JetBrains\IntelliJ IDEA 2023.2.5\bin" -Dfile.encoding=UTF-8 -classpath D:\Java\jdk-1.8\jre\lib\charsets.jar;D:\Java\jdk-1.8\jre\lib\deploy.jar;D:\Java\jdk-1.8\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk-1.8\jre\lib\ext\cldrdata.jar;D:\Java\jdk-1.8\jre\lib\ext\dnsns.jar;D:\Java\jdk-1.8\jre\lib\ext\jaccess.jar;D:\Java\jdk-1.8\jre\lib\ext\jfxrt.jar;D:\Java\jdk-1.8\jre\lib\ext\localedata.jar;D:\Java\jdk-1.8\jre\lib\ext\nashorn.jar;D:\Java\jdk-1.8\jre\lib\ext\sunec.jar;D:\Java\jdk-1.8\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk-1.8\jre\lib\ext\sunmscapi.jar;D:\Java\jdk-1.8\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk-1.8\jre\lib\ext\zipfs.jar;D:\Java\jdk-1.8\jre\lib\javaws.jar;D:\Java\jdk-1.8\jre\lib\jce.jar;D:\Java\jdk-1.8\jre\lib\jfr.jar;D:\Java\jdk-1.8\jre\lib\jfxswt.jar;D:\Java\jdk-1.8\jre\lib\jsse.jar;D:\Java\jdk-1.8\jre\lib\management-agent.jar;D:\Java\jdk-1.8\jre\lib\plugin.jar;D:\Java\jdk-1.8\jre\lib\resources.jar;D:\Java\jdk-1.8\jre\lib\rt.jar;D:\idea2023.3workspace\Mybatis_you\mybatis_you\target\classes;D:\maven\repository\org\mybatis\mybatis\3.5.5\mybatis-3.5.5.jar;D:\maven\repository\org\slf4j\slf4j-api\1.7.20\slf4j-api-1.7.20.jar;D:\maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\maven\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar;D:\maven\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar org.example.MyBatisDemo
[DEBUG] [main] o.a.i.l.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
[DEBUG] [main] o.a.i.d.p.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[DEBUG] [main] o.a.i.d.p.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[DEBUG] [main] o.a.i.d.p.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[DEBUG] [main] o.a.i.d.p.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[DEBUG] [main] o.a.i.t.j.JdbcTransaction - Opening JDBC Connection
[DEBUG] [main] o.a.i.d.p.PooledDataSource - Created connection 318857719.
[DEBUG] [main] o.a.i.t.j.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@130161f7]
[DEBUG] [main] o.e.m.U.selectAll - ==> Preparing: select * from tb_user
[DEBUG] [main] o.e.m.U.selectAll - ==> Parameters:
[DEBUG] [main] o.e.m.U.selectAll - <== Total: 3
[User{id=1, username='zhangsan', password='123', gender='男', addr='北京'}, User{id=2, username='李四', password='234', gender='女', addr='天津'}, User{id=3, username='王五', password='11', gender='男', addr='西安'}]
[DEBUG] [main] o.a.i.t.j.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@130161f7]
[DEBUG] [main] o.a.i.t.j.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@130161f7]
[DEBUG] [main] o.a.i.d.p.PooledDataSource - Returned connection 318857719 to pool.
进程已结束,退出代码为 0
==注意:==
如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载,那么org.example.mapper目录下的同名xml文件都会映射到org.example.mapper目录下的mapper文件
。也就是将核心配置文件的加载映射配置文件的配置修改为
<mappers>
<!--加载sql映射文件-->
<!-- <mapper resource="org/example/mapper/UserMapper.xml"/>-->
<!--Mapper代理方式-->
<package name="org.example.mapper"/>
</mappers>