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