
mybatis
niceYF
这个作者很懒,什么都没留下…
展开
-
Mybatis一级缓存
什么是缓存:存在内存中的临时数据。将用户经常查询的数据放在缓存(内存中),用户去查询数据就不用从磁盘上查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题。为什么使用缓存?减少和数据库的交互次数,减少系统开销,提高系统效率。什么样的数据能使用缓存?经常查询并不经常改变的数据。Mybatis中的缓存:系统默认定义了两级缓存:一级缓存和二级缓存- 默认情况下,只有一级缓存开启,(SqlSession级别的缓存,也称为本地缓存)- 二级缓存需要手动开启和配置,基原创 2022-03-20 18:03:44 · 2833 阅读 · 0 评论 -
Mybatis二级缓存
怎么样开启二级缓存?1.开启全局缓存(虽然默认就是开启的)2.在映射器中加一行它的里面有很多属性可以添加:工作机制:- 一个会话查询一条数据,这个数据就会被放到当前会话的一级缓存中去;- 如果当前会话被关闭了,这个会话对应的一级缓存就没了;但是我们想要的是,一级缓存中的数据被保存到二级缓存中。(死了就遗传给儿子)- 新的会话查询信息,就可以从二级缓存中获取内容- 不同的mapper查出的数据会放到自己对应的缓存中去。例子 @Test pu.原创 2022-03-20 21:07:47 · 216 阅读 · 0 评论 -
mybatis--SQL片段
SQL片段的作用:将重复的SQL语句抽取出来,放到<sql>标签中,可以进行复用。1.抽取重复的SQL语句 <sql id="where-title-author"> <if test="title!=null"> title like concat('%',#{title},'%') </if> <if test="author!=n原创 2022-03-13 00:48:37 · 2476 阅读 · 0 评论 -
Mybatis中choose标签和set标签的使用
choose标签就类似于java中的switch。从多个条件中选择一个进行查找。 <select id="queryBlogIf" resultType="blog" parameterType="map"> select * from mybatis.blog where <choose> <when test="title!=null"> tit原创 2022-03-12 23:54:40 · 1007 阅读 · 0 评论 -
mybatis中<where>标签使用场景
<select id="queryBlogIf" resultType="blog" parameterType="map"> select * from mybatis.blog where <if test="title!=null"> title like concat('%',#{title},'%') </if> <if test="author!=null"...原创 2022-03-09 10:44:47 · 464 阅读 · 0 评论 -
mybatis动态sql模糊查询方法
动态SQL可以省略很多拼接SQL的步骤,使用类似于JSTL方式。方式1 : <select id="queryBlogIf" resultType="blog" parameterType="map"> select * from mybatis.blog where 1 = 1 <if test="title!=null"> and title like #{title} </if>原创 2022-03-09 10:28:29 · 7653 阅读 · 0 评论 -
mybatis中的驼峰转换
明明数据库中是有值的,但是通过代码拿出来之后却显示为null。这是因为数据库字段命名和类中属性命名不一致所导致的。可以采用驼峰转换的方式,解决这个问题。在数据库中的数据一般都是带下划线的而在类中却是驼峰式的public class Blog { private String id; private String title; private String author; private Date createTime; private int原创 2022-03-09 01:12:58 · 3890 阅读 · 0 评论 -
mybatis中多对一查询处理
方式1:按照结果集嵌套就是先把所需要的数据查询出来,再做映射。 <select id="getStudent" resultMap="StudentTeacher"> select s.id sid,s.name sname,t.name tname,s.tid stid from student s,teacher t where s.tid = t.id </select> <result原创 2022-03-05 16:39:13 · 170 阅读 · 0 评论 -
mybatis中#{}和${}的区别
#{}可以很大程度的防止sql注入${}却不可以原创 2022-03-05 11:29:14 · 78 阅读 · 0 评论 -
mybatis中关于注解的一些知识
1.可以设置自动提交在创建工具类的时候可以填入参数设置为自动提交public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResou原创 2022-03-04 18:24:52 · 126 阅读 · 0 评论 -
Mybatis中的生命周期和作用域
生命周期和作用域是至关重要的,错误的使用会导致非常严重的并发问题。SqlSessionFactoryBuilder:一旦使用就会被丢弃,所以最好设置为局部变量SqlSessionFactory:一旦被创建就应该在运行期间一直存在,没有任何理由丢弃它或重新创建一个。所以设置为单例模式或者静态单例模式。SqlSession:每个线程都有它自己的SQLSession实例,SQLSession的实例不是线程安全的。所以最好也设置为局部变量,并且需要记得每次使用完之后进行关闭操作。.原创 2022-02-28 12:29:04 · 150 阅读 · 0 评论 -
Mabatis中mapper映射器的三种主要形式和注意事项
1.使用相对类路径的资源引用<mapper resource="com/yf/dao/UserMapper.xml"></mapper>2.使用映射器接口<mapper class="com.yf.dao.UserMapper"/>3.将包内的所有接口全部注册为映射器<package name="com.yf.dao"/>注意:1.接口和映射器应该同名2.接口和映射器在同一个包下...原创 2022-02-27 23:08:53 · 183 阅读 · 0 评论 -
Mybatis中map的使用
在Mybatis中传递参数除了可以使用实体类对象和基本数据类型外还可以使用map进行参数的传递,而且更为便捷。mapper的代码public interface UserMapper { List<User> getUserList(); int addUser(User user); int updateUser(User user); int addUser2(Map<String,Object> map);}mappe原创 2022-02-27 20:52:32 · 4557 阅读 · 0 评论 -
mybatis使用注解进行开发
1.在接口的上方进行注解例如@Select("select * from user") List<User> getUserList();2.在配置文件中注册接口 <mappers> <mapper class="com.yf.dao.UserMapper"></mapper> </mappers>3.进行测试 public void test1(){ SqlSess原创 2022-03-03 16:39:46 · 234 阅读 · 0 评论 -
mybatis中使用limit进行分页
sql中limit的语法SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset先在接口mapper中定义方法。List<User> getUserByLimit(Map<String,Integer> map);然后到mapper.xml中进行注册 <select id="getUserByLimit" parameterType="map" resultMap="UserMap"&原创 2022-03-03 16:09:42 · 3906 阅读 · 0 评论 -
Mabatis中log4j的简单使用
1.下载log4j的包(maven) <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>2.setting中设置日志为log4j格式原创 2022-03-03 15:26:37 · 80 阅读 · 0 评论