久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Mybatis的基礎知識點

155次閱讀
沒有評論

共計 8743 個字符,預計需要花費 22 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

丸趣 TV 小編給大家分享一下 Mybatis 的基礎知識點,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

mybatis

mybatis-config.xml 詳細配置 (配置時要把多余的屬性刪除 不能有中文 否則報錯!)

?xml version= 1.0  encoding= UTF-8  ? !DOCTYPE configuration
 PUBLIC  -//mybatis.org//DTD Config 3.0//EN 
  http://mybatis.org/dtd/mybatis-3-config.dtd !--configuration 核心配置   配置文件的根元素  -- configuration 
  !--  屬性:定義配置外在化  -- 
  properties /properties 
  !--  設置:定義 mybatis 的一些全局性設置  -- 
  settings 
  !--  具體的參數名和參數值  -- 
  setting name=  value= / 
  /settings 
  !--  類型名稱:為一些類定義別名  -- 
  typeAliases 
  !--  實體類少   建議   第一種取別名方式 -- 
  typeAlias type= 包路徑  alias= 別名 /typeAlias 
  !-- 實體類多   建議   第二種取別名方式
  默認情況下用這種方式   別名為類名   首字母最好小寫
 -- 
  package name= 包名 / 
  /typeAliases 
  !--  類型處理器:定義 Java 類型與數據庫中的數據類型之間的轉換關系  -- 
  typeHandlers /typeHandlers 
  !--  對象工廠  -- 
  objectFactory type= /objectFactory 
  !--  插件:mybatis 的插件, 插件可以修改 mybatis 的內部運行規則  -- 
  plugins 
  plugin interceptor= /plugin 
  /plugins 
  !--  環境:配置 mybatis 的環境  -- 
  environments default= development 
  !--  環境變量:可以配置多個環境變量,比如使用多數據源時,就需要配置多個環境變量  -- 
  environment id= development 
  !--  事務管理器  -- 
  transactionManager type= JDBC / 
  !--  數據源   配置連接我的數據庫 -- 
  dataSource type= POOLED 
  property name= driver  value= com.mysql.jdbc.Driver / 
  property name= url 
 value= jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8 amp;useSSL=true amp;useUnicode=true amp;characterEncoding=UTF-8 / 
  property name= password  value= 123 / 
  property name= username  value= root / 
  /dataSource 
  /environment 
  /environments 
  !--  數據庫廠商標識  -- 
  databaseIdProvider type= /databaseIdProvider 
  !--  映射器:指定映射文件或者映射類  -- 
  mappers 
  mapper resource= com/kang/w/dao/impl/UserMapper.xml /mapper 
  /mappers /configuration

分頁

減少數據訪問量
limt 實現分頁
sql 語句: select * from 表名 limt 0,5;

0: 數據開始的位置

5: 數據的長度

第一種: 使用 Mybatis
1 接口

 List User  getUserByLimit(Map String, Object  map);

2mapeer.xml

  select id= getUserByLimit  parameterType= map  resultType= user 
 select *
 from mybatis.user
 limit ${starIndex},${pageSize}  /select

2- 1 結果集映射

resultMap id= map  type= User 
  result property= pwd  column= password /result 
  /resultMap

3 測試

 @Test
 public void getUserByLimitTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession ();
 UserMapper mapper = sqlSession.getMapper (UserMapper.class);
 HashMap hashMap = new HashMap String, Object  ();
 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. 實現接口

select id= getUserList  resultType= user 
 select *
 from mybatis.user  /select

3. 測試:

 /**
 *  測試使用 RowBounds 實現分頁
 */@Test
 public void getUserByLimitRowBoundsTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession ();
 RowBounds rowBounds = new RowBounds (0, 2);
 List User  userList = sqlSession.selectList (com.kuang.w.dao.UserMapper.getUserList , null, rowBounds);
 for (User user : userList) { System.out.println (user);
 }
 // 關閉
 sqlSession.close ();
 }

第三種:使用 Mybatis 的分頁插件 pageHeIper
Mybatis 的基礎知識點

sql 多對一處理

數據庫:Mybatis 的基礎知識點
pojo
數據庫中 teacher-table 表 對應實體類 Teacher

package com.kuang.w.pojo;
import lombok.Data;
 * @author W
 */
@Data
public class Teacher {
 private int tId;
 private String tName;
}

數據庫中 user 表 對應 實體類 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. 接口

 List Student  getStudentList();

2.xml 配置實現接口

  !--  多對一查詢
 1  子查詢  mysql  通過一個表里是數據   與另一個表的一個數據相的情況下   查詢另一個的數據   一起顯示
 -- 
  select id= getStudentList  resultMap= studentTeacher 
 select *
 from mybatis.user;  /select 
  resultMap id= studentTeacher  type= Student 
  !--  復雜屬性   對象用  :association  集合用:collection-- 
  !--column  數據庫中的字段  property  實體類中的屬性 -- 
  result property= id  column= id / 
  result property= name  column= name / 
  result property= password  column= password / 
  !--javaType 一個  Java  類的全限定名
 ,或一個類型別名(關于內置的類型別名,可以參考上面的表格)。  如果你映射到一個  JavaBean,MyBatis  通常可以推斷類型。  然而,如果你映射到的是  HashMap,  那么你應該明確地指定  javaType  來保證行為與期望的相一致。-- 
  association property= teacher  column= tid  javaType= Teacher  select= getTeacher /association 
  /resultMap 
  select id= getTeacher  resultType= Teacher 
 select *
 from mybatis.teacher_table
 where tid = #{id};  /select
  !--2  多表聯查 -- 
  select id= getStudentList  resultMap= StudentList 
 select u.id uid,
 u.name uname,
 u.password upassword,
 u.tid utid,
 t.tname
 from mybatis.user u,
 mybatis.teacher_table t
 where t.tid = u.tid;  /select 
  !--  映射 -- 
  resultMap id= StudentList  type= Student 
  result column= uid  property= id / 
  result column= utid  property= tid / 
  result column= uname  property= name / 
  result column= upassword  property= password / 
  association property= teacher  javaType= Teacher 
  result property= tName  column= tname /result 
  /association 
  /resultMap

mybatis-config.xm 配置

?xml version= 1.0  encoding= UTF8  ? !DOCTYPE configuration
 PUBLIC  -//mybatis.org//DTD Config 3.0//EN 
  http://mybatis.org/dtd/mybatis-3-config.dtd configuration 
  properties resource= db.properties / 
  settings 
  setting name= logImpl  value= STDOUT_LOGGING / 
  /settings 
  typeAliases 
  typeAlias type= com.kuang.w.pojo.Teacher  alias= teacher / 
  typeAlias type= com.kuang.w.pojo.Student  alias= student / 
  /typeAliases 
  environments default= development 
  environment id= development 
  transactionManager type= JDBC / 
  dataSource type= POOLED 
  property name= driver  value= ${driver} / 
  property name= url  value= ${url} / 
  property name= password  value= ${password} / 
  property name= username  value= ${username} / 
  /dataSource 
  /environment 
  /environments 
  mappers 
  !--  mapper resource= com/kuang/w/dao/TeacherMapper.xml /mapper 
  mapper resource= com/kuang/w/dao/StudentMapper.xml /mapper -- 
  mapper  >

3 測試

 @Test
 public void getStudentListTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession ();
 StudentMapper mapper = sqlSession.getMapper (StudentMapper.class);

 List Student  studentList = mapper.getStudentList ();  for (Student student : studentList) { System.out.println (student);  }  sqlSession.commit ();  sqlSession.close ();  }

sql 一對多處理

數據表結構 對應的實體類 不變

第一種方式:多表聯查
1 接口

 List Teacher  getTeacher(int tid);

2.1 xml 實現接口

  select id= getTeacher  resultMap= TeacherStudent 
 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 = #{tid};  /select

2.2 映射配置

resultMap id= TeacherStudent  type= Teacher 
  result property= tName  column= tname / 
  result property= tId  column= tid / 
  !--  復雜屬性   對象用  :association  集合用:collection-- 
  collection property= students  ofType= Student 
  !--javaType  指定屬性類型   一個  Java  類的全限定名 -- 
  result column= id  property= id /result 
  result column= name  property= name /result 
  result column= password  property= password /result 
  result column= tid  property= tid /result 
  /collection 
  /resultMap

3 測試

 /* 測試一對多 */
 @Test
 public void getTeacherTest2() { SqlSession sqlSession = MyBatisUtils.getSqlSession ();
 TeacherMapper mapper = sqlSession.getMapper (TeacherMapper.class);
 List Teacher  teacher = mapper.getTeacher (1);
 for (Teacher teacher1 : teacher) { System.out.println (teacher1);
 }
 // 提交事務   架子   這里可以不要
 sqlSession.commit ();
 //  關閉
 sqlSession.close ();
 }

結果

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 接口

 List Teacher  getTeacher(int tid);

2 實現接口

  !-- 第二種方式:  子查詢 -- 
  select id= getTeacher3  resultMap= TeacherStudent3 
 select *
 from mybatis.teacher_table
 where tid = #{tid};  /select 
  resultMap id= TeacherStudent3  type= Teacher 
  !--  復雜屬性   對象用  :association  集合用:collection
  我們需要單獨處理對象: association  集合: collection
 javaType= 指定屬性的類型!
  集合中的泛型信息,我們使用 ofType  獲取
 -- 
  result column= tid  property= tId / 
  result column= tname  property= tName / 
  collection property= students  javaType= ArrayList  ofType= Student  select= getStudentByTeacherId 
 column= tid 
  /collection 
  /resultMap 
  select id= getStudentByTeacherId  resultType= Student 
 select *
 from mybatis.user
 where tid = #{tid};  /select

3 測試 同上

以上是“Mybatis 的基礎知識點”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!

向 AI 問一下細節

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-04發表,共計8743字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 平武县| 湘阴县| 瓮安县| 德阳市| 乌鲁木齐市| 徐水县| 游戏| 皋兰县| 湟中县| 邵阳县| 云霄县| 历史| 临泉县| 水富县| 尚志市| 万源市| 惠东县| 云和县| 察雅县| 化德县| 连城县| 泾源县| 迁安市| 荆州市| 志丹县| 西和县| 昭平县| 思南县| 彰化市| 和平区| 依兰县| 和林格尔县| 和硕县| 陆良县| 鱼台县| 荆州市| 孝义市| 龙陵县| 吴江市| 吴桥县| 兴仁县|