MyBatis的注解使用、ORM层优化方式(懒加载和缓存)
作者:bamzhy13 时间:2023-04-24 09:16:13
Mybatis注解
查找
@Select( "SELECT * FROM tt_user WHERE username Like #{id};")
User findUserByName(String name);
<!--以map为输入参数查找-->
@Select("select * from tt_user where username= #{name} and password=#{pass}")
User findUserByUsernameAndPassword(HashMap map);
插入
@Insert("insert into tt_user values (#{id},#{username},#{password},#{email},#{age})")
int AddUser(User user) throws IOException;
更改
@Update("update tt_user set username = #{username} where id = #{id}")
void updateUserById(User user);
删除
@Delete("delete from tt_user where id =#{id}")
void deleteUserById(String id);
动态SQL语句
@SelectProvider(type = UserDaoSqlProvider.class,method = "findUserByName")
List<User> findUserByName(String username) throws IOException;
public String findUserByName(String username){
//return "select * from tt_user where username like '" +username+"'";
String sql = new SQL() {{
SELECT("*");
FROM("tt_user");
if (username!=null&&!username.isEmpty()){
WHERE("username like "+username );
}
}}.toString();
System.out.println("sql="+sql);
return sql;
}
Mybatis提供了一个匿名内部类
另一种写法:
public String getxxx(){
return new SQL().UPDATE("PERSON")
.SET("FULL_NAME = #{fullName}", "DATE_OF_BIRTH = #{dateOfBirth}")
.WHERE("ID = #{id}")
.toString();
}
}
Mybatis对ORM层的优化
缓存
查了一次,结果保存在本地,下次查相同内容的时候就不用再去访问数据库了。
懒加载
Lazyloading:先加载主信息,在需要的时候再去加载关联数据
Mybatis中ResultMap中的association和Collection标签具有懒加载的功能
默认情况没有懒加载
如何开启懒加载呢?
在主配置文件中的configuration标签下配置
<!--全局懒加载-->
<settings>
<!--开关-->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
局部懒加载配置
在从配置文件中的association或者collection里边添加fetchType字段(fetch翻译是拿、取)
<resultMap id="studentAndCourses" type="com.bamzhy.bean.Student">
<id column="id" property="id"></id>
<collection property="courses"
javaType="list"
select="com.bamzhy.dao.UserDao.findcourseBySid"
column="id"
fetchType="eager">
<id column="cid" property="cid"></id>
</collection>
</resultMap>
全局懒加载和局部懒加载同时出现时,以局部懒加载优先
MyBatis缓存机制
一级缓存指的就是sqlsession,在sqlsession中有一个数据区域,是map结构,这个区域就是一级缓存区域。
查询同一个对象(使用同一个session)时,第二次查询并不会调用sql语句,而是从sqlsession中取数据。
开启一级缓存:默认开启
验证一级缓存存在
我查了两次,但是只执行了一次sql语句
现在在第一次查询结束后关闭sqlSession,再重新new一个session出来,
执行查询
果然,sql语句被执行了两次,可以证明一级缓存的确存在
二级缓存
二级缓存指的就是同一个namespace下的mapper,二级缓存中,也有一个map结构,这个区域就是二级缓存区域。
开启二级缓存
主配置文件里边:
从配置文件:
注意点 cache对象需要实现序列化接口,否则会报Error serializing object
测试二级缓存
虽然我关闭了sqlsession,但仍然没有执行两次sql。
说明二级缓存的确存在
如果不关闭一级缓存,那么二级缓存就不生效。
比如我创建了一个sqlsession,不关闭,再创建一个sqlsession,那么二级缓存看见第一个sqlsession没有关闭(一级缓存生效了),二级缓存他就不生效了。事实上这俩session不是同一个session,二级缓存也没生效,这样要执行两次sql语句。
来源:https://blog.csdn.net/qq_37366958/article/details/79946193
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Spring Boot 定制HTTP消息转换器
![](https://img.aspxhome.com/file/2023/3/59853_0s.png)
使用Java和ffmpeg把音频和视频合成视频的操作方法
Unity常用命令模式详解
![](https://img.aspxhome.com/file/2023/8/119548_0s.png)
Java使用Socket简单通讯详解
![](https://img.aspxhome.com/file/2023/6/125576_0s.png)
如何获取Android设备挂载的所有存储器
mybatis框架xml下trim中的prefix与suffix等标签的用法
Android项目实现视频播放器
![](https://img.aspxhome.com/file/2023/1/129141_0s.jpg)
详谈.net中的垃圾回收机制
C#使用async和await实现异步编程
![](https://img.aspxhome.com/file/2023/0/108760_0s.png)
DUBBO 日志过滤器,输出dubbo 接口调用入参、出参等信息(最新推荐)
![](https://img.aspxhome.com/file/2023/8/104208_0s.png)
Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件
![](https://img.aspxhome.com/file/2023/9/120089_0s.webp)
Java Springboot如何基于图片生成下载链接
winform用datagridview制作课程表实例
![](https://img.aspxhome.com/file/2023/5/87035_0s.jpg)
Android双向选择控件DoubleSeekBar使用详解
![](https://img.aspxhome.com/file/2023/0/137230_0s.jpg)
Freemarker如何生成树形导航菜单(递归)
.NET Core使用C#扫描并读取图片中的文字
![](https://img.aspxhome.com/file/2023/6/111096_0s.jpg)
Intellij IDEA 2017新特性之Spring Boot相关特征介绍
![](https://img.aspxhome.com/file/2023/0/88140_0s.gif)
Java实现批量向mysql写入数据的方法
Android实现界面内嵌多种卡片视图(ViewPager、RadioGroup)
![](https://img.aspxhome.com/file/2023/5/108235_0s.png)