本文共 7270 字,大约阅读时间需要 24 分钟。
再次学习Mybatis,日后,有时间会把这个文档更新,改的越来越好,然后,改成新手老手通用的文档
Mybatis 是一个持久层框架,(之前 我虽然学了这个mybatis但一直 没有深入的学习,只是达到会用的程度,没有写过什么笔记,后来转jpa+hibernate和tk.mybatis),用jpa用久了之后,需要用到mybatis时候才来用的mybatis
解耦
下面是maven坐标
org.mybatis mybatis 3.4.6
官网其实很详细:
不过我要做的是更简单化的复习mybatis框架
package com.pipihao.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.io.Serializable;import java.util.Date;@Data@NoArgsConstructor@AllArgsConstructorpublic class Blog implements Serializable { private String id; private String title; private String author; private Date createTime; private int view;}
首先是mybatis-config.xml配置文件,这个文件主要是用的配置mybatis的一些操作,例如:扫描一个xxx.xxx.mapper的包
首先我们得写一个XxxMapper类,下面我以BlogMapper为例
package com.pipihao.mapper;import com.pipihao.pojo.Blog;import org.apache.ibatis.annotations.Param;import java.util.List;import java.util.Map;public interface BlogMapper { Blog findBlogById(int id);}
在这个类目录的下面要配置一个BlogMapper.xml
其实我个人觉得这个配置文件,的太多细节在这个官方文档内,而我目前主要是记录下一些 常用的
configuration(配置)
如果你的字段名和mysql的字段名都是规范的对应命名
例如: JavaBean的命名createTime ,mysql的字段命名create_time
这样则大多数不需要配置映射,只需要开在mybatis-config.xml中的setting中开启一个值即可
希望你是使用的规范命名,不然你的代码会显得很low
mybatis提供了一个rowBunds分页,不过仔细看了之后,发现还不如使用mysql的分页,缺点是增加了代码量,然后该做还是要做,并没有省什么操作(这里我建议,要么使用PageHelper,要么自己做原生的分页查询)
原生分页
RowBunds
RowBounds rowBounds = new RowBounds(0,2);ListrolesByrowBounds = mapper.findBlogById(rowBounds);System.out.println(rolesByrowBounds.size());
主要是注解开发也可以达到跟xml配置一样的 效果,所以在以后 的开发之中,大多数使用的是注解开发,但不过,xml,永不过时
@Select @Insert @Update @Delete
public interface StudentMapper { @Select("select * from student where id = #{sid}") Students getStudentById(@Param("sid")int sid);}
这个再提一下@Results 注解,这个是可以在注解实现和resultMap一样功能的注解,也可以设置id,可以复用
下面这个 是复制网上,大概知道这个有这个东西就可以了
@Results(id="groupWithUsers", value = { @Result(property = "groupId", column = "group_id", id = true), @Result(property = "name", column = "name"), @Result(property = "accountId", column = "account_id"), @Result(property = "deleteFlag", column = "delete_Flag"), @Result(property = "parentId", column = "parent_Id"), @Result(property = "userList", javaType=List.class, many =@Many(select="selectUsersByGroupId"), column = "group_id")})//查询@Select({"select * from group where account_id=#{accountId} and delete_flag=0"})ListselectGroupWithUsers(@Param("accountId") String accountId);
主要还是看怎么用
mapUnderscoreToCamelCase 是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。 true | false False
choose我没有设置了,因为 choose是类似于一个switch case一次只会选择一个条件语句进行执行
动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合就可以了。
insert into blog (id,title,author,create_time,view) values ( #{id},#{title},#{author},#{createTime},#{view} ); and title = #{title} and author= #{author} update blog where id = #{id} title=#{title} title=#{author}
,choose,when,otherwise 这个类似switch case一次只会执行一个条件,本命的不执行
trim, 可以 给SQL语句加上前缀和后缀,然后,也可以去除某些前缀或后缀
foreach,可以遍历拼接sql
ListfindBlogByList(@Param("ids")List ids);
include,sql 增加mybatis的xml配置的复用性
and title = #{title} and author= #{author}
where,if 和上面的代码意义相同,只是上面的代码,在业务复杂的情况下,耦合度更低
建议:
这个在上面的配置文件配置了
默认开启的
小结:一极缓存黑夜是开启的,只在一次SqlSession中有效,也就是拿到连接到关闭连接这个区间段!一级缓存就相当于一个Map
设置名 | 描述 | 有效值 | 默认值 |
---|---|---|---|
cacheEnabled | 全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。 | true | false | true |
小结:
转载地址:http://wfkfz.baihongyu.com/