1 . 基于dao层接口和mapper文件(sql语句)完成开发。
2. 加上mybatis的配置可以访问数据库以及ssm之间的整合,基于注解的开发。
3. 注解绑定sql和xml绑定Sql 。
输入映射和输出映射:
Mapper.xml映射文件中定义了操作数据库的sql,映射文件是mybatis的核心。
Mybatis使用ognl表达式解析对象字段的值,#{}(可以防止sql注入)或者${}。 括号中的值为pojo属性名称。
parameterType(输入类型):
- 简单类型
- pojo对象
- pojo包装对象:开发中通过pojo传递查询条件 ,查询条件是综合的查询条件。
Mapper文件:
<!-- 使用包装类型查询用户 使用ognl从对象中取属性值,如果是包装对象可以使用.操作符来取内容部的属性 --> <select id="findUserByQueryVo" parameterType="queryvo" resultType="user"> SELECT * FROM user where username like '%${user.username}%' </select>
括号中的值为pojo属性名称。属性也可以是一个包装类或者对象。
resultType(输出类型):
- 输出pojo对象
- 输出pojo列表
- resultMap:resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。如 果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 。
- 简单类型
resultMap实现将查询结果映射为复杂类型的pojo:
- 用resultMap,如果字段和属性一致也是要写的。
- 返回配置是resultmap : 必须配置resultmap才可以正确返回;
- 返回配置是resultType : 返回Bean的全名,自动封装到实体对象中。
在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
<!-- resultMap入门 --> <!-- resultMap定义 type:返回结果映射的pojo,可以使用别名 --> <resultMap type="orders" id="order_list_result_map"> <!-- id主键的映射, property时候pojo中主键的属性,column:返回结果中主键的列--> <id property="id" column="id"/> <!-- 普通列使用result映射 --> <result property="userId" column="user_id"/> <result property="number" column="number"/> <result property="createtime" column="createtime"/> <result property="note" column="note"/> </resultMap> <select id="getOrderListResultMap" resultMap="order_list_result_map"> select id,user_id,number,createtime,note from orders </select>
public interface OrderMapper { List<Orders> getOrderListResultMap(); }
动态sql
动态sql-if
<select id="findUserList" parameterType="user" resultType="user">
select * from user
where 1=1
<if test="id!=null">
and id=#{id}
</if>
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
</select>
动态sql-where、foreach
where标签的作用: <where />可以自动处理第一个and。
<!-- 动态sql foreach测试 -->
<select id="findUserByIds" parameterType="queryvo" resultType="user">
SELECT
*
FROM `user`
<where>
<!-- and id in(1,10,20,21,31) -->
<foreach collection="ids" item="id" open="and id in(" close=")" separator=",">
#{id}
</foreach>
</where>
</select>
SELECT * FROM `user`
<where>
<if test="id!=null">
and id=#{id}
</if>
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
</where>
如果传入 id:6 , name:zhangsan
拼接后: SELECT * FROM `user` where id=6 and username like '%zhangsan%'
动态sql-sql片段: 复用sql代码:
<sql id="find_user_list_where">
<if test="id!=null">
and id=#{id}
</if>
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
</sql>
<select id="findUserList" parameterType="user" resultType="user">
select <include refid="user_field_list"/> from user
<where>
<include refid="find_user_list_where"/>
</where>
</select>
*********
Mybatis的Xml映射文件中,不同的Xml映射文件,id是否可以重复?namespace和id确定的
在mapper.xml映射文件中,dao接口的方法映射到xml中的sql-----是根据namespace名称空间和id确定的。
namespace就是接口的全类名找到这个接口,id就是方法的名字找到这个方法。
redis缓存和mybatis的缓存有啥不一样?什么时候应该用那种?
mybatis一级缓存默认开启,二级缓存实际开发不使用,缓存会造成脏数据。
mybatis的缓存:
分为一级缓存和二级缓存,一级缓存的作用范围为session,所以当session commit或close后,缓存就会被清空 ,
二级缓存的作用范围为sqlsessionfactory,映射语句文件中的所有select语句都会被缓存,所有CRUD的操作都会刷新缓存,缓存会存储1024个对象,缓存容易造成脏毒数据,影响真实数据的准确性,实际开发业务中会放弃二级缓存。
redis的缓存:
可控制的后端缓存服务,通常用来缓存后端数据,当程序第二次访问数据库的时候,命中redis,大大减少数据库的负担,减少访问数据库的链接时间,实际开发过程中都会采用这种缓存方式,达到访问速度和效率的解决方案。
使用二级缓存 还是使用redis取决你的业务需求 ,如果你只insert , select ,那么二级缓存是个不错的选择,毕竟粗犷的缓存作用域比使用redis要少不少代码,但是秒杀绝对不适合二级缓存。
mybatis的常用注解说明:
注解 | 功能 |
---|---|
@Insert | 新增 |
@Update | 更新 |
@Delete | 删除 |
@Result | 结果集封装 |
@Results | 可以与@Result一期使用,封装多个结果集 |
@ResultMap | 实现引用@Results定义的封装 |
@One | 实现一对一结果集封装 |
@Many | 实现一对多结果集封装 |
@SelectProvider | 实现动态SQL映射 |
@CacheNamespace | 实现注解二级缓存的使用 |
public interface PersonMapper {
@Select("SELECT * FROM persons WHERE id = #{id}")
Person selectPerson(int id);
@Select("SELECT * FROM persons")
List<Person> selectAllPerson();
@Delete("DELETE FROM persons WHERE id = #{id}")
void deletePerson(int id);
@Update("UPDATE persons SET name = #{name}, age = #{age} where id = #{id}")
void update(Person person);
@Insert("INSERT INTO persons(name, age) VALUES(#{name}, #{age})")
void insert(Person person);
https://blog.csdn.net/x_iya/article/details/72983906
问题:
数据库上锁的问题?报异常
操作数据的事务问题? 多表数据的增删改一定要加事务
数据操作的线程的问题?
MybatisPLUS简化mybatis开发
只做mybatis增强的开发工具,简化开发,提高效率
WARNING
引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。
注意:我们最好是能用#{}则用它,因为它可以防止sql注入,且是预编译的,在需要原样输出时才使用${},