当我们执行查询的时候,查询的结果会同时存入到SqlSession为我们提供的一块区域中。
该区域的结构是一个Map。当我们再次查询同样的数据,mybatis会先去查询SqlSession中是否有,有的话直接拿来用。
当SqlSession对象消失时,mybatis的一级缓存也就消失了。
二级缓存的使用步骤:
其实mybatis中默认就是一级缓存了(平时的测试类就是一级缓存存在SqlSession中)
一级缓存是SqlSession范围的缓存,当调用SqlSession的修改,添加,删除,commit(),close()等方法时,就会清空一级缓存。
1.SqlMaoConfig.xml中
<settings>
<setting name="cacheEnabled" value="true"/>
settings>
2.在需要使用二级缓存的实体类的mapper中
<cache/>
<select id="findById" parameterType="INT" resultType="user" useCache="true">
select * from user where id = #{uid}
select>
mybatis中的延迟加载
什么是延迟加载?
什么是立即加载?
采用策略
如何开启?
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="true"/>
settings>
public interface IAccountDao {
/**
* 查询所有账户,并且获取每个账户所属的用户信息
* @return
*/
@Select("select * from account")
@Results(id="accountMap",value = {
@Result(id=true,column = "id",property = "id"),
@Result(column = "uid",property = "uid"),
@Result(column = "money",property = "money"),
//这个注解是引入主表 FetchType(加载时机) EAGER(立即加载)
@Result(property = "user",column = "uid",one=@One(select="com.ajacker.dao.IUserDao.findById",fetchType= FetchType.EAGER))
})
List<Account> findAll();
/**
* 根据用户id查询账户信息
* @param userId
* @return
*/
@Select("select * from account where uid = #{userId}")
List<Account> findAccountByUid(Integer userId);
}
@One
注解将colomn
的参数传递给指定方法,以返回的实体类装填该属性
public interface IUserDao {
/**
* 查询所有用户
* @return
*/
@Select("select * from user")
@Results(id="userMap",value={
@Result(id=true,column = "id",property = "userId"),
@Result(column = "username",property = "userName"),
@Result(column = "address",property = "userAddress"),
@Result(column = "sex",property = "userSex"),
@Result(column = "birthday",property = "userBirthday"),
@Result(property = "accounts",column = "id",
many = @Many(select = "com.itheima.dao.IAccountDao.findAccountByUid",
fetchType = FetchType.LAZY))
})
List<User> findAll();
/**
* 根据id查询用户
* @param userId
* @return
*/
@Select("select * from user where id=#{id} ")
@ResultMap("userMap")
User findById(Integer userId);
}
@Many
注解将colomn
的参数传递给指定方法,以返回的实体类集合装填该属性
哪个Dao接口需要就写在哪儿
@CacheNamespace(blocking = true)
第一种:将xml配置文件居中的语句以标签包裹写在注解内,例如:
@Select("")
public List<DemandComment> listDemandComment(@Param("demandId") Long demandId, @Param("startNo") Integer pageNo, @Param("pageSize") Integer pageSize);
第二种:使用@SelectProvider
提供sql
例如:
mapper:
public interface StudentMapper {
@SelectProvider(type = StudentSqlProvider.class, method="select")
List<Student> findAll(Student student) throws Exception;
@InsertProvider(type = StudentSqlProvider.class , method = "insertStudent" )
void insert(Student student) throws Exception;
@DeleteProvider(type = StudentSqlProvider.class, method = "delete")
void delete(Student student) throws Exception;
@UpdateProvider(type = StudentSqlProvider.class, method = "update")
void update(Student student) throws Exception;
}
Provider:
/**
* Created with IDEA
* author:bigStone
* Date:2019/5/2
**/
public class StudentSqlProvider {
//插入
public String insertStudent(Student student) {
SQL sql = new SQL();
sql.INSERT_INTO("student");
if (student.getId() != null) {
sql.VALUES("id", "#{id}");
}
if (student.getUsername() != null) {
sql.VALUES("username", "#{username}");
}
if (student.getPassword() != null) {
sql.VALUES("password", "#{password}");
}
if (student.getAddr() != null) {
sql.VALUES("addr", "#{addr}");
}
return sql.toString();
}
//查询
public String select(Student student) {
return new SQL() {{
SELECT("id, username, password, addr");
FROM("student");
if (student != null) {
if (student.getId() != null) {
WHERE("id = #{id}");
}
if (student.getUsername() != null) {
WHERE("username like '%' || #{username} || '%'");
}
if (student.getPassword() != null) {
WHERE("password = #{password}");
}
if (student.getAddr() != null) {
WHERE("addr like '%' || #{addr} || '%' ");
}
}
}}.toString();
}
//删除
public String delete(Student student) {
return new SQL() {{
DELETE_FROM("student");
if (student.getId() != null) {
WHERE("id = #{id}");
}
}}.toString();
}
//更新
public String update(Student student) {
return new SQL() {{
UPDATE("student");
if (student.getUsername() != null) {
SET("username = #{username}");
}
if (student.getPassword() != null) {
SET("password = #{password}");
}
if (student.getAddr() != null) {
SET("addr = #{addr}");
}
WHERE("id = #{id}");
}}.toString();
}
}