API:
Resources:將核心配置文件加載到流中;
SqlsessionFactoryBuilsder:用于構造SqlsessionFactory
SqlSessionFactory:構建SqlSeeion對象
接口代理方法實現對Dao層的開發
1.映射文件和dao層下的接口名一致 編譯后在同一個文件夾中
2.映射文件的namespace的名字與接口全限定名一致
3.接口定義的方法名參數類型返回值類型與xml中定義的一致
Mapper mapper = SqlSeesion.getMapper();
mapper.select()
ResultMap:可以解決數據庫的屬性和實體類對應不上的問題。

子標簽中id時表中的主鍵id 其他列都是用result column----property
Mybatis中多參數傳遞:
1.索引 where uid = #{0}----0占位符 低版本多參數uid=#{arg0} and uname=#{arg1}
2.map集合:uid=#{key1} and uname=#{key2} key1.key2對應map中鍵
3.注解:( @param(value=" 11 ") String uname) uname=#{11} sql語句{}中的值必 須與注解中的value值一樣
4.屬性名 傳入對象 uid=#{屬性名} and uname=#{屬性名}
//順序傳值
select * from user where uid = #{0} and uname =#{1}
//通過注解 注解設置的value值與sql相對應
select(@parm(value ="uid") int uid ,@parm(value ="uname") String uname )
select * from user where uid =#{uid} and uname=#{uname}
//通過map集合
Map map = new HashMap();
map.put("key1",1)
map.put("key2","李四")
select * from user where uid={key1} and uname={key2}
//通過對象
User user =new User();
User.setId(1)
User,setName("李四")
select * from user where uid={uid} and uname={uname}
模糊查詢:
占位符: 1. select * from user where uname like #{uname}
測試selectByLike("%"+"王"+"%")
2. select * from user where uname like '%${value}%'(適用范圍mysql sqlserver)
測試selectByLike("王") 不使用value接口頂頂方法時需要使用注解方式傳值
3. select * from user where uname like concat(’%‘,#{uname},’%‘)
字符串拼接:4."%"#{uname}"%" 會有sql注入問題
${}和#{}的區別:
#{}表示的是占位符, 相對安全 里面的名字隨意
可以實現preparedStatement向占位符賦值 自動進行java類型轉換和jdbc類型轉換 可以防止sql注入
可以接收簡單類型或pojo類型 如果paramenterType傳遞單個簡單類型{}里可以實value或者其他名稱
${}表示的是拼接sql串, 變量名不能隨意給 否則必須給注解
Mybatis中主鍵的使用:

sql片段:
對公共的sql代碼進行抽取

分頁:
使用插件進行分頁開發
引入jar包:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.3</version>
</dependency>
核心xml文件引入插件:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
編寫代碼:
//設置當前頁和頁量
PageHelper.startPage(3,3);
//實例化PageInfo對象傳入數據庫中的所有的數據集合
List<User> users = userMapper.selectAll(); PageInfo<User> pageInfo =new PageInfo<>(users);
//通過get方法可以獲取分頁相關的屬性值
System.out.println(pageInfo.getPageNum());
System.out.println(pageInfo.getPages());
System.out.println(pageInfo.getSize());
System.out.println(pageInfo.getTotal());
System.out.println(pageInfo.isIsFirstPage());
System.out.println(pageInfo.isIsLastPage());
System.out.println(pageInfo.getList());
表與表之間的關系一對一:一對多:
多對多:
嵌套查詢:
把一對一 或者一對多的查詢語句 分成兩個語句來分別查詢
1.一對一:
<trim>標簽
<trim prefix="前綴" suffixOverrides="去除后綴"></trim>
<foreach ></foreach>遍歷
1.遍歷集合
collection:遍歷的對象 list array
open:開始
item:每次遍歷的數據
close:以..結束
separtor:以..分割
close:以..結束
使用注解進行開發:
浙公網安備 33010602011771號