🍁Mapper代理
传统 dao层开发有一些地方是重复工作的,而 MyBatis 提供了另一种高效开发的方式,那就是Mapper代理,但是使用Mapper代理开发应该遵循Mybatis 的规范。
第一步搭建MyBatis项目:MyBatis入门程序
🍁用mapper代理的方式开发dao层要遵循一些规范:
1、Mapper接口和映射文件在同一个目录下,映射文件一般放在resources中。
2、userMapper.xml中namespace指定为mapper接口的全限定名。
<mapper namespace="com.jkj.Mapper.UserMapper">
3、userMapper.xml中id就是UserMapper.java中方法名
<select id="selectAll" resultType="com.jkj.pojo.User">
4、配置别名
- 在配置文件(mybatis-config.xml或者是其他名字)中的正确位置加入以下代码:
<!--起别名,不区分大小写-->
<typeAliases>
<package name="com.jkj.pojo"/>
</typeAliases>
- 设置别名后之前的select就可以改成:
<select id="selectAll" resultType="User">
select * from tb_user;
</select>
🍁编码
🔥调用SQLSession的getMapper方法获取Mapper接口的代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
🔥调用对应的方法执行sql语句
List<User> users = userMapper.selectAll();
🍁案例
user.java:
package com.jkj.pojo;
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String addr;
public User() {
}
public User(Integer id, String username, String password, String gender, String addr) {
this.id = id;
this.username = username;
this.password = password;
this.gender = gender;
this.addr = addr;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", addr='" + addr + '\'' +
'}';
}
}
UserMapper.java:
package com.jkj.Mapper;
import com.jkj.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> selectAll();
}
userMapper.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.jkj.Mapper.UserMapper">
<select id="selectAll" resultType="User">
select * from tb_user;
</select>
</mapper>
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 core file-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--加载sql映射文件-->
<mappers>
<!--<mapper resource="com/jkj/Mapper/userMapper.xml"/>-->
<!--Mapper代理方式-->
<package name="com.jkj.Mapper"/>
</mappers>
</configuration>
MybatisDemo.java测试类:
import com.jkj.Mapper.UserMapper;
import com.jkj.pojo.User;
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;
import java.util.List;
public class MybatisDemo {
public static void main(String[] args) throws IOException {
//1.加载MyBatis的核心配置文件获取sqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取sqlSession,执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取UserMapper接口的代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> users = userMapper.selectAll();
System.out.println(users);
//4.释放资源
sqlSession.close();
}
}
运行结果:
[User{id=1, username='小马哥', password='111', gender='男', addr='北京'}, User{id=2, username='小飞侠', password='222', gender='男', addr='上海'}, User{id=3, username='马奎斯', password='333', gender='女', addr='西班牙'}]