Mybatis实现Mapper * 方式详解
作者:鱼笑笑 时间:2023-08-13 08:37:41
一、实现原理
Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的 * 对象,代理对象的方法体同上边Dao接口实现类方法。
Mapper接口开发需要遵循以下规范:
1、Mapper.xml文件中的namespace与mapper接口的类路径相同。
2、 Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同
4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
二、Mapper.xml映射文件
定义mapper映射文件UserMapper.xml(内容同Users.xml),需要修改namespace的值为 UserMapper接口路径。将UserMapper.xml放在classpath 下mapper目录下。
<?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="cn.itcast.mybatis.mapper.UserMapper">
<!-- 根据id获取用户信息 -->
<select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User">
select * from user where id = #{id}
</select>
<!-- 自定义条件查询用户列表 -->
<select id="findUserByUsername" parameterType="java.lang.String"
resultType="cn.itcast.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
<!-- 添加用户 -->
<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address})
</insert>
</mapper>
三、Mapper.java(接口文件)
/**
* 用户管理mapper
*/
Public interface UserMapper {
//根据用户id查询用户信息
public User findUserById(int id) throws Exception;
//查询用户列表
public List<User> findUserByUsername(String username) throws Exception;
//添加用户信息
public void insertUser(User user)throws Exception;
}
接口定义有如下特点:
1、Mapper接口方法名和Mapper.xml中定义的statement的id相同
2、Mapper接口方法的输入参数类型和mapper.xml中定义的statement的parameterType的类型相同
3、Mapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同
四、加载UserMapper.xml文件
修改sqlMapConfig.xml文件:
<!-- 加载映射文件 -->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
五、测试
Public class UserMapperTest extends TestCase {
private SqlSessionFactory sqlSessionFactory;
protected void setUp() throws Exception {
//mybatis配置文件
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//使用SqlSessionFactoryBuilder创建sessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
Public void testFindUserById() throws Exception {
//获取session
SqlSession session = sqlSessionFactory.openSession();
//获取mapper接口的代理对象
UserMapper userMapper = session.getMapper(UserMapper.class);
//调用代理对象方法
User user = userMapper.findUserById(1);
System.out.println(user);
//关闭session
session.close();
}
@Test
public void testFindUserByUsername() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> list = userMapper.findUserByUsername("张");
System.out.println(list.size());
}
Public void testInsertUser() throws Exception {
//获取session
SqlSession session = sqlSessionFactory.openSession();
//获取mapper接口的代理对象
UserMapper userMapper = session.getMapper(UserMapper.class);
//要添加的数据
User user = new User();
user.setUsername("张三");
user.setBirthday(new Date());
user.setSex("1");
user.setAddress("北京市");
//通过mapper接口添加用户
userMapper.insertUser(user);
//提交
session.commit();
//关闭session
session.close();
}
}
六、总结
selectOne和selectList
* 对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。
namespace
mybatis官方推荐使用mapper代理方法开发mapper接口,程序员不用编写mapper接口实现类,使用mapper代理方法时,输入参数可以使用pojo包装对象或map对象,保证dao的通用性。
来源:http://www.cnblogs.com/efforts-will-be-lucky/p/7388791.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#9.0推出的4个新特性介绍
手把手教你设置IntelliJ IDEA 的彩色代码主题的图文教程
![](https://img.aspxhome.com/file/2023/0/74980_0s.jpg)
SpringBoot配置log4j输出日志的案例讲解
Android自定义viewGroup实现点击动画效果
C#简单读写txt文件的方法
internal修饰符探索kotlin可见性控制详解
java小数位的例子
Spring中的@Transactional的工作原理
![](https://img.aspxhome.com/file/2023/8/58028_0s.webp)
Java值传递之swap()方法不能交换的解决
![](https://img.aspxhome.com/file/2023/4/59594_0s.jpg)
详解Java回环屏障CyclicBarrier
![](https://img.aspxhome.com/file/2023/9/76219_0s.png)
Flutter实现底部导航栏创建详解
![](https://img.aspxhome.com/file/2023/3/126693_0s.jpg)
java连接zookeeper实现zookeeper教程
![](https://img.aspxhome.com/file/2023/7/77127_0s.png)
C#实现对AES加密和解密的方法
Springboot导入本地jar后 打包依赖无法加入的解决方案
![](https://img.aspxhome.com/file/2023/1/87221_0s.png)
Android RadarView雷达图(蜘蛛网图)的实现代码
![](https://img.aspxhome.com/file/2023/5/137545_0s.jpg)
java查找字符串中的包含子字符串的个数实现代码
Android自定义View实现拖动自动吸边效果
![](https://img.aspxhome.com/file/2023/3/93273_0s.gif)