MyBatis開發原始Dao層請閱讀我的上一篇博客:MyBatis開發Dao層的兩種方式(原始Dao層開發) 接上一篇博客繼續介紹MyBatis開發Dao層的第二種方式:Mapper動態代理方式 Mapper介面開發方法只需要程式員編寫Mapper介面(相當於Dao介面),由Mybatis框架根據接 ...
MyBatis開發原始Dao層請閱讀我的上一篇博客:MyBatis開發Dao層的兩種方式(原始Dao層開發)
接上一篇博客繼續介紹MyBatis開發Dao層的第二種方式:Mapper動態代理方式
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的類型相同
1、定義mapper映射文件UserMapper.xml(內容同User.xml),需要修改namespace的值為 UserMapper介面路徑。將UserMapper.xml放在classpath 下sqlmapperr目錄下。
<?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.xyfer.mapper.UserMapper"> <!-- 根據id查詢用戶 --> <select id="getUserById" parameterType="int" resultType="com.xyfer.po.User"> select * from user where id = #{id} </select> <!-- 添加用戶 --> <insert id="insertUser" parameterType="com.xyfer.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> <!-- 修改用戶 --> <update id="updateUser" parameterType="com.xyfer.po.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} </update> <!-- 刪除用戶 --> <delete id="deleteUserById" parameterType="int"> delete from user where id=#{id} </delete> </mapper>
2、UserMapper.java介面文件
package com.xyfer.mapper; import com.xyfer.po.User; public interface UserMapper { public User getUserById(int id); //根據id值查詢一個用戶 public void insertUser(User user); //新增一個用戶 public void updateUser(User user); //修改一個用戶 public void deleteUser(int id); //刪除一個用戶 }
介面定義有如下特點:
(1)UserMapper介面方法名和Mapper.xml中定義的statement的id相同
(2)UserMapper介面方法的輸入參數類型和mapper.xml中定義的statement的parameterType的類型相同
(3) UserMapper介面方法的輸出參數類型和mapper.xml中定義的statement的resultType的類型相同
3、在SqlMapConfig.xml文件中載入UserMapper.xml文件
<mappers> <mapper resource="UserMapper.xml"/> </mappers>
4、測試
package com.xyfer.mapper; import static org.junit.Assert.*; import java.io.InputStream; import java.util.Date; 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.junit.Before; import org.junit.Test; import com.xyfer.po.User; import junit.framework.TestCase; public class UserMapperTest extends TestCase{ private SqlSessionFactory sqlSessionFactory; protected void setUp() throws Exception { SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder(); InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); sqlSessionFactory = sessionFactoryBuilder.build(inputStream); } @Test public void testGetUserById() { //獲取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //獲取UserMapper介面代理對象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //調用代理對象方法 User user = userMapper.getUserById(10); //列印結果 System.out.println(user); //關閉sqlsession sqlsession.close(); } @Test public void testInsertUser() { //獲取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //獲取UserMapper介面代理對象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //新建一個對象 User user = new User(); user.setUsername("小謝"); user.setSex("男"); user.setBirthday(new Date()); user.setAddress("浙江省杭州市"); //調用代理對象方法 userMapper.insertUser(user); //提交事務 sqlsession.commit(); //關閉sqlsession sqlsession.close(); } @Test public void testUpdateUser() { //獲取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //獲取UserMapper介面代理對象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //新建一個對象 User user = new User(); user.setUsername("小謝"); user.setSex("男"); user.setBirthday(new Date()); user.setAddress("上海市"); //調用代理對象方法 userMapper.updateUser(user); //提交事務 sqlsession.commit(); //關閉sqlsession sqlsession.close(); } @Test public void testDeleteUser() { //獲取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //獲取UserMapper介面代理對象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //調用代理對象方法 userMapper.deleteUser(6); //提交事務 sqlsession.commit(); //關閉sqlsession sqlsession.close(); } }
以上步驟,完成mybatis框架以Mapper動態代理方式開發Dao層,並對資料庫進行增刪改查操作。
需要註意的是,上文提到的config文件夾和sqlmapper文件夾均為資源文件夾(source folder),預設會載入該路徑下的文件。