您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何在MyBatis中開發Dao層,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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 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 sqlsession.close(); } @Test public void testDeleteUser() { //獲取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //獲取UserMapper接口代理對象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //調用代理對象方法 userMapper.deleteUser(6); //關閉sqlsession sqlsession.close(); } }
上述就是小編為大家分享的如何在MyBatis中開發Dao層了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。