小編給大家分享一下Mybatis的基礎(chǔ)知識點(diǎn),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、網(wǎng)站建設(shè)、項(xiàng)城網(wǎng)絡(luò)推廣、重慶小程序開發(fā)公司、項(xiàng)城網(wǎng)絡(luò)營銷、項(xiàng)城企業(yè)策劃、項(xiàng)城品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供項(xiàng)城建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
mybatis
mybatis-config.xml詳細(xì)配置(配置時(shí)要把多余的屬性刪除 不能有中文 否則報(bào)錯(cuò)!)
分頁
減少數(shù)據(jù)訪問量
limt實(shí)現(xiàn)分頁
sql語句: select * from 表名 limt 0,5;
0:數(shù)據(jù)開始的位置
5:數(shù)據(jù)的長度
第一種:使用Mybatis
1接口
ListgetUserByLimit(Map map);
2mapeer.xml
2-1結(jié)果集映射
3測試
@Test public void getUserByLimitTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession (); UserMapper mapper = sqlSession.getMapper (UserMapper.class); HashMap hashMap = new HashMap(); hashMap.put ("starIndex", 1); hashMap.put ("pageSize", 2); List userByLimit = mapper.getUserByLimit (hashMap); for (Object o : userByLimit) { System.out.println (o); } sqlSession.close (); }
第二種:使用RowBounds方法
1.接口
List getUserList();
2.實(shí)現(xiàn)接口
3.測試:
/** * 測試使用RowBounds實(shí)現(xiàn)分頁 */@Test public void getUserByLimitRowBoundsTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession (); RowBounds rowBounds = new RowBounds (0, 2); ListuserList = sqlSession.selectList ("com.kuang.w.dao.UserMapper.getUserList", null, rowBounds); for (User user : userList) { System.out.println (user); } //關(guān)閉 sqlSession.close (); }
第三種:使用Mybatis的分頁插件 pageHeIper
sql 多對一處理
數(shù)據(jù)庫 :
pojo
數(shù)據(jù)庫中teacher-table表 對應(yīng)實(shí)體類 Teacher
package com.kuang.w.pojo; import lombok.Data; /** * @author W */ @Data public class Teacher { private int tId; private String tName; }
數(shù)據(jù)庫中user表 對應(yīng) 實(shí)體類Student
package com.kuang.w.pojo;import lombok.Data;/** * @author W */@Datapublic class Student { private int id; private int tid; private String name; private String password; private Teacher teacher;}
1.接口
ListgetStudentList();
2.xml配置實(shí)現(xiàn)接口
mybatis-config.xm配置
3 測試
@Test public void getStudentListTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession (); StudentMapper mapper = sqlSession.getMapper (StudentMapper.class); ListstudentList = mapper.getStudentList (); for (Student student : studentList) { System.out.println (student); } sqlSession.commit (); sqlSession.close (); }
sql 一對多處理
數(shù)據(jù)表結(jié)構(gòu) 對應(yīng)的實(shí)體類 不變
第一種方式: 多表聯(lián)查
1接口
ListgetTeacher(int tid);
2.1 xml實(shí)現(xiàn)接口
2.2映射配置
3測試
/*測試一對多*/ @Test public void getTeacherTest2() { SqlSession sqlSession = MyBatisUtils.getSqlSession (); TeacherMapper mapper = sqlSession.getMapper (TeacherMapper.class); Listteacher = mapper.getTeacher (1); for (Teacher teacher1 : teacher) { System.out.println (teacher1); } //提交事務(wù) 架子 這里可以不要 sqlSession.commit (); // 關(guān)閉 sqlSession.close (); }
結(jié)果
com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.kuang.w.dao.myTest,getTeacherTest2 Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.Opening JDBC Connection Created connection 164974746.Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]==> Preparing: select t.tid, t.tname, u.id, u.name, u.password from mybatis.user u, mybatis.teacher_table t where t.tid = u.tid and t.tid = ?; ==> Parameters: 1(Integer)<== Columns: tid, tname, id, name, password<== Row: 1, 狂神, 1, 天王蓋地虎, 111<== Row: 1, 狂神, 2, 小波, 123<== Row: 1, 狂神, 3, 雷神, 922<== Row: 1, 狂神, 5, 馬兒扎哈, 123<== Total: 4Teacher(tId=1, tName=狂神, students=[Student(id=1, tid=1, name=天王蓋地虎, password=111), Student(id=2, tid=1, name=小波, password=123), Student(id=3, tid=1, name=雷神, password=922), Student(id=5, tid=1, name=馬兒扎哈, password=123)])Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]Returned connection 164974746 to pool.Process finished with exit code 0
第二種方式: 子查詢
1接口
ListgetTeacher(int tid);
2 實(shí)現(xiàn)接口
3測試 同上
以上是“Mybatis的基礎(chǔ)知識點(diǎn)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!