MyBatis的CRUD中的不同參數(shù)綁定查詢實現(xiàn)
com.by.pojo下的User類
package com.by.pojo; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private Integer id; private String username; private Date birthday; private String sex; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}'; } }
測試類
private SqlSession sqlSession; private InputStream inputStream; @Before public void init() throws IOException { //加載配置文件 String resource = "mybatis-config.xml"; inputStream = Resources.getResourceAsStream(resource); //創(chuàng)建SessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //使用數(shù)據(jù)的會話實例 sqlSession = sessionFactory.openSession(); } ? @After public void close() throws IOException { sqlSession.close(); inputStream.close(); } ?
單個參數(shù)傳遞綁定
在UserDao接口中:
User getUserById(Integer id);
在UserDao.xml中:
<!-- 傳遞單個參數(shù)--> <select id="getUserById" parameterType="java.lang.Integer" resultType="com.by.pojo.User"> select * from user where id=#{id}; </select>
測試類:
@Test //傳遞單個參數(shù)有 public void testgetUserById() throws IOException { //返回接口的代理類 UserDao userDao = sqlSession.getMapper(UserDao.class); User user = userDao.getUserById(43); System.out.println(user); }
序號參數(shù)傳遞綁定
在UserDao接口中:
//序號多個參數(shù) User getUser(Integer id, String username);
在UserDao.xml中:
<!-- 序號傳遞多個參數(shù)--> <select id="getUser" resultType="com.by.pojo.User"> select * from user where id=#{arg0} and username=#{arg1}; select * from user where id=#{param1} and username=#{param2}; </select>
測試類:
@Test //序號傳遞多個參數(shù) public void testgetUser() throws IOException { //返回接口的代理類 UserDao userDao = sqlSession.getMapper(UserDao.class); User user = userDao.getUser(43, "俞蓮舟"); System.out.println(user); }
注解參數(shù)傳遞綁定
在UserDao接口中:
//注解多個參數(shù) User getUser2(@Param("id") Integer id, @Param("username") String username);
在UserDao.xml中:
<!-- 注解傳遞多個參數(shù)--> <select id="getUser2" resultType="com.by.pojo.User"> select * from user where id=#{id} and username=#{username}; </select>
測試類:
@Test //注解傳遞多個參數(shù) public void testgetUser2() throws IOException { //返回接口的代理類 UserDao userDao = sqlSession.getMapper(UserDao.class); User user = userDao.getUser2(43, "俞蓮舟"); System.out.println(user); }
pojo(對象)參數(shù)傳遞綁定
在UserDao接口中:
//pojo參數(shù) User getUser3(User user);
在UserDao.xml中:
<!-- pojo傳遞多個參數(shù)--> <select id="getUser3" parameterType="com.by.pojo.User" resultType="com.by.pojo.User"> select * from user where id=#{id} and username=#{username}; </select>
測試類:
@Test //pojo(對象)傳遞多個參數(shù) public void testgetUser3() throws IOException { //返回接口的代理類 UserDao userDao = sqlSession.getMapper(UserDao.class); User userParam = new User(); userParam.setId(43); userParam.setUsername("俞蓮舟"); User user = userDao.getUser3(userParam); System.out.println(user); }
map參數(shù)傳遞綁定
在UserDao接口中:
//map參數(shù) User getUser4(Map<String, Object> map);
在UserDao.xml中:
</select> <!-- map傳遞多個參數(shù)--> <select id="getUser4" parameterType="java.util.Map" resultType="com.by.pojo.User"> select * from user where id=#{id} and username=#{username}; </select>
測試類:
@Test //map傳遞多個參數(shù) public void testgetUser4() throws IOException { //返回接口的代理類 UserDao userDao = sqlSession.getMapper(UserDao.class); HashMap<String, Object> map = new HashMap<>(); map.put("id", 43); map.put("username", "俞蓮舟"); User user = userDao.getUser4(map); System.out.println(user); }
到此這篇關(guān)于MyBatis的CRUD中的不同參數(shù)綁定查詢實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis CRUD參數(shù)綁定查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基于websocket協(xié)議與netty實時視頻彈幕交互實現(xiàn)
本文主要介紹了Java基于websocket協(xié)議與netty實時視頻彈幕交互實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09eclipse/intellij idea 遠程調(diào)試hadoop 2.6.0
這篇文章主要介紹了eclipse/intellij idea 遠程調(diào)試hadoop 2.6.0的相關(guān)資料,需要的朋友可以參考下2016-07-07Spring監(jiān)聽器及定時任務(wù)實現(xiàn)方法詳解
這篇文章主要介紹了Spring監(jiān)聽器及定時任務(wù)實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07Java各種鎖在工作中使用場景和細節(jié)經(jīng)驗總結(jié)
本章主要說一說鎖在工作中的使用場景,主要以 synchronized 和 CountDownLatch 為例,會分別描述一下這兩種鎖的使用場景和姿勢2022-03-03Java實現(xiàn)深度優(yōu)先搜索(DFS)和廣度優(yōu)先搜索(BFS)算法
深度優(yōu)先搜索(DFS)和廣度優(yōu)先搜索(BFS)是兩種基本的圖搜索算法,可用于圖的遍歷、路徑搜索等問題。DFS采用棧結(jié)構(gòu)實現(xiàn),從起點開始往深處遍歷,直到找到目標節(jié)點或遍歷完整個圖;BFS采用隊列結(jié)構(gòu)實現(xiàn),從起點開始往廣處遍歷,直到找到目標節(jié)點或遍歷完整個圖2023-04-04Java中字符串與byte數(shù)組之間的相互轉(zhuǎn)換
Java語言中字符串類型和字節(jié)數(shù)組類型相互之間的轉(zhuǎn)換經(jīng)常發(fā)生,網(wǎng)上的分析及代碼也比較多,這篇文章將主要介紹Java中字符串與byte數(shù)組之間的相互轉(zhuǎn)換,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-10-10