Mybatis實(shí)現(xiàn)Mapper動態(tài)代理方式詳解
一、實(shí)現(xiàn)原理
Mapper接口開發(fā)方法只需要程序員編寫Mapper接口(相當(dāng)于Dao接口),由Mybatis框架根據(jù)接口定義創(chuàng)建接口的動態(tài)代理對象,代理對象的方法體同上邊Dao接口實(shí)現(xiàn)類方法。
Mapper接口開發(fā)需要遵循以下規(guī)范:
1、Mapper.xml文件中的namespace與mapper接口的類路徑相同。
2、 Mapper接口方法名和Mapper.xml中定義的每個statement的id相同
3、Mapper接口方法的輸入?yún)?shù)類型和mapper.xml中定義的每個sql 的parameterType的類型相同
4、Mapper接口方法的輸出參數(shù)類型和mapper.xml中定義的每個sql的resultType的類型相同
二、Mapper.xml映射文件
定義mapper映射文件UserMapper.xml(內(nèi)容同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">
<!-- 根據(jù)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 {
//根據(jù)用戶id查詢用戶信息
public User findUserById(int id) throws Exception;
//查詢用戶列表
public List<User> findUserByUsername(String username) throws Exception;
//添加用戶信息
public void insertUser(User user)throws Exception;
}
接口定義有如下特點(diǎn):
1、Mapper接口方法名和Mapper.xml中定義的statement的id相同
2、Mapper接口方法的輸入?yún)?shù)類型和mapper.xml中定義的statement的parameterType的類型相同
3、Mapper接口方法的輸出參數(shù)類型和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創(chuàng)建sessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
Public void testFindUserById() throws Exception {
//獲取session
SqlSession session = sqlSessionFactory.openSession();
//獲取mapper接口的代理對象
UserMapper userMapper = session.getMapper(UserMapper.class);
//調(diào)用代理對象方法
User user = userMapper.findUserById(1);
System.out.println(user);
//關(guān)閉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);
//要添加的數(shù)據(jù)
User user = new User();
user.setUsername("張三");
user.setBirthday(new Date());
user.setSex("1");
user.setAddress("北京市");
//通過mapper接口添加用戶
userMapper.insertUser(user);
//提交
session.commit();
//關(guān)閉session
session.close();
}
}
六、總結(jié)
selectOne和selectList
動態(tài)代理對象調(diào)用sqlSession.selectOne()和sqlSession.selectList()是根據(jù)mapper接口方法的返回值決定,如果返回list則調(diào)用selectList方法,如果返回單個對象則調(diào)用selectOne方法。
namespace
mybatis官方推薦使用mapper代理方法開發(fā)mapper接口,程序員不用編寫mapper接口實(shí)現(xiàn)類,使用mapper代理方法時,輸入?yún)?shù)可以使用pojo包裝對象或map對象,保證dao的通用性。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用lamda表達(dá)式對list進(jìn)行求和
這篇文章主要介紹了如何使用lamda表達(dá)式對list進(jìn)行求和問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
PowerJob的QueryConvertUtils工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的QueryConvertUtils工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
詳解Reactor如何優(yōu)雅Exception異常處理
初識響應(yīng)式編程的時候,除了從命令式的思維方式轉(zhuǎn)變?yōu)楹瘮?shù)式的編程方式外,其中有一個很大的不適應(yīng)的地方就是在面對異常時該怎么處理。本文將通過Project?Reactor的文檔以及源碼來深入解讀,在reactor中是如何優(yōu)雅地實(shí)現(xiàn)這異常處理三板斧,希望對大家有所幫助2023-02-02
RocketMQ中的消費(fèi)模式和消費(fèi)策略詳解
這篇文章主要介紹了RocketMQ中的消費(fèi)模式和消費(fèi)策略詳解,RocketMQ 是基于發(fā)布訂閱模型的消息中間件,所謂的發(fā)布訂閱就是說,consumer 訂閱了 broker 上的某個 topic,當(dāng) producer 發(fā)布消息到 broker 上的該 topic 時,consumer 就能收到該條消息,需要的朋友可以參考下2023-10-10
基于SpringBoot+Mybatis實(shí)現(xiàn)Mysql分表
這篇文章主要為大家詳細(xì)介紹了基于SpringBoot+Mybatis實(shí)現(xiàn)Mysql分表的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
AsyncHttpClient IOExceptionFilter異常過濾器
這篇文章主要為大家介紹了AsyncHttpClient IOExceptionFilter異常過濾器代碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Java中的synchronized有幾種加鎖方式(實(shí)例詳解)
在Java中,synchronized關(guān)鍵字提供了內(nèi)置的支持來實(shí)現(xiàn)同步訪問共享資源,以避免并發(fā)問題,這篇文章主要介紹了java的synchronized有幾種加鎖方式,需要的朋友可以參考下2024-05-05

