Mapper動態(tài)代理方式

Mapper動態(tài)代理方式遵循四個原則可以取代接口加上實現(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的類型相同
UserMapper.xml配置文件內(nèi)容:
?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.zyh.mybatis.mapper.UserMapper">




<select id="queryUserById" parameterType="int"
resultType="cn.zyh.mybatis.pojo.User">
select * from user where id = #{id}
</select>

<select id="queryUserByUsername" parameterType="string"
resultType="cn.zyh.mybatis.pojo.User">
select * from user where username like '%${value}%'
</select>

<insert id="saveUser" parameterType="cn.zyh..mybatis.pojo.User">
<selectKey keyProperty="id" keyColumn="id" order="AFTER"
resultType="int">
select last_insert_id()
</selectKey>
insert into user(username,birthday,sex,address) values
(#{username},#{birthday},#{sex},#{address});
</insert>
創(chuàng)建UserMapper接口代碼如下:
public interface UserMapper {
/**
* 根據(jù)id查詢
*
* @param id
* @return
*/
User queryUserById(int id);

/**
 * 根據(jù)用戶名查詢用戶
 * 
 * @param username
 * @return
 */
List<User> queryUserByUsername(String username);

/**
 * 保存用戶
 * 
 * @param user
 */
void saveUser(User user);

}
修改SqlMapConfig.xml文件,添加以下所示的內(nèi)容:
<mappers>
<mapper resource="mapper/UserMapper.xml" />
</mappers>
編寫的測試方法如下:
public class UserMapperTest {
private SqlSessionFactory sqlSessionFactory;

@Before
public void init() throws Exception {
    // 創(chuàng)建SqlSessionFactoryBuilder
    SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    // 加載SqlMapConfig.xml配置文件
    InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
    // 創(chuàng)建SqlsessionFactory
    this.sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
}

@Test
public void testQueryUserById() {
    // 獲取sqlSession,和spring整合后由spring管理
    SqlSession sqlSession = this.sqlSessionFactory.openSession();

    // 從sqlSession中獲取Mapper接口的代理對象
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    // 執(zhí)行查詢方法
    User user = userMapper.queryUserById(1);
    System.out.println(user);

    // 和spring整合后由spring管理
    sqlSession.close();
}

@Test
public void testQueryUserByUsername() {
    // 獲取sqlSession,和spring整合后由spring管理
    SqlSession sqlSession = this.sqlSessionFactory.openSession();

    // 從sqlSession中獲取Mapper接口的代理對象
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    // 執(zhí)行查詢方法
    List<User> list = userMapper.queryUserByUsername("張");
    for (User user : list) {
        System.out.println(user);
    }

    // 和spring整合后由spring管理
    sqlSession.close();
}

@Test
public void testSaveUser() {
    // 獲取sqlSession,和spring整合后由spring管理
    SqlSession sqlSession = this.sqlSessionFactory.openSession();

    // 從sqlSession中獲取Mapper接口的代理對象
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    // 創(chuàng)建保存對象
    User user = new User();
    user.setUsername("劉備");
    user.setBirthday(new Date());
    user.setSex("1");
    user.setAddress("蜀國");
    // 執(zhí)行查詢方法
    userMapper.saveUser(user);
    System.out.println(user);


    // 和spring整合后由spring管理
    sqlSession.commit();
    sqlSession.close();
}

}
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接口實現(xiàn)類,使用mapper代理方法時,輸入?yún)?shù)可以使用pojo包裝對象或map對象,保證dao的通用性。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優(yōu)秀的...
    笨鳥慢飛閱讀 6,229評論 0 4
  • 1 緩存介紹# MyBatis支持聲明式數(shù)據(jù)緩存(declarative data caching)。當(dāng)一條SQL...
    七寸知架構(gòu)閱讀 2,222評論 2 51
  • Spring 技術(shù)筆記Day 1 預(yù)熱知識一、 基本術(shù)語Blob類型,二進(jìn)制對象Object Graph:對象圖...
    OchardBird閱讀 1,076評論 0 2
  • 每個線程都應(yīng)該有它自己的SqlSession實例。SqlSession的實例不能共享使用,它是線程不安全的 配置文...
    蕊er閱讀 512評論 0 0
  • 項目要求做一個從匯總數(shù)據(jù)跳轉(zhuǎn)到柱狀圖分項展示的功能,設(shè)計到兩部分?jǐn)?shù)據(jù),一個是后臺數(shù)據(jù)庫分項統(tǒng)計,一個是統(tǒng)計結(jié)果封裝...
    ltjxwxz閱讀 2,400評論 0 0

友情鏈接更多精彩內(nèi)容