這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在MyBatis中開發(fā)Dao層,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元改則做網(wǎng)站,已為上家服務(wù),為改則各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575
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的類型相同
1、定義mapper映射文件UserMapper.xml(內(nèi)容同User.xml),需要修改namespace的值為 UserMapper接口路徑。將UserMapper.xml放在classpath 下sqlmapperr目錄下。
select LAST_INSERT_ID() insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} delete from user where id=#{id}
2、UserMapper.java接口文件
package com.xyfer.mapper; import com.xyfer.po.User; public interface UserMapper { public User getUserById(int id); //根據(jù)id值查詢一個用戶 public void insertUser(User user); //新增一個用戶 public void updateUser(User user); //修改一個用戶 public void deleteUser(int id); //刪除一個用戶 }
接口定義有如下特點(diǎn):
(1)UserMapper接口方法名和Mapper.xml中定義的statement的id相同
(2)UserMapper接口方法的輸入?yún)?shù)類型和mapper.xml中定義的statement的parameterType的類型相同
(3) UserMapper接口方法的輸出參數(shù)類型和mapper.xml中定義的statement的resultType的類型相同
3、在SqlMapConfig.xml文件中加載UserMapper.xml文件
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); //調(diào)用代理對象方法 User user = userMapper.getUserById(10); //打印結(jié)果 System.out.println(user); //關(guān)閉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("浙江省杭州市"); //調(diào)用代理對象方法 userMapper.insertUser(user); //關(guān)閉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("上海市"); //調(diào)用代理對象方法 userMapper.updateUser(user); //關(guān)閉sqlsession sqlsession.close(); } @Test public void testDeleteUser() { //獲取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //獲取UserMapper接口代理對象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //調(diào)用代理對象方法 userMapper.deleteUser(6); //關(guān)閉sqlsession sqlsession.close(); } }
上述就是小編為大家分享的如何在MyBatis中開發(fā)Dao層了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。