mybatis笔记

mybatis笔记

一、mybati九步构建

1、创建一个maven项目

  1. 删除掉src包

  2. 在这个项目中新建一个子Moudle,作为实现mybatis的项目根

2、搭建数据库

  1. 创建数据库mybatis

    (file://C:/Users/%E8%88%AA%E8%88%AA/AppData/Roaming/Typora/typora-user-images/1617624622412.png?lastModify=1623658583)]

3、导入Mybatis相关的jar包

父类的pom.xml中

  1. 导入数据库驱动
  2. 导入mybatis驱动
  3. 导入测试驱动
  <!--导入驱动-->
    <dependencies>
        <!--导入数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!--自己的数据库的版本是多少,导多少-->
            <version>8.0.21</version>
        </dependency>
        <!--导入mybatis驱动-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--导入Junit测试驱动-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

	<!--每个项目都必须导-->
    <!--Maven项src/main/java目录下配置文件无法被导出或者生效的问题和处理方案-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

4、编写mybatis核心配置文件

  1. 在IDEA中连接mysql数据库步骤

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.连接成功后,就需要在mybatis中文文档中复制mybatis的核心配置文件到mybatis-config.xml文件中,就是下面的代码
在这里插入图片描述

-  <transactionManager type="JDBC"/>
-  <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8"/>
-  <property name="username" value="root"/>
- <property name="password" value="123456"/>

mybatis-config.xml代码如下:

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis核心配置文件-->
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--每一个Mapper.xml都需要在mybatis核心配置中注册-->
        <mapper resource="com/sh/dao/UserMapper.xml"/>
    </mappers>
</configuration>

5、编写mybatis工具类

在这里插入图片描述

  • MybatisUtils.java工具类
/*
工具类
 */
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            //使用mybatis第一步获取SqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream  = Resources.getResourceAsStream(resource);
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
    我们可以从SqlSessionFactory对象获取sqlSession实例
    SqlSession实例就可以调用增删查改的操作方法
     */
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

6、创建实体类

  • 编写pojo包user实体类

    /*
    数据库的实体类
     */
    public class User {
        private int id;
        private String name;
        private String psw;
    
        public User() {
        }
    
        public User(int id, String name, String psw) {
            this.id = id;
            this.name = name;
            this.psw = psw;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPsw() {
            return psw;
        }
    
        public void setPsw(String psw) {
            this.psw = psw;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", psw='" + psw + '\'' +
                    '}';
        }
    }
    

7、编写Mapper接口

编写dao包中的UserDao接口,UserMapper.xml(这个xml就相当于UserDao接口的实现类UserDaoImpl)

  • UserDao接口
public interface UserDao {    
		List<User> getUserList();
}

8、编写Mapper.xml配置文件写

  • 相当于UserDaoImpl.java
  • UserMapper.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">
	<!--namespace=绑定一个对应的Dao/Mapper接口-->
	<mapper namespace="com.sh.dao.UserDao" >
    <!--select查询语句-->
    <select id="getUserList" resultType="com.sh.pojo.User">
        select * from mybatis.user
    </select>

</mapper>

9、编写测试类

UserDaoTest.java测试类
在这里插入图片描述

/*
测试类
 */
public class UserDaoTest {
    @Test
    public void test(){
        //1、获得sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //2、方法1,推荐
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> userList = userDao.getUserList();

        //3、方法二
        //List<User>  userList= sqlSession.selectList("com.sh.dao.UserDao.getUserList");

        for (User user : userList) {
            System.out.println(user);
        }
        //关闭sqlSession
        sqlSession.close();
    }
}

知识点

  1. 在子项目和父项目的pom.xml都需要导入下面的文件来解决Maven中我们写的配置文件无法生效或者导出的问题

    <!--每个项目都必须导-->
    <!--来解决Maven中我们写的配置文件无法生效或者导出的问题-->
     <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
     </build>
    
  2. 注册UserMapper.xml,每一个Mapper.xml都需要在mybatis核心配置文件中注册

    mybatis-config.xml

        <mappers>
            <!--注册UserMapperMapper.xml-->
            <!--每一个Mapper.xml都需要在mybatis核心配置中注册-->
            <mapper resource="com/sh/dao/UserMapper.xml"/>
        </mappers>
    

二、CRUD

  • CRUD的步骤:在Mappe接口中编写方法--------->在Mapper.xml中选择增删改查的标签,编写sql语句------>在测试类中调用工具类,调用Mapper接口中的方法,实现数据库的增删改查

1、namespace标签

namespace绑定的类名要==我们在Dao包中定义的UserMapper接口

<mapper namespace="com.sh.dao.UserMapper" >

2、select

  • id:绑定的namespace中的方法,就是进行数据库查询的方法
  • resultType:执行sql之后返回的类型
  • parameterType:参数类型,就是各种查询方法中的参数
<!--根据id查询用户-->
    <select id="getUserListById" parameterType="int" resultType="com.sh.pojo.User">
        select * from mybatis.user where id=#{id}
    </select>

3、insert

  • #{}里面的属性,就是User类中的参数,可以直接使用,但是名字必须与User类中的名字一致
	<!--添加一个用户-->
    <!--对象的属性可以直接取出来====就是#{}里面的参数对象,可以直接取-->
    <!--insert into 表(参数) values(值) -->
    <insert id="insertUser" parameterType="com.sh.pojo.User" >
        insert into mybatis.user(id,name,psw) values (#{id},#{name},#{psw})
    </insert>

4、updata

 <!--修改一个用户-->
 <!--updata表set...where...-->
    <update id="updataUser" parameterType="com.sh.pojo.User" >
        update mybatis.user set name=#{name},psw=#{psw} where id=#{id}
    </update>

5、delete

 <!--根据id删除一个用户-->
 <!--delete from表where...-->
    <delete id="deleteUserById" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>

知识点:

  1. 提交事务:数据库的增删改操作都需要提交事务,否则不会成功,需要在测试类中的对应的方法中提交事务

    • 方法eg:sqlSession.commit();

         //添加一个用户
      
        @Test
      public void  addtUser(){
             //1、通过MybatisUtils工具类获取sqlSession对象
             SqlSession sqlSession = MybatisUtils.getSqlSession();
             //2、通过sqlSession对象获取UserMapper(UserDao)接口的对象mapper
             UserMapper mapper = sqlSession.getMapper(UserMapper.class);
             //3、通过UserMapper(UserDao)接口的对象mapper调用接口中的增删查改的方法,实现操作数据库
             int res = mapper.insertUser(new User(4, "小城", "111111111"));
             if(res>0){
                 System.out.println("插入用户成功");
             }
             /*
                4、数据库数据的增删改操作都需要进行,提交事务
                   不提交事务,不能算完成,就是数据库表里的数据没有改变
              */
         	sqlSession.commit();
        		//5、关闭sqlSession对象
         	sqlSession.close();
      }
      
  2. 万能的map

    • Map传递参数,直接在sql中取map对应的key即可(多个参数时,推荐使用)
    • 对象传递参数,直接在sql中去取出对象的属性即可
    • 只有一个基本类型的情况下,可以直接在sql中取出
    1. 演示一个map作为参数的例子
    • Mapper接口中的map作为参数的方法----Map<String,Object> map

      //根据map查询用户
      User getUserListById2(Map<String,Object> map);
      
    • Mapper.xml----parameterType=“map”

          <!--根据map传递参数-->
          <select id="getUserListById2" parameterType="map" resultType="com.sh.pojo.User">
              select  * from user where id=#{helloId}
          </select>
      
    • 测试类中需要给map赋值

       //map传参
          @Test
          public void getUserById2(){
              //1、获取sqlsqssion对象
              SqlSession sqlSession = MybatisUtils.getSqlSession();
              //2、通过反射得到UserMapper(UserDao)的对象
              UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      
              Map<String, Object> map = new HashMap<String, Object>();
              map.put("helloId",2);
              User userListById2 = mapper.getUserListById2(map);
              //4、输出
              System.out.println(userListById2);
              //5、关闭sqlSession
              sqlSession.close();
          }
      

3、模糊查询(查询用户当中所有姓李的)

  • Mapper接口中的模糊查询的方法

    //模糊查询,查询所有姓李的用户
    List<User> getUserListByLike(String name);
    
  • Mapper.xml中select标签: select * from user where name like #{value}

    <!--模糊查询,查询所有姓李的用户-->
        <select id="getUserListByLike" parameterType="com.sh.pojo.User" resultType="com.sh.pojo.User">
            select * from user where  name like #{value}
        </select>
    
  • 测试类中测试:使用通配符 List userList = mapper.getUserListByLike("%李%");

     //模糊查询,查询所有姓李的
        @Test
        public void getUserLike(){
            //1、获得sqlSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            //2、方法1,推荐
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = mapper.getUserListByLike("%李%");
            for (User user : userList) {
                System.out.println(user);
            }
            //关闭sqlSession
            sqlSession.close();
        }
    

三、配置解析

1、核心配置文件(mybatis-config.xml)

  • MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息 。

configuration(配置)

2、环境配置(environments)

  • mybatis可以配置多种环境
  • 不过要记住:尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。
  • 默认使用的环境 ID(比如:default=“development”)。
  • 每个 environment 元素定义的环境 ID(比如:id=“development”)。
  • 事务管理器的配置(比如:type="JDBC(推荐:有提交和回滚)/ MANAGED(没啥用) ")。
  • 数据源的配置(比如:type=“POOLED(推荐:有连接池)/ UNPOOLED(没有池) /JNDI (普通)”)。

3、属性(properties)

  • 注意:xml文件中配置各种标签的顺序
  • 这些属性可以在外部进行配置,并可以进行动态替换。你既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置。(db.properties)
  1. 编写一个配置文件(db.properties)

    driver=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8
    username=root
    password=123456
    

  1. 我们可以使用properties属性来实现properties文件中和properties标签中的数据,如果两个文件中的数据,有冲突,则优先使用外部的文件中的属性
<!--引入db.properties文件-->
    <properties resource="db.properties">
        <property name="name" value="root"/>
        <property name="pwd" value="123456"/>
    </properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--引入properties文件中和properties标签中的数据-->
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>

4、类型别名(typeAliases)

  • 类型别名可为 Java实体类(Bean)设置一个缩写名字。

  • 它仅用于 XML 配置,意在降低冗余的全限定类名书写。

    1、方式一: (实体类少:使用)

<!--在mybatis-config.xml中设置,alias="user1",这里的user1可是是任何名字-->
    <typeAliases>
        <typeAlias type="com.sh.pojo.User" alias="user1"/>
    </typeAliases>
<!--在UserMapper.xml文件中使用设置好的user1-->
<!--通过在mybatis-config.xml设置返回值类型:com.sh.pojo.User为user1-->
    <select id="getUserList" resultType="user1">
        select * from mybatis.user
    </select>

​ 2、方式二: 可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean

​ 1、没有注解:在UserMapper.xml文件中使用时,会使用 Bean 的首字母小写的非限定类名来作为它的别名

<!--在mybatis-config.xml中指定包-->
    <typeAliases>
        <package name="com.sh.pojo"/>
    </typeAliases>
<!--在UserMapper.xml文件中使用小写类名:resultType="user->	
	<select id="getUserList" resultType="user">
        select * from mybatis.user
    </select>

2、有注解: 若有注解,则别名为其注解值 (实体类多:使用

/*
数据库的实体类
 */
@Alias("helloUser")
public class User {}
<!--在mybatis-config.xml中指定包-->
    <typeAliases>
        <package name="com.sh.pojo"/>
    </typeAliases>
<!--在UserMapper.xml文件中使用注解名->	
<select id="getUserList" resultType="helloUser">
        select * from mybatis.user
    </select>
  • mybatis3中文文档中有:常见的 Java 类型内建的类型别名。它们都是不区分大小写的,注意,为了应对原始类型的命名重复,采取了特殊的命名风格 。

5、常见设置(setting)

MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为 (其他的都在mybatis3中文文档中)

设置名描述有效值默认值
mapUnderscoreToCamelCase是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。true | falseFalse
cacheEnabled全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。true | falsetrue
lazyLoadingEnabled延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。true | falsefalse
logImpl指定 MyBatis 所用日志的具体实现,未指定时将自动查找。SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING未设置
logPrefix指定 MyBatis 增加到日志名称的前缀。任何字符串未设置

6、mappers(映射器)

MapperRegister:注册绑定我们的UserMapper.xml(操作数据库的文件)

方式一:使用resource(推荐使用:局限性小,玩不坏)

	<mappers>
       <!--注册UserMapperMapper.xml-->
        <!--每一个Mapper.xml都需要在mybatis核心配置中注册-->
        <mapper resource="com/sh/dao/UserMapper.xml"/>
    </mappers>

方式二:使用class

 	<mappers>
        <!--注册UserMapperMapper.xml-->
        <!--每一个Mapper.xml都需要在mybatis核心配置中注册-->
      	<mapper class="com.sh.dao.UserMapper"/>
    </mappers>

方式三:使用package

 <mappers>
        <!--注册UserMapperMapper.xml-->
        <!--每一个Mapper.xml都需要在mybatis核心配置中注册-->
        <package name="com.sh.dao"/>
    </mappers>
  • 注意:方式二和方式三
    1. UserMapper接口和它的UserMapper.xml配置文件必须同名
    2. UserMapper接口和它的UserMapper.xml配置文件必须在同一个包下

7、生命周期和作用域

不同作用域和生命周期类别是至关重要的,因为错误的使用会导致非常严重的并发问题

在这里插入图片描述

1、SqlSessionFactoryBuilder:

  • 一旦创建了 SqlSessionFactory,就不再需要它了
  • 作用域是方法作用域(也就是局部方法变量)

2、SqlSessionFactory:

  • 说白了可以想象为,一个数据库连接池

  • 一旦被创建就应该在应用的运行期间一直存在,没有任何理由丢弃它或重新创建另一个实例

  • 最佳作用域是应用作用域(全局)

  • 简单的就是使用单例模式或者静态单例模式。

3、SqlSession:

  • 连接数据库的一个请求
  • SqlSession的实例不是线程安全的,因此是不能被共享的
  • 最佳的作用域是请求或方法作用域(方法)
  • 用完之后需要立马关闭

在这里插入图片描述

这里的每一个Mapper,就是一个业务

四、字段名和属性名不一致的问题

  • 数据库中的字段名和java实体类中的属性名不一致的问题

在这里插入图片描述

 	这两句话相等,所以查询到的知识数据库中的psw,并没有查到password,这是类处理器的作用
 	<!--select * from mybatis.user where id=#{id}-->
    <!--select id,name,psw from mybatis.user where id=#{id}-->

解决方法:

1、起别名:
  • select id,name, psw as password from mybatis.user where id=#{id}
	<!--在UserMapper.xml文件中--起别名-->
    <select id="getUserListById" parameterType="int" resultType="user1">
        <!--select * from mybatis.user where id=#{id}-->
        select id,name, psw as password from mybatis.user where id=#{id}
    </select>
2、resultMap(结果集映射)
<!--在UserMapper.xml文件中-->
<!--结果集映射:column="psw"为数据库中的字段  property="password"为javabean中的属性名  -->
<!--id="userMap"与select标签中的id互相映射  type="user1"是类的对象-->
    <resultMap id="userMap" type="user1">
       	<result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="psw" property="password"/>
    </resultMap>
<!--resultMap="userMap"在这里取得resultMap标签中id的映射-->
	<select id="getUserListById" parameterType="int" resultMap="userMap">
        select * from mybatis.user where id=#{id}  
    </select>

注意:

  • resultMap 元素是 MyBatis 中最重要最强大的元素
  • ResultMap的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。
  • ResultMap 的优秀之处——你完全可以不用显式地配置它们**(如果你的数据库字段名和java类的属性名一样可以不用显示配置,只配置不一样的**)
  • 如果这个世界总是这么简单就好了

五、日志

1、日志工厂

如果一个数据库操作,出现了异常,我们需要排错,日志就是最好的帮手!

曾经:sout、debug

现在:日志工厂

设置名描述有效值默认值
logImpl指定 MyBatis 所用日志的具体实现,未指定时将自动查找。SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING未设置
  • SLF4J

  • LOG4J【掌握】标准日志

  • LOG4J2

  • JDK_LOGGING

  • COMMONS_LOGGING

  • STDOUT_LOGGING【掌握】

  • NO_LOGGING

1、 STDOUT_LOGGING

在mybatis-config.xml配置标准日志

<!--标准日志的配置-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

在这里插入图片描述

2、LOG4J

什么是LOG4J?

  • Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件
  • 可以控制每一条日志的输出格式
  • 通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程
  • 通过一个配置文件来灵活地进行配置,而不需要修改应用的代码

1、首先、导入LOG4J包

<!--放在pom.xml-->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

2、log4j.properties(配置文件)

### 设置###
log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

3、Log4J日志的配置

<!--LOG4J在mybatis-config.xml核心配置文件中的配置-->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

4、LOG4J的实现

在这里插入图片描述

LOG4J的使用

1、在需要的使用LOG4J的类中,导入包

2、由于多数方法需要用到日志,所以该变量,为类变量,参数为当前类的class

//import org.apache.log4j.Logger;
     public static Logger logger = Logger.getLogger(UserDaoTest.class);

3、日志级别(由高到低)

 A:off 最高等级,用于关闭所有日志记录。
   	 B:fatal 指出每个严重的错误事件将会导致应用程序的退出。
    	 C:error 指出虽然发生错误事件,但仍然不影响系统的继续运行。
   	 D:warm 表明会出现潜在的错误情形。
    	 E:info 一般和在粗粒度级别上,强调应用程序的运行全程。
   	 F:debug 一般用于细粒度级别上,对调试应用程序非常有帮助。
    	 G:all 最低等级,用于打开所有日志记录。 

eg:日志级别

@Test
    public void testLog4j(){
        logger.info("info:进入了testLog4j");
        logger.debug("debug:进入了testLog4j");
        logger.error("error:进入了testLog4j");
    }

六、分页

思考:为什么要利用分页‘?

  • 减少数据的处理量

使用Limit分页

  • sql格式是:select * from table limit (start-1)*pageSize,pageSize; 其中start是页码,pageSize是每页显示的条数。

eg:

  • 查询第1到第10条数据的sql是: select * from table limit 0,10; - >对应我们的需求就是查询第一页的数据: select * from table limit (1-1)*10,10;
  • 查询第10条到第20条数据的sql是: select * from table limit 10,20; -> 对应我们的需求就是查询第二页的数据: select ★from table limit (2-1)*10,10;
  • 查询第20条到第30条数据的sql是: select * from table limit 20,30; -> 对应我们的需求就是查询第三页的数据: select * from table limit (3-1)*10,10; :

1、使用limit传参实现分页(掌握,底层)

​ 1、接口中的方法

public interface UserMapper {
    //根据分页查询
    List<User> getUserListLimitById(Map<String,Integer> map);

}

​ 2、Mapper.xml,写sql语句 #{startIndex}开始查询的下标,#{pageSize}这页有几个数据

<select id="getUserListLimitById" parameterType="map" resultType="user1">
        select * from  mybatis.user limit #{startIndex},#{pageSize}
    </select>

​ 3、测试

    //根据分页查询用户
    @Test
    public void getUserByIdLimit(){
        //1、通过Myabtis.utils工具类,获得sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2、通过sqlSessio对象,获得Mapper/Dao接口的mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("startIndex",0);
        map.put("pageSize",4);
        //3、通过mapper调用数据库的增删查改的方法
        List<User> userList = mapper.getUserListLimitById(map);
        for (User user : userList) {
            System.out.println(user);
        }
        //4、关闭sqlSession
        sqlSession.close();
    }

2、使用RowBounds实现分页(了解)

​ 1、接口中的方法,不传参

public interface UserMapper {
    //分页查询2
    List<User> getUserListRowBounds();
}

​ 2、Mapper.xml中的方法,SQL语句

<!--分页查询2:使用RowBounds-->
    <select id="getUserListRowBounds" resultType="user1">
        select * from  mybatis.user
    </select>

​ 3、测试

    //分页查询2:使用RowBounds方法
    @Test
    public void getUserListRowBounds(){
        //1、通过使用MybtisUtils工具类,获得sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2、RowBounds的实现
        RowBounds rowBounds = new RowBounds(0,3);
        //3、通过java代码层面实现分页
        List<User> userList = sqlSession.selectList("com.sh.dao.UserMapper.getUserListRowBounds",null,rowBounds);
        for (User user : userList) {
            System.out.println(user);
        }
        //4、关闭sqlSession
        sqlSession.close();
    }

3、分页插件(了解)

在这里插入图片描述

七、使用注解开发

1、使用注解(CRUD)

  • 注解只适合简单的sql语句
  • 如果你需要做一些很复杂的操作,最好用 XML 来映射语句
  • 永远不要拘泥于一种方式,你可以很轻松的在基于注解和 XML的语句映射方式间自由移植和切换。
1、注解的CRUD

1、在MybatisUtils工具类实现自动提交事务

public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }

2、注解在接口上面实现

	//查询所有的用户
    @Select("SELECT * FROM user")
    List<User> getUserList();

    //多个参数,采用@Param传参,注意:sql语句中的参数与@Param的参数保持一致
    //根据id查询用户
    @Select("SELECT * FROM user WHERE id=#{ID} ")
    User getUserListById(@Param("ID") int id);

    //注意:引用类型不用@Param传参
    //添加一个用户
    @Insert("insert into user(id,name,psw)values(#{id},#{name},#{psw}) ")
    int insertUser(User user);

    //修改一个用户
    @Update("update user set name=#{name},psw=#{psw} where id=#{id}")
    int updataUser(User user);

    //根据id删除一个用户
    @Delete("delete from user where id=#{ID}")
    int deleteUserById(@Param("ID") int id);

3、在mybatis-config.xml中绑定上面的接口: 采用class映射到UserMapper接口(重要)

	<mappers>
        <!--注册UserMapperMapper.xml-->
        <!--每一个Mapper.xml都需要在mybatis核心配置中注册-->
        <mapper class="com.sh.dao.UserMapper"/>
    </mappers>

4、测试(测试代码没变)

    @Test
    public void getUserById(){
        //1、通过MybatisUtils工具类,获得sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2、通过sqlSession对象,获得UserMapper(Dao)接口的对象mapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //3、通过mapper对象,调用UserMapper接口中的增删查改的方法
        User user = mapper.getUserListById(1);
        System.out.println(user);

        //4、关闭sqlSession对象
        sqlSession.close();
    }

本质:反射加载类

底层:动态代理

2、数据库的注意点

1、关于@Param()的注意点

  • 基本类型的和String类型的参数需要加上
  • 引用类型不需要加
  • 如果只有一个引用类型可以忽略,但是建议加上
  • 我们在sql语句中引用的就是这里的@Param(“ID”)中设定的属性名ID

2、#{},${}的区别

#{},

1、用来传入参数,sql在解析的时候会加上**" ",**当成字符串来解析 ,如这里 role_id = “roleid”;

2、**#{}**能够很大程度上防止sql注入;

${}

1、用${}传入数据直接显示在生成的sql中,如上面的语句,用role_id = ${roleId,jdbcType=INTEGER},那么sql在解析的时候值为role_id = roleid,执行时会报错;

2、${}方式无法防止sql注入;

3、$一般用入传入数据库对象,比如数据库表名;

4、能用#{}时尽量用#{};

注意:

mybaties排序时使用order by 动态参数时需要注意,使用${}而不用#{};

2、lombok(注解实体类)

1.Lombok简介

意思:Lombok是一个Java库,能自动插入编辑器并构建工具,简化Java开发。通过添加注解的方式,不需要为类编写getter或eques方法,同时可以自动化日志变量

2.Lombok使用

1、先下载jar包

在这里插入图片描述
在这里插入图片描述

2、在maven仓库中找到lombok插件,并在pom.xml中导入

	<dependencies>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
        </dependency>
    </dependencies>
3、常用的lombok注解:
  • @Setter 注解在类或字段,注解在类时为所有字段生成setter方法,注解在字段上时只为该字段生成setter方法。
  • @Getter 使用方法同上,区别在于生成的是getter方法。
  • @ToString 注解在类,添加toString方法。
  • @EqualsAndHashCode 注解在类,生成hashCode和equals方法。
  • @NoArgsConstructor 注解在类,生成无参的构造方法。
  • @RequiredArgsConstructor 注解在类,为类中需要特殊处理的字段生成构造方法,比如final和被@NonNull注解的字段。
  • @AllArgsConstructor 注解在类,生成包含类中所有字段的构造方法。
  • @Data 注解在类,生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
  • @Slf4j 注解在类,生成log变量,严格意义来说是常量。private static final Logger log = LoggerFactory.getLogger(UserController.class);
4.Lombok的优缺点

优点:

  1. 能通过注解的形式自动生成构造器、getter/setter、equals、hashcode、toString等方法,提高了一定的开发效率
  2. 让代码变得简洁,不用过多的去关注相应的方法
  3. 属性做修改时,也简化了维护为这些属性所生成的getter/setter方法等

缺点:

  1. 不支持多种参数构造器的重载(可以手动添加)
  2. 虽然省去了手动创建getter/setter方法的麻烦,但大大降低了源代码的可读性和完整性,降低了阅读源代码的舒适度

八、mybatis详细执行流程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vu4V58qK-1623662901568)(C:\Users\航航\Desktop\未命名文件.png)]

八、多对一的处理

  • 多个学生,对应一个老师
  • 对于学生而言,多个学生,关联一个老师【多对一】:关联
  • 对于老师而言,一个老师,由很多学生【一对多】:集合
    在这里插入图片描述

案例环境搭建(多对一)

1、建立数据库的两张表

//建立一张表teacher
CREATE TABLE teacher(
	id INT(10) NOT NULL PRIMARY KEY,
	name VARCHAR(30) DEFAULT(NULL)

)ENGINE=INNODB 

INSERT INTO teacher(id,name) VALUES(1,'航老师');

SELECT * FROM teacher;

//建立一张表student
CREATE TABLE student(
	id int(10) NOT NULL, 
	name VARCHAR(30) DEFAULT NULL,
	tid INT(10) DEFAULT NULL,
	PRIMARY KEY(id),
	KEY fktid (tid),
	CONSTRAINT fktid FOREIGN KEY(tid) REFERENCES teacher (id)
)ENGINE=INNODB 



INSERT INTO student(id,name,tid) VALUES (2,'小明',1);

INSERT INTO student (id,name,tid)VALUE (3,'小虎',1);
INSERT INTO student (id,name,tid) VALUES (4,'小航',1);
INSERT INTO student (id,name,tid) VALUES(5,'小马',1);

SELECT * FROM student;

2、连接数据库

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7ax7WP0H-1623662901570)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618042254177.png)]

3、导入各种依赖

4、新建实体类(pojo)

5、新建Mapper接口

6、建立Mapper.xml

7、在核心配置文件中,绑定我们的Mapper.xml【有多种方式】

8、测试

1、按照查询嵌套处理(子查询)

1、编写pojo

@Data
public class Student {
    private int id;
    private String name;
    //学生需要关联一个老师(多对一:关联)
    private Teacher teacher;
}
@Data
public class Teacher {
    private int id;
    private String name;
}

2、编写接口:StudentMapper

public interface StudentMapper {
    //@Select("select * from student")
    List<Student> getStudent();
}

3、编写StudentMapper.xml

方式一:

**注意:**association标签中两个属性

  1. javaTypejavaType是对应的property = teacher对象的类型(在外面用了别名)

  2. selectselect是对应的下面的select标签,通过这样的嵌套查询,由id查到老师

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q3nbzouN-1623662901570)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618109529730.png)]

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sh.dao.StudentMapper">
    <!--思路:
            1、查询所有的学生信息
            2、根据查询出来的学生的tid,寻找对应的老师,=====子查询
    -->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from student
    </select>

    <!-- 定义java Bean的属性与数据库的列之间的映射 -->
    <resultMap id="StudentTeacher" type="student1">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性我们需要单独处理
             对象:association
             集合: collection
        -->
        <association property="teacher" column="tid" javaType="teacher1" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="teacher1">
        select * from teacher where id=#{id}
    </select>
</mapper>

方式二:

​ 1、在association标签中直接指定select=“com.sh.dao.TeacherMapper.getTeacher”,绑定到查询老师的嵌套的getTeacher方法,就不用后面的根据id,查询老师的select标签了

​ 2、但是需要这个接口,因为在这里要通过select反射到该接口实现查询老师

​ 3、说白了,就是将第一种方式的select标签换成这个接口了

public interface TeacherMapper {
    @Select("select * from teacher")
    Teacher getTeacher();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sh.dao.StudentMapper">
    <!--思路:
            1、查询所有的学生信息
            2、根据查询出来的学生的tid,寻找对应的老师,=====子查询
    -->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from student
    </select>

    <!-- 定义java Bean的属性与数据库的列之间的映射 -->
    <resultMap id="StudentTeacher" type="student1">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性我们需要单独处理
             对象:association
             集合: collection
        -->
        <association property="teacher" column="tid" javaType="teacher1" select="com.sh.dao.TeacherMapper.getTeacher"/>
    </resultMap>
</mapper>

3、测试

@Test
    public void getAllStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }

2、按照结果集映射(连表查询)

1、编写pojo

@Data
public class Student {
    private int id;
    private String name;
    //学生需要关联一个老师(多对一:关联)
    private Teacher teacher;
}
@Data
public class Teacher {
    private int id;
    private String name;
}

2、编写接口

public interface StudentMapper {
    List<Student> getStudent2();
}

3、编写StudentMapper.xml

    <!--多对一方式二:结果嵌套处理
        思路:
            1、首先,写select标签中写连表查询语句
            2、然后,在resultMap标签中,直接进行映射,复杂类型用association
            注意:这里的teacher没用column,是因为结果集映射,teacher直接映射到Teacher对象
            3、最后,在association标签中再映射teacher对象的各个属性即可
    -->
    <select id="getStudent2" resultMap="StudentTeacher2">
       select s.id sid,s.name sname,t.id tid,t.name tname from mybatis.teacher t, mybatis.student s where s.tid = t.id;
    </select>
    <resultMap id="StudentTeacher2" type="student1">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <!--复杂的属性我们需要单独处理
             对象:association
             集合: collection
        -->
        <association property="teacher" javaType="teacher1">
            <result property="name" column="tname"/>
            <result property="id" column="tid"/>
        </association>
    </resultMap>

4、测试

@Test
    public void getAllStudent2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent2();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }

九、一对多处理

  • 一个老师拥有多个学生
  • 对于老师而言就是,一对多的关系

1、按照结果集查询

1、编写pojo

@Data
public class Student {
    private int id;
    private String name;
    private int tid;
}
@Data
public class Teacher {
    private int id;
    private String name;
    //一个老师拥有多个学生
    private List<Student> students;
}

2、编写TeacherMapper接口

public interface TeacherMapper {
    //获取指定老师下的所有学生及老师的信息
    Teacher getTeacher(@Param("tid") int id);
}

3、编写TeacherMapper.xml

<mapper namespace="com.sh.dao.TeacherMapper">
    <!--按照结果集查询-->
    <!--这里的tid就是传过来的参数tid-->
    <select id="getTeacher" resultMap="TeacherStudent" parameterType="_int">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from mybatis.teacher t,mybatis.student s
        where s.tid=t.id and t.id = #{tid}
    </select>
    <!--javabean映射数据库的字段-->
    <resultMap id="TeacherStudent" type="teacher1">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--
               javaType:指定属性的类型
               ofType:集合中的泛型中的约束,我们用ofType来获取 
        -->
        <!--复杂的属性我们需要单独处理
             对象:association
             集合: collection
        -->
        <collection property="students" ofType="student1">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
</mapper>

4、测试

   @Test
    public void getTeacherStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacher(1);
        System.out.println(teacher);
        sqlSession.close();

    }

2、按照嵌套查询(子查询)

1、编写pojo

@Data
public class Student {
    private int id;
    private String name;
    private int tid;
}
@Data
public class Teacher {
    private int id;
    private String name;
    //一个老师拥有多个学生
    private List<Student> students;
}

2、编写TeacherMapper.java接口

public interface TeacherMapper {
    //获取指定老师下的所有学生及老师的信息
    Teacher getTeacher2(@Param("tid") int id);
}

3、编写TeacherMapper.xml

<mapper namespace="com.sh.dao.TeacherMapper">
    <!--按照嵌套查询(子查询)-->
    <!--teacher表的tid======由column="id"=====传给了student表中sql语句tid-->
    <select id="getTeacher2" resultMap="TeacherStudent2">
        select * from  mybatis.teacher where id=#{tid}
    </select>
    <resultMap id="TeacherStudent2" type="teacher1">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--
            javaType:是property属性的返回值类型
            ofType:是改返回值类型的泛型里面的对象所属的类
        -->
        <!--
			 复杂的属性我们需要单独处理
             对象:association
             集合: collection
        -->
        <collection property="students" javaType="ArrayList" ofType="student1" column="id" select="getStudentID"/>
    </resultMap>
    <select id="getStudentID" resultType="student1">
        select * from mybatis.student where tid=#{tid}
    </select>
</mapper>

4、测试

 @Test
    public void getTeacherStudent2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacher2(1);
        System.out.println(teacher);
        sqlSession.close();

    }

5、结果
\AppData\Roaming\Typora\typora-user-images\1618127152923.png)]

3、小结

1、关联:assocition【多对一】对象

2、集合:collection【一对多】集合

3、javaType & ofType

  • JavaType是用来指定pojo中属性的类型
  • ofType指定的是 映射到list或集合属性中pojo的类型 ,泛型中的约束类型

十、动态sql

1、什么是动态SQL:

动态sql就是根据不同的条件生成不同的sql

所谓的动态sql:本质还是SQL语句,只是我们在SQL层面,去执行一个逻辑代码

动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合就好了

动态SQL的使用相当于JSTL标签

你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

2、搭建环境

CREATE TABLE blog(
	id VARCHAR(50) NOT NULL COMMENT '博客id',
	title VARCHAR(100) NOT NULL COMMENT '博客标题',
	author VARCHAR(30) NOT NULL COMMENT '博客作者',
	create_time DATETIME NOT NULL COMMENT '创建时间',
	views INT(30) NOT NULL COMMENT '浏览量'
)ENGINE=INNODB DEFAULT CHARSET=utf8

3、if

1、编写BlogMapper.java接口
public interface BlogMapper {
    //根据map查询博客数据
    List<Blog> selectBlogIF(Map map);
}
2、编写Blog.java的pojo实体类
@Data
public class Blog {
    private String id;//这里的id采用随机生成
    private String title;
    private String author;
    //javabean属性名和数据库字段名不一样,在mybatis-config.xml采用setting的驼峰命名设置
    //将数据库中create_time转变成createTime
    private Date createTime;
    private int views;
}

2.1、在Utils工具类的包中,编写id随机生成的IdUtils.java类,随机生成id

public class IdUtils {
    public static String getId(){
        //将所有的字符串中的-用null替代
        return UUID.randomUUID().toString().replace("-","");
    }
}

2.2、在mybatis-config.xml的核心配置文件中,配置将数据库中create_time转变成createTime

<settings>
     <!--是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
3、编写BlogMapper.xml
<mapper namespace="com.sh.dao.BlogMapper">
    <!--if标签-->  
    <select id="selectBlogIF" parameterType="map" resultType="blog1">
        select * from mybatis.blog where 1=1
        <if test="title!=null">
            and title=#{title}
        </if>
        <if test="author!=null">
            and author=#{author}
        </if>
    </select>
</mapper>
4、测试插入数据
   //查询博客
    @Test
    public void selectTestIF(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();
        map.put("title","mysql精通");
        map.put("author","航航");
        List<Blog> blogList = mapper.selectBlogIF(map);
        for (Blog blog : blogList) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

4、where

代替sql语句中的where关键字

1、编写BlogMapper.java接口

public interface BlogMapper {
    //测试wehere标签
    List<Blog> selectBlogWhere(Map map);
}

2、编写BlogMapper.xml

注意:

  • 不关view和author是否为空,标签中的多余and会根据数据自动去掉
  • 如果标签中没有sql,则就会直接执行上面的sql语句
<!--Where标签的使用-->
    <select id="selectBlogWhere" parameterType="map" resultType="blog1">
        select * from mybatis.blog
        <where>
            <if test="views!=null">
                and views=#{views}
            </if>
            <if test="author!=null">
               and author=#{author}
            </if>
        </where>
    </select>

5、set

代替sql语句中的set关键字

1、编写BlogMapper.java接口

public interface BlogMapper {
    //set标签
    int updateBlogSet(Map map);
}

2、编写BlogMapper.xml

注意:

  • 不关view和author是否为空,标签中多余的逗号会根据数据自动去掉
<!--Set标签的使用-->
    <update id="updateBlogSet" parameterType="map">
        update mybatis.blog
        <set>
            <if test="author!=null">
                 author=#{author},
            </if>
            <if test="views!=null">
                  views=#{views},
            </if>
        </set>
        where id=#{id}
    </update>

6、choose (when, otherwise)

有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用。

1、编写BlogMapper.java接口

public interface BlogMapper {
    //choose标签的使用
    List<Blog> selectBlogChoose(Map map);
}

2、编写BlogMapper.xml

注意:

  • choose语句只能执行一句,就像switch case语句一样
<!--Choose标签的使用:相当于switch case-->
    <select id="selectBlogChoose" parameterType="map" resultType="blog1">
        select * from mybatis.blog
        <where>
            <choose>
                <when test="author!=null">
                    author=#{author}
                </when>
                <when test="views!=null">
                    and views=#{views}
                </when>
                <otherwise>
                    and title=#{title}
                </otherwise>
            </choose>
        </where>
    </select>

7、foreach

  • 一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)
  • 可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach
  • 使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素
  • 使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值

1、编写BlogMapper.java接口

public interface BlogMapper {
    //foreach标签的使用
    List<Blog> selectBlogForeach(Map map);
}

2、编写BlogMapper.xml

<!--foreach标签的使用-->
    <select id="selectBlogForeach" parameterType="map" resultType="blog1">
        select * from mybatis.blog
        <where>
<!--
   ids:我们现在传递一个万能的map,这个map中可以存在一个集合(List)
   id:从ids这个集合中遍历的每一项的名字叫做id,这个id是不停的变,才能遍历集合
   这里遍历list集合:使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素
-->
            <foreach collection="ids" index="ids" item="id" open="(" separator="or" close=")">
                <!--#{id}:是item的id-->
                id=#{id}
            </foreach>
        </where>
    </select>

3、编写测试类

  • ids:这里的 map.put(“ids”,list)==对应上面collection=“ids”
 //测试foreach标签
    @Test
    public void selectTestforeach(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);

        Map map = new HashMap();
        map.put("ids",list);
        List<Blog> blogList = mapper.selectBlogForeach(map);
        for (Blog blog : blogList) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

8、sql片段

**理解:**有的时候我们可能会将一些公共的sql语句抽取出来,方便复用

用法:

1、使用sql标签抽取公共部分

<!--提取出来的sql,方便复用-->
    <sql id="title-author">
        <if test="title!=null">
            and title=#{title}
        </if>
        <if test="author!=null">
            and author=#{author}
        </if>
    </sql>

2、在需要使用的地方使用include标签引用即可

<!--IF标签的使用-->
    <select id="selectBlogIF" parameterType="map" resultType="blog1">
        select * from mybatis.blog where 1=1
        <!--引入sql-->
        <include refid="title-author"/>
    </select>

3、注意事项

  • 最好基于单表来定义SQL片段
  • 不要存在where标签

十一、缓存

1、缓存简介

1、什么是缓存【Cache】?

  • 存在内存中的零时数据
  • 将用户经常查询的数据放在缓存(内存)中,用户去查询数据库就不用从磁盘上(关系型数据库文件)查询,而是从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题

2、为什么使用缓存?

  • 减少和数据库的交互次数,减少系统开销,提高系统的效率

3、什么样的数据能使用缓存?

  • 经常查询并且不经常高边数据

2、Mybatis缓存

1、Mbatis包含一个非常大的查询缓存特性,它可以非常方便地定制和配置缓存,缓存可以极大的提升查询效率

2、Mybatis系统中默认定义了两级缓存:一级缓存二级缓存

  • 默认情况下,一级缓存是开启的(SqlSession级别的缓存,也称本地缓存,只在sqlSession创建到关闭之间有用)
  • 全局的二级缓存需要手动开启和配置,它是基于namespace级别的缓存
  • 为了提高扩展性,Mybatis定义了缓存接口,我们可以实现Cache接口来定义全局的(表级)二级缓存

3、刷新缓存知识点:

  • 映射语句文件中的所有 select 语句的结果将会被缓存。
  • 映射语句文件中的所有 insert、update 和 delete 语句会刷新缓存。
  • 缓存会使用最近最少使用算法(LRU, Least Recently Used)算法来清除不需要的缓存。
  • 缓存不会定时进行刷新(也就是说,没有刷新间隔)。
  • 缓存会保存列表或对象(无论查询方法返回哪种)的 1024 个引用。
  • 缓存会被视为读/写缓存,这意味着获取到的对象并不是共享的,可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。

4、清楚缓存的策略

  • LRU – 最近最少使用:移除最长时间不被使用的对象。(默认)
  • FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
  • SOFT – 软引用:基于垃圾回收器状态和软引用规则移除对象。
  • WEAK – 弱引用:更积极地基于垃圾收集器状态和弱引用规则移除对象

3、一级缓存

1、一级缓存也叫本地缓存
  • 一级缓存的作用域是SqlSession

  • 数据库同一次会话期间查询到的数据会放在本地缓存中

  • 以后如果需要获得相同的数据,直接从缓存中拿,没必要再去查询数据库,没必要执行SQL(这样也快一点)

  • 在操作数据库时需要构造 sqlSession 对象,**在对象中有一个(内存区域)数据结构(HashMap)用于存储缓存数据。不同的 sqlSession 之间的缓存数据区域(HashMap)是互相不影响的。**用一张图来表示:一级缓存,其中每一个 SqlSession 的内部都会有一个一级缓存对象。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EF6W71IX-1623662901572)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618229125088.png)]

2、一级缓存测试步骤:

​ 1、打开日志

​ 2、测试在一个sqlSession中查询两次相同的记录

//根据id查询用户
    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.getUserById(1);
        System.out.println(user1);

        System.out.println("-----------------------");
        User user2 = mapper.getUserById(1);
        System.out.println(user2);
        sqlSession.close();

    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uonnFVf3-1623662901572)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618300140384.png)]

3、一级缓存失效
  • 查询不同的sql语句
//根据id查询用户
    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.getUserById(1);
        System.out.println(user1);

        System.out.println("-----------------------");
        User user2 = mapper.getUserById(2);
        System.out.println(user2);
        sqlSession.close();

    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FWc9VKhF-1623662901573)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618300478966.png)]

  • 增删改,可能改变原来的数据,必定刷新缓存
@Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.getUserById(1);
        System.out.println(user1);


        HashMap<Object, Object> map = new HashMap<Object, Object>();
        map.put("name","hello");
        map.put("psw","00000");
        map.put("id",4);
        //更新用户

        int i = mapper.updateUser(map);

        System.out.println("-----------------------");
        User user2 = mapper.getUserById(1);
        System.out.println(user2);
        sqlSession.close();
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-scZdHuxU-1623662901574)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618301354486.png)]

  • 查询不同的Mapper.xml

就相当于查询不同的的sql语句

  • 手动清理缓存(sqlSession.clearCache()😉
 @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.getUserById(1);
        System.out.println(user1);

        //手动清理缓存
        sqlSession.clearCache();

        System.out.println("-----------------------");
        User user2 = mapper.getUserById(1);
        System.out.println(user2);
        sqlSession.close();
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uQSlBbDx-1623662901574)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618302007185.png)]

4、二级缓存

1、概念

1、二级缓存也叫全局缓存,一级缓存的作用域太低了,所以诞生了二级缓存

2、基于namespace级别的缓存,一个名称空间,对应一个二级缓存

3、工作机制

  • 一个会话查询了一条数据,这个数据就会在当前会话的一级缓存中

  • 如果当前会话关闭了,这个会话对应的一级缓存就没有了,但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中

  • 型的会话查询信息,就可以在二级缓存中获取内容

  • 不同的mapper查出的数据会放在自己对应的缓存(map)中

2、二级缓存使用步骤

1、 设置开启全局缓存(mbatis-config.xml)

<settings>
        <!--(二级缓存)全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。默认开启 -->
        <setting name="cacheEnabled " value="true"/>
    </settings>

2、在需要使用的Mapper.xml中开启

<cache/>

也可以自定义参数

  • 这个更高级的配置创建了一个 FIFO 缓存,
  • 每隔 60 秒刷新,
  • 最多可以存储结果对象或列表的 512 个引用,
  • 而且返回的对象被认为是只读的
<!--在当前要使用的Mapper.xml开启二级缓存-->
    <cache
            eviction="FIFO"
            flushInterval="60000"
            size="512"
            readOnly="true"/>

3、测试

注意:

  • 这里由于开启了一级缓存,虽然说关闭了二级缓存,但是二级缓存,在关闭的时候,会将自己的数据,传递给二级缓存,所以二级缓存在查东西的时候,会直接在二级缓存中查询,而不是在数据库中查询
@Test
    public void getUserById(){
        SqlSession sqlSession1 = MybatisUtils.getSqlSession();
        SqlSession sqlSession2 = MybatisUtils.getSqlSession();
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
        UserMapper mapper1 = sqlSession1.getMapper(UserMapper.class);
        User user1 = mapper1.getUserById(1);
        System.out.println(user1);
        sqlSession1.close();


        System.out.println("-----------------------");


        User user2 = mapper2.getUserById(1);
        System.out.println(user2);

        sqlSession2.close();

    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5djNF6iU-1623662901575)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618305975356.png)]

小结:

  1. 当 Java 对象需要在网络上传输 或者 持久化存储到文件中时,就需要对 Java 对象进行序列化处理。(需要对实体类序列化)
  2. 只要开启了二级缓存,在同一个Mapper.xml中就有效
  3. 所有的数据都会先放在一级缓存中
  4. 只有当会话提交,会话关闭的时候,才会提交到二级缓存中去

5、缓存底层原理

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IrxiNo8V-1623662901575)(C:\Users\航航\AppData\Roaming\Typora\typora-user-images\1618309331885.png)]

6、自定义缓存【ehcache】

1、概念

  • ehcahe是一种广泛使用的开源Java分布式缓存,主要面向通用缓存

使用eheache步骤

​ 1、先导入依赖

		<!--自定义缓存的依赖-->
        <dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.2.1</version>
        </dependency>

2、导入配置文件 eheache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">
    <!--
       diskStore:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。参数解释如下:
       user.home – 用户主目录
       user.dir  – 用户当前工作目录
       java.io.tmpdir – 默认临时文件路径
     -->
    <diskStore path="java.io.tmpdir/Tmp_EhCache"/>
    <!--
       defaultCache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。
     -->
    <!--
      name:缓存名称。
      maxElementsInMemory:缓存最大数目
      maxElementsOnDisk:硬盘最大缓存个数。
      eternal:对象是否永久有效,一但设置了,timeout将不起作用。
      overflowToDisk:是否保存到磁盘,当系统当机时
      timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
      timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
      diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
      diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
      diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
      memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
      clearOnFlush:内存数量最大时是否清除。
      memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
      FIFO,first in first out,这个是大家最熟的,先进先出。
      LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
      LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
   -->
    <defaultCache
            eternal="false"
            maxElementsInMemory="10000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="259200"
            memoryStoreEvictionPolicy="LRU"/>

    <cache
            name="cloud_user"
            eternal="false"
            maxElementsInMemory="5000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="1800"
            memoryStoreEvictionPolicy="LRU"/>

</ehcache>

3、在Mapper.xml中引入

<!--引入第三方缓存,相当于自定义缓存-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

自定义缓存,就是需要实现Cache接口,即可

package com.sh.utils;

import org.apache.ibatis.cache.Cache;

public class MyChcache implements Cache {
    public String getId() {
        return null;
    }

    public void putObject(Object o, Object o1) {

    }

    public Object getObject(Object o) {
        return null;
    }

    public Object removeObject(Object o) {
        return null;
    }

    public void clear() {

    }

    public int getSize() {
        return 0;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值