mybatis實(shí)現(xiàn)查詢(xún)操作及獲得添加的ID
- 接口
/** * 獲得所有用戶(hù) * @return */ List<User> findAll(); /** * 根據(jù)id查詢(xún)用戶(hù) * @param id * @return */ User findById(Integer id); /** *根據(jù)名字模糊查詢(xún) * @param username * @return */ List<User> findByName(String username); /** *查詢(xún)總記錄條數(shù) * @param * @return */ int findTotal();
- mapper
<!-- 獲得所有用戶(hù)--> <select id="findAll" resultType="com.itheima.domain.User"> select * from user; </select> <!-- 根據(jù)id查詢(xún)用戶(hù)--> <select id="findById" parameterType="int" resultType="com.itheima.domain.User"> select * from user where id=#{id}; </select> <!-- 根據(jù)名字模糊查詢(xún)--> <select id="findByName" parameterType="String" resultType="com.itheima.domain.User"> select * from user where username like #{username} </select> <!-- 查詢(xún)總記錄條數(shù)--> <select id="findTotal" resultType="int"> select count(id) from user; </select>
- 測(cè)試
/** * 測(cè)試查詢(xún)所有 */ @Test public void testSelectAll(){ List<User> user = userDao.findAll(); for(User user1 : user){ System.out.println(user1); } } /** * 測(cè)試根據(jù)id查詢(xún)user */ @Test public void testFindById() { User user = userDao.findById(57); System.out.println(user); } /** * 測(cè)試根據(jù)名字模糊查詢(xún) */ @Test public void testFindByName() { List<User> list = userDao.findByName("%王%"); for(User user:list){ System.out.println(user); } } /** * 測(cè)試獲得總記錄條數(shù) */ @Test public void testFindTotal() { int count = userDao.findTotal(); System.out.println(count); }
添加一組數(shù)據(jù),同時(shí)獲得他的id值:last_insert_id()
接口
/** * 添加用戶(hù) */ void saveUser(User user);
mapper
<!-- 添加一個(gè)用戶(hù);同時(shí)獲得用戶(hù)的id值--> <insert id="saveUser" parameterType="com.itheima.domain.User"> <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER"> select last_insert_id(); </selectKey> insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) </insert>
測(cè)試
/** * 測(cè)試添加用戶(hù),同時(shí)獲得添加之后id值 */ @Test public void testSave(){ User user = new User(); user.setUsername("mybatis inserid"); user.setBirthday(new Date()); user.setSex("女"); user.setAddress("香港"); System.out.println("保存操作之前:" + user); userDao.saveUser(user); System.out.println("保存操作之后:" + user); }
到此這篇關(guān)于mybatis實(shí)現(xiàn)查詢(xún)操作及獲得添加的ID的文章就介紹到這了,更多相關(guān)mybatis獲得添加的ID內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(一)
這篇文章主要介紹了Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05Java不帶break將導(dǎo)致case穿透問(wèn)題
這篇文章主要介紹了Java不帶break將導(dǎo)致case穿透問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Java中Collection、List、Set、Map之間的關(guān)系總結(jié)
今天小編就為大家分享一篇關(guān)于Java中Collection、List、Set、Map之間的關(guān)系總結(jié),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Linux中使用shell腳本管理Java應(yīng)用程序
在日常開(kāi)發(fā)和運(yùn)維工作中,管理基于Java的應(yīng)用程序是一項(xiàng)基礎(chǔ)且頻繁的任務(wù),本文將通過(guò)一個(gè)示例腳本,展示如何利用Shell腳本簡(jiǎn)化這一流程,實(shí)現(xiàn)Java應(yīng)用的一鍵式啟動(dòng)、停止與重啟操作,本腳本不僅提升了工作效率,還確保了操作的標(biāo)準(zhǔn)化與可靠性2024-06-06Mybatis的mapper.xml中if標(biāo)簽test判斷的用法說(shuō)明
這篇文章主要介紹了Mybatis的mapper.xml中if標(biāo)簽test判斷的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06一文詳解Spring任務(wù)執(zhí)行和調(diào)度(小結(jié))
這篇文章主要介紹了一文詳解Spring任務(wù)執(zhí)行和調(diào)度(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08