小汤学编程之JavaEE学习day09——Mybatis

在这里插入图片描述

一、Mybatis简介
1.官网     2.Mybatis是什么     3.有何作用     4.扩展:Hibernate
二、Maven介绍
1.导包     2.准备数据库和表、实体类     3.定义dao层接口和接口映射文件     4.编写测试类,获取SqlSession实例,执行已映射语句
三、Mybatis核心配置文件解析
四、Mybatis的CRUD
1.查询     2.新增     3.修改     4.删除     5.$和#的区别
五、Mybatis注解
六、Mybatis日志
七、Lombok
八、动态sql
1.if     2.choose、when、otherwise     3.trim、where、set
九、Mybatis一对多与多对一
1.一对多     2.多对一
十、Mybatis缓存
1.一级缓存     2.二级缓存


一、Mybatis简介

1.官网

https://mybatis.org/mybatis-3/zh/getting-started.html

2.Mybatis是什么

mybatis是一个优秀的持久层框架,也是一个半自动的ORM框架

3.有何作用

将内存中的数据持久化到数据库中,优化JDBC操作。

4.扩展:Hibernate

Hibernate是一个全自动ORM框架(对象关系映射)。


二、使用步骤

(以Maven项目的使用为例)

1.导包

将下面的依赖代码置于 pom.xml 文件中:

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>x.x.x</version>// 版本号
</dependency>
2.准备数据库和表、实体类
3.定义dao层接口和接口映射文件

SQL的映射xml文件示例:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
  <select id="selectBlog" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>
4.编写测试类,获取SqlSession实例,执行已映射的SQL语句
public class Test {
    public static void main(String[] args) throws IOException {
        // 获取输入流对象
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        // 获取会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获取会话
        SqlSession session = sqlSessionFactory.openSession();
        // 获取接口对象
        UserMapper userMapper = session.getMapper(UserMapper.class);
        // 使用接口

        User user = new User();
//        user.setUserName("zh");
        user.setUserSex("女");
        user.setUserAdd("中国");
        List<User> list = userMapper.getDynamic(user);

        for (User u:list) {
            System.out.println(u);
        }

        // 提交会话
        session.commit(); 
    }
}


三、Mybatis核心配置文件解析

配置说明
properties属性,包括一些加载数据库的SQL语句
setting设置,极为重要的调整设置,它们会改变Mybatis的运行时行为
typeAliases类型别名,可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写
typeHandlers类型处理器,用来将从数据库取得的值转换成java类型
objectFactory对象工厂,用来在帮助完成结果对象的实例化
plugins插件,用来配置各种插件,而插件可用来完成各种不能的功能
environments环境配置,用来配置一种或多种环境,但只能使用其中一种
databaseldProvider数据库厂商标识,用来根据不同的数据库厂商执行不同的语句
mapper映射器,指定所使用的SQL映射xml文件


四、Mybatis的CRUD

1.查询
<!-- 查询所有用户数据 -->
<select id="allUser" resultType="com.blb.beans.User">
	select * from s_user
</select>
2.新增
<!-- 添加用户 -->
<insert id="addUser" parameterType="com.blb.beans.User">
	insert into s_user values  (#{id},#{username},#{password});
</insert>
3.修改
<!-- 更新用户数据 -->
<update id="updateUser" parameterType="com.blb.beans.User">
	update s_user set  username=#{username},password=#{password},nickname=#{nickname},sex=#{sex},tel=#{tel},email=#{email},head_img=#{headImg},create_time=#{createTime},create_user=#{createUser},update_time=#{updateTime},update_user=#{updateUser},status=#{status} where id=#{id}
</update>
4.删除
<!-- 删除用户 -->
<delete id="delUser" parameterType="java.lang.String">
	delete from s_user where id=#{deleteId}
</delete>
5.$和#的区别

$是字符串拼接,#是占位符引用,前者可能出现注入漏洞,后者不会。


五、Mybatis注解

注解作用
@Param在参数前使用,可指定参数在sql语句中的的别名
@Select在方法前使用,可直接在该注解里写查询语句
@Insert在方法前使用,可直接在该注解里写插入语句
@Update在方法前使用,可直接在该注解里写修改语句
@Delete在方法前使用,可直接在该注解里写删除语句


六、Mybatis日志

开启步骤:

  • 在Mybatis核心配置文件中指定使用的日志组件
<setting name="logImpl" value="LOG4J"/>
  • maven里导入对应的组件jar包
<!--log4j-->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
  • 在resources目录下创建一个jdbc.properties文件
# 全局日志配置
log4j.rootLogger=ERROR, stdout
# MyBatis 日志配置
log4j.logger.com.blb.mapper=TRACE
# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n


七、Lombok

使用步骤:

  • 在Idea中安装lombok插件
  • Maven里导入依赖包
<!--lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.16</version>
</dependency>
  • 在实体类前加上各种注解
注解说明
@Data自动加上常用方法,包括所有set/get方法、toString方法、无参构造方法等
@AllArgsConstructor自动加上全参构造方法
@NoArgsConstructor自动加上无参构造(一般用来防止有参构造的覆盖)


八、动态Sql

1.if

用来给sql语句动态添加内容。

SELECT * FROM BLOG
  WHERE state = ‘ACTIVE’
  <if test="title != null">
    AND title like #{title}
  </if>

test里的条件可以是多个,用 and 连接即可。

2.choose、when、otherwise

用来从多个内容中选择一种添加到sql。

SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>

类似Java中的 switch 语句。

3.trim、where、set

用来从多个内容中选择任意个添加到sql。

SELECT * FROM BLOG
<where>
    <if test="state != null">
         state = #{state}
    </if>
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
</where>

where 元素只会在子元素返回任何内容的情况下才会插入“where”语句,而且若子句的开头为“AND”或“OR”时,也会将他们消除。

当然你也可以使用 trim 元素来自定义要消除的词,如:

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>
update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
where id=#{id}

set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号。

当然你也可以使用 trim 元素来自定义要消除的符号,如:

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>


九、Mybatis一对多与多对一

1.一对多

使用collection的select属性来实现:

<resultMap id="aaa" type="com.blb.entity.Customer">
    <id property="id" column="id"></id>
    <result property="name" column="name"></result>
    <!-- property对应实体类中的属性,select对应查询的语句, column对应传递的列 -->
    <collection property="orders" select="getOrderByCId" column="id"></collection>
</resultMap>

<select id="getById" resultMap="aaa">
    select * from t_customer where id = #{id}
</select>

    <!--根据客户id查询订单-->
<select id="getOrderByCId" resultType="com.blb.entity.Order">
    select * from t_order where c_id = #{id}
</select>

使用collection的ofType属性来实现:

<resultMap id="aaa" type="com.blb.entity.Customer">
    <id property="id" column="id"></id>
    <result property="name" column="name"></result>
    <collection property="orders" ofType="com.blb.entity.Order">
        <id property="id" column="oid"></id>
        <result property="orderName" column="order_name"></result>
    </collection>
</resultMap>

<select id="getById" resultMap="aaa">
    select c.*, o.id oid, o.order_name from t_customer c, t_order o where c.id = o.c_id and c.id=2
</select>
2.多对一

使用association的select属性来实现:

<resultMap id="orderMap" type="com.blb.entity.Order">
    <id property="id" column="id"></id>
    <result property="orderName" column="order_name"></result>
    <!--多对一-->
    <association property="customer" select="getCustomerByCid" column="c_id">
    </association>
</resultMap>

<select id="getById" resultMap="orderMap">
    select * from t_order where id = #{id}
</select>

<select id="getCustomerByCid" resultType="com.blb.entity.Customer">
    select * from t_customer where id = #{id}
</select>

使用association的JavaType属性来实现:

<resultMap id="orderMap" type="com.blb.entity.Order">
    <id property="id" column="id"></id>
    <result property="orderName" column="order_name"></result>
    <association property="customer" javaType="com.blb.entity.Customer">
        <id property="id" column="c_id"></id>
        <result property="name" column="name"></result>
    </association>
</resultMap>

<select id="getById" resultMap="orderMap">
    select * from t_order o, t_customer c
    where c.id = o.c_id and o.id = #{id}
</select>


十、Mybatis缓存

1.一级缓存

默认开启,SqlSession级别,执行以前执行过的api时,且参数相同的情况下,会直接使用缓存中的数据。如果执行session.commit、session.update、session.clearCache等方法时,会造成一级缓存丢失。

2.二级缓存

默认不开启,全局缓存。
开启步骤:

  1. 在Mybatis核心配置文件中添加开启设置:
<!--开启二级缓存  -->
<settings>    
<setting name="cacheEnabled" value="true"/>
</settings>
  1. 在映射文件中使用来开启二级缓存:
<!-- 开启二级缓存 -->
<!--方式一-->
<cache></cache>
<!--方式二-->
<cache type="org.apache.ibatis.cache.impl.PerpetualCache"></cache>

PerpetualCache这个类是mybatis默认实现缓存功能的类,可以去实现 Cache 接口来自定义缓存。


待补充内容:
  1. Mybatis plus
  2. 自定义连接池
  3. 自己实现一个Mybatis
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值