Mybatis学习笔记

目录

简介

什么是Mybatis:

持久层和持久化:

Mybatis的优势:

第一个Mybatis程序

搭建环境

2.创建模块

若是文件导出失败则在Maven的配置文件中配置如下

增删改查

 1.namespace

2.select

 万能的Map

模糊查询

配置解析

1.核心配置文件

2.环境配置(environments)

3.属性(properties) 

4.类型别名(typeAliases)

​ 5.设置

​6.映射器(mappers)

 生命周期和作用域

解决属性名和字段名不一致的问题:ResultMap

解决方法:

        2.ResultMap 结果集映射

日志

        日志工厂

        LOG4J实现

分页

limit分页

RowBounds实现分页不建议开发中使用:

分页插件

 使用注解开发

注解增删改查

@Param()注解

#{}${}的区别:

Mybatis执行的流程

Lombok

 多对一处理 (association)

按照查询嵌套处理

按照结果嵌套处理

一对多处理

按结果嵌套查询

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

小结

面试高频:

动态SQL

SQL标签,抽取公共部分

IF

choose,when,otherwise(Java中的switch,case,default)

trim(where,set)

where

SET

trim自定义

Foreach

缓存(cache)

Mybatis缓存

一级缓存:

二级缓存

 流程:

缓存原理

自定义缓存Ehcache


官网文档:

https://mybatis.org/mybatis-3/zh/index.html

简介

什么是Mybatis:

        MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录

        如何获得MyBatis?

        1.maven        

        2.GitHub

持久层和持久化:

        将程序的数据在持久状态和瞬时状态转化的过程

        持久化:数据库,io文件持久化

        持久层:

                完成持久化工作的代码块

Mybatis的优势:

        方便

        传统的JDBC代码过于复杂,想要提高效率。自动化

使用的人多!

Mybatis Spring   SpringMVC  SpringBoot

第一个Mybatis程序

搭建环境--导入Mybatis--编写代码--测试

搭建环境

搭建数据库

CREATE TABLE `user`(
`id` int(20) not null,
`name` VARCHAR(30) DEFAULT null,
`pwd` VARCHAR(30) DEFAULT null,
primary key(`id`)
)ENGINE=Innodb DEFAULT CHARSET=utf8;


insert into `user`(`id`,`name`,`pwd`) values 
(1,'宋燚','123'),
(2,'宋扬','123'),
(3,'一帆','123')

新建项目

导入MYSQL,MYBATIS,JUNIT

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

2.创建模块

编写Mybatis的默认配置文件

创建新文件,命名为mybatis-config

添加如下代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

修改过的个人代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <!--事务管理-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--连接名-->
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <!--URL-->
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
                <!--账号密码-->
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

编写mybatis工具类:

package com.song.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

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();
        }
    }
    //获取sqlsession对象
    public static SqlSession getSqlSession(){
        return   sqlSessionFactory.openSession();
    }

}

编写代码

        1 实体类

        

package com.song.pojo;

public class User {
    private int id;
    private  String name;
    private  String pwd;

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    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 getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

        2 Dao接口        

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

        3 接口实现类(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">

<!--绑定一个对应的Mapper接口-->
<mapper namespace="com.song.dao.UserDao">
    <!--查询语句
        id可以理解为方法名
        resultType结果类型 返回一个
        resultMap结果集    返回多个
    -->
    <select id="getUserList" resultType="com.song.pojo.User">
        select * from mybatis.`user`;
    </select>
    <select id="selectBlog" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>

测试

注意:

org.apache.ibatis.binding.BindingException: Type interface com.song.dao.UserDao is not known to the MapperRegistry.
 

MapperRegistry是什么

每一个Mapper.xml都要在核心配置中注册!

<mappers>
        <mapper resource="com/song/dao/UserMapper.xml"/>
    </mappers>

若是文件导出失败则在Maven的配置文件中配置如下

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

编写测试类

public class UserDaoText {
    @Test
    public void test(){
        //获取Sqlsession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //执行sql
        UserDao mapper =sqlSession.getMapper(UserDao.class);
        List<User> userList = mapper.getUserList();
        for (User user:userList){
            System.out.println(user.getName());
        }
        sqlSession.close();
    }
}

第二种方式

  //方式2
        List<User> userList =sqlSession.selectList("com.song.dao.UserDao.getUserList");
       根据方法返回值返回,

         for (User user:userList){
            System.out.println(user.getName());
        }

成功输出

一定要记得关闭SqlSession!!

一定要记得关闭SqlSession!!

一定要记得关闭SqlSession!!

可能会遇到的问题:

        配置文件没有注册

        绑定接口错误

        方法名不对

        返回类型不对

        maven导出资源问题

 总结:

1.写工具类(写配置文件)

2.写实体类,

3接口,

4Mapper.xml

测试  

增删改查

增删改需要提交事务!!!!

增删改需要提交事务!!!!

增删改需要提交事务!!!!

 1.namespace

namespace中的包名要和Mapper接口的包名一致

2.select

        id:方法名(唯一标识)

        resultType:sql语句返回值

        parameterType:参数类型

3.insert

4.update

5.delete

综合:

接口:

public interface UserMapper {
    //查询全部用户
    List<User> getUserList();
    //根据ID查询用户
    User getUserById(int id);
    //插入一个用户
    int addUser(User user);
    //修改用户
    int updateUser(User user);
    //删除一个用户
    int deleteUser(int id);
}

mapper.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接口-->
<mapper namespace="com.song.dao.UserMapper">
    <!--查询语句
        id可以理解为方法名
        resultType结果类型 返回一个
        resultMap结果集    返回多个
    -->
    <select id="getUserList" resultType="com.song.pojo.User">
        select * from mybatis.`user`;
    </select>
    <select id="getUserById" parameterType="int" resultType="com.song.pojo.User">
        select * from mybatis.`user` where `id`=#{id};
    </select>
    <!--
    对象中的属性可以直接取出来,insert语句没有返回值类型
    -->
    <insert id="addUser" parameterType="com.song.pojo.User" >
        insert into mybatis.`user`(id,`name`,pwd)values(#{id},#{name},#{pwd});
    </insert>
    <!--修改-->
    <update id="updateUser" parameterType="com.song.pojo.User">
        update mybatis.`user` set `name`=#{name} ,`pwd`=#{pwd} where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from mybatis.`user` where id=#{id};
    </delete>
</mapper>

测试类:


public class UserMapperText {
    @Test
    public void test(){
        //获取Sqlsession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
//        //执行sql
        UserMapper mapper =sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
//


//        //方式2
  //     List<User> userList =sqlSession.selectList("com.song.dao.UserDao.getUserList");
       for (User user:userList){
           System.out.println(user.getName());
       }
        sqlSession.close();
    }
    @Test
    public void getUserById(){
        //获取Sqlsession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper =sqlSession.getMapper(UserMapper.class);
        User user= mapper.getUserById(1);
        System.out.println(user.getName());
        sqlSession.close();
    }
    //增删改需要提交事务
    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper =sqlSession.getMapper(UserMapper.class);

        mapper.addUser(new User(4,"升达","11214"));

        //提交事务
        sqlSession.commit();

        //关闭
        sqlSession.close();
    }
    @Test
    public void updateUser(){
        SqlSession sqlSession =MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        mapper.updateUser(new User(1,"宋燚","12"));
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void deleteUser(){
        SqlSession sqlSession =MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        mapper.deleteUser(1);
        sqlSession.commit();
        sqlSession.close();
    }
}

 万能的Map

如果字段过多怎么办?用Map或注解

使用Map

接口:

  //Map添加用户
    User getUserById2(Map<String,Object> map);

Mapper.xml

    <insert id="getUserById2" parameterType="map">
        insert into mybatis.`user`(id,`name`,pwd)value (#{userid},#{username},#{userpwd});
    </insert>

测试

public void getUser2(){
        SqlSession sqlSession =MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        Map<String,Object> map=new HashMap<String, Object>();
        map.put("userid",5);
        map.put("username","w");
        map.put("userpwd","2131");
        mapper.getUserById2(map);
        
        sqlSession.commit();
        sqlSession.close();
    }

模糊查询

 1.在java代码执行的时候传递通配符

2.在sql拼接中使用通配符

配置解析

1.核心配置文件

        mybatis-config.xml

        配置文件会深深影响Mybatis行为的设置和属性信息。

        

2.环境配置(environments)

 

3.属性(properties) 

通过db.properties设置属性

编写一个db.properties配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF8
username=root
password=root

在核心配置文件中引入

 <properties resource="db.properties"/>

也可以在properties标签内设置属性,但是同时设置时,优先使用外部文件的配置

 <properties resource="db.properties">
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </properties>

4.类型别名(typeAliases)

 <typeAliases>
        <!--指定类起别名-->
        <typeAlias type="com.song.pojo.User" alias="User"/>
        <!--指定包起别名,在没有注解的情况下,默认别名就是这个类的类名首字母小写-->
        <package name="com.song.pojo"/>
    </typeAliases>

 5.设置

  日志:

6.映射器(mappers)

 MapperRegistry:注册绑定Mybatis文件

 生命周期和作用域

重要,错误的使用会导致并发问题

 

解决属性名和字段名不一致的问题:ResultMap

当数据库字段名和实体类的额属性名不一致时:

        输出的不一致的字段名的值为null

解决方法:

        1.起别名(一般不用)

        给sql语句的字段名起别名  ‘’AS‘’关键字

        2.ResultMap 结果集映射

        

日志

        日志工厂

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

        曾经:debug,sout

        现在:日志工厂

        

  •  SLF4J
  •  LOG4J(掌握)
  •  LOG4J2
  • JDK_LOGGING
  •  COMMONS_LOGGING
  •  STDOUT_LOGGING(掌握)
  •  NO_LOGGING

在Mybatis中具体使用哪一个日志实现,在xml设置中设定!

        标准的日志工厂设置:

 <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

        LOG4J实现

1.导包

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

2.配置文件 log4j.properties


# console:控制台
#INFO:消息
#file:文件
log4j.rootLogger=DEBUG,console,file
##控制台输出相关设置
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target =System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%C]-%m%n
#[QC] %p [%t] %C.%M(%L) | %m%n
##文件输出相关设置
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=./log/song.log
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

   3.配置log4j为日志的实现

  <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

4.log4j的使用

        (1)在使用该类的类中导包要看准阿帕奇包

        

        (2)创建对象,参数为当前类的class

        static Logger logger =Logger.getLogger(LogTest.class);

         (3)使用(级别)

logger.info("info:进入了logtesst方法");
logger.debug("debug:进入了logtesst方法");
logger.error("error:进入了logtesst方法");

分页

limit分页

SELECT *from 表名 limit 初始数据起始位置(第一个从0开始),一页显示几个

如果只给定一个参数,就传输第一个到该参数的数据

使用Mybatis实现分页

1.接口

    //分页
    List<User> getUserByLimit(Map<String,Integer> map);

2.mapper.xml

<select id="getUserByLimit" parameterType="map" resultType="User">
    select * from `user` limit #{startIndex},#{pageSize}
    </select>


3.测试

 public void getbylimit(){
        SqlSession sql= MybatisUtils.getSqlSession();
        UserMapper mapper =sql.getMapper(UserMapper.class);
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("startIndex",0);
        map.put("pageSize",2);
        List<User> list=mapper.getUserByLimit(map);
            for (User user:list){
                System.out.println(user.toString());
            }
        sql.close();
    }

RowBounds实现分页不建议开发中使用:

1.接口

//分页2
    List<User> getUserByRowBounds();

2.xml

<select id="getUserByRowBounds" parameterType="map" resultType="User">
    select * from `user`
    </select>

3.代码

public void getbyRowBounds(){
        SqlSession sql= MybatisUtils.getSqlSession();
        //RowBounds分页
        RowBounds rowBounds = new RowBounds(1,2);



        //java代码实现分页
       List<User> list =sql.selectList("com.song.dao.UserMapper.getUserByRowBounds",null,rowBounds);
        for (User user:list){
            System.out.println(user.toString());
        }



        sql.close();
    }

分页插件

 使用注解开发

本质:反射机制实现

底层:动态代理

面向接口编程

原因:解耦

接口即是定义、约束实现的分离

注解开发:

        

 注意:简单的sql语句可以使用注解,但是复杂的sql语句还是需要在xml中映射。

1.接口

public interface UserMapper {
    @Select("select * from `user`")
    public List<User> getUsers();

}

2.测试类

 @Test
    public void testtt(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users =mapper.getUsers();
        for (User user:users){
            System.out.println(user.getName());
        }

        sqlSession.close();
    }

注解增删改查

可以在工具类创建的时候实现自动提交事务

在工具类的获取Sqlsession对象时加入参数
 

//获取sqlsession对象
    public static SqlSession getSqlSession(){
        return   sqlSessionFactory.openSession(true);
    }

查询:

    //通过ID查询
    //方法存在多个参数,所有参数方法前必须加入@Param注解,为了和sql语句中的#{}对应
    @Select("select * from `user` where id=#{id} ")
    User getById(@Param("id") int userid,@Param("name") String username);

更新

    //更新
    @Update("update `user` set name=#{name},pwd=#{pwd} where id=#{id}")
    int upUser(User user);

删除

  //删除
    @Delete("delete from user where id=#{uid}")
    int DU(@Param("uid") int id);

@Param()注解

基本类型和S听类型的参数需要加上

如果只有一个基本类型,可以不加

引用类型不需要加

我们在sql中引用的就是@Param设定的属性名

#{}${}的区别:

能用#就用#,

$无法防止SQL注入 

Mybatis执行的流程

Lombok

只需要在类上加一个注解,就不用写get set 等重复代码。

安装Lombok插件或者maven下载

<repositories>
    <repository>
        <id>projectlombok.org</id>
        <url>https://projectlombok.org/edge-releases</url>
    </repository>
</repositories>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>edge-SNAPSHOT</version>
    <scope>provided</scope>
</dependency>

导入jar包

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
    <scope>provided</scope>
</dependency>
 

说明:

1.安装插件

2.导入jar包

3.在实体类上加注解


 多对一处理 (association)

多个学生,对应一个老师

用结果映射中的resultMap来实现

创建表,学生表和老师表,学生表的tid与老师的id绑定(外键)

测试环境搭建

建实体类,mapper接口,mapper。xml文件,

按照查询嵌套处理

<mapper namespace="com.song.dao.StudentMapper">
    <!--
        1.查询所有学生信息
        2.根据学生的tid查询对应的老师
    -->
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂属性
            对象:association
            集合:collection
        -->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getStudent" resultMap="StudentTeacher">
        select  * from student
    </select>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id =#{tid}
    </select>
</mapper>

按照结果嵌套处理

   <select id="getStudent" resultMap="StudentTeacher">
            select s.id sid,s.name sname,t.name tname
            from student s,teacher t 
            where s.tid=t.id
        </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

一对多处理

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

1,环境搭建

实体类

 2.测试

按结果嵌套查询

javaType=""是一个指定的属性类型,List不符合规范,集合中的泛型信息使用ofType
 <resultMap id="TeacherStudent" type="song.pojo.Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="song.pojo.Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from student s,teacher t
        where s.tid=t.id and t.id=#{tid}
    </select>

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

    <select id="getTeacher" resultMap="TeacherStudent">
        select *from teacher where id =#{tid}
    </select>
    <resultMap id="TeacherStudent" type="song.pojo.Teacher">
        <collection property="students" column="id" javaType="ArrayList" ofType="song.pojo.Student" select="getS"/>

    </resultMap>
    <select id="getS" resultType="song.pojo.Student">
        select * from student where tid=#{id}
    </select>

小结

1.关联association多对一

2.集合collection一对多

3.javatype   &  oftype

        javatype用来指定实体类中属性的类型

        oftype指定映射到list或者集合中的pojo类型,泛型中的约束类型 

注意点:保证sql的可读性,尽量通俗易懂

        排查问题使用log4j

面试高频:

Mysql引擎

InnoDB底层原理

索引

索引优化

动态SQL

指根据不同的条件生成不同的sql语句

SQL标签<sql>,抽取公共部分

使用include标签引用

<select id="queryBlogChoose" parameterType="map" resultType="com.song.pojo.Blog">
        select * from blog where 1=1
        <include refid="if"/>
    </select>
    <sql id="if">
        <choose>
            <when test="title != null"></when>
        </choose>
    </sql>

 注意:最好基于单表定义来定义sql片段

                不要用where

IF

<select id="queryBlogIF" parameterType="map" resultType="com.song.pojo.Blog">
        select * from blog where 1=1
        <if test="title !=null">
            and title=#{title}
        </if>
        <if test="author!=null">
            and author=#{author}
        </if>
    </select>

choose,when,otherwise(Java中的switch,case,default)

<select id="findActiveBlogLike"
     resultType="Blog">
  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>
</select>

trim(where,set)

where

where会自动排错,无论是多了and或or 还是where后没有语句他都会自动检测并且纠正

<select id="findActiveBlogLike"
     resultType="Blog">
  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>
</select>

SET

<update id="updateAuthorIfNecessary">
  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}
</update>

trim自定义

和where一样的标签:

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

和set一样的标签

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

Foreach

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>

例子

<foreach collection="userlist" item="uid" 
                 open="(" separator="or" close=")">
            id=#{uid}
        </foreach>

缓存(cache)

        所有的查询都要连接数据库,较耗资源

        可以将一次查询的结果暂存到一个可以直接取到的地方

        放到内存里的这些数据就叫缓存(cache)

        再次查询相同数据时直接走缓存,不走数据库了

        解决高并发系统的性能问题

为什么使用缓存:

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

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

        经常查询且不经常改变的数据

        主从复制

一、什么是主从复制?

主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库,主数据库一般是准实时的业务数据库。您看,像在mysql数据库中,支持单项、异步赋值。在赋值过程中,一个服务器充当主服务器,而另外一台服务器充当从服务器。此时主服务器会将更新信息写入到一个特定的二进制文件中。并会维护文件的一个索引用来跟踪日志循环。这个日志可以记录并发送到从服务器的更新中去。当一台从服务器连接到主服务器时,从服务器会通知主服务器从服务器的日志文件中读取最后一次成功更新的位置。然后从服务器会接收从哪个时刻起发生的任何更新,然后锁住并等到主服务器通知新的更新
 

Mybatis缓存

默认情况只有一级缓存(本地缓存)即在sqlsession从创建到close有效

二级缓存:需要手动开启,基于namespace级别的缓存(一个mapper对应一个namespace)

一级缓存:

在一个session中查询两次相同的语句时,第二次查询直接从缓存内取,取到的对象即为第一次取到的对象(相同地址)

缓存失效的情况:

1、查询不同的数据

2、增删改操作(可能会改变原数据,所以必定会刷新缓存)

3、手动清理缓存

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

一级缓存默认是开启的,无法关闭,但可以手动清理

二级缓存

工作机制

  • 一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中;
  • 如果当前会话关闭了,这个会话对应的一级缓存就没了,但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中;
  • 新的会话查询信息,就可以从二级缓存中获取内容
  • 不同的mapper查询的数据会放在自己对应的缓存中(map)中

若想使用全局缓存,只需要在Mapper.XML文件中加入<cache/>标签即可

 流程:

1.开启全局缓存(在全局配置文件中mybatis-config.cml)

<settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>

2.在Mapper.xml的mapper标签内加入<cache/>标签或做如下配置(也可以自定义参数)

<cache
            eviction="FIFO"
            flushInterval="60000"
            size="512"
            readOnly="true"/>

 3.测试

 结果

问题:

        1、需要将实体类序列化,否则会报错

序列化:即实现Serializable接口

总结 

         开启了二级缓存,在一个mapper对象下都有效

        所有的数据都会先放到一级缓存中,只有当session会话提交或关闭(close)后才提交到二级缓存

缓存原理

先看二级缓存有没有,再看二级缓存有没有,都没有再查询数据库

自定义缓存Ehcache

 Ehcache框架,进程内缓存框架

在程序中使用

1、导包

<!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.1.0</version>
</dependency>

2、在Mapper.xml中配置

<cache
    type="org.mybatis.caches.ehcache.EhcacheCache"    
/>

3.创建ehcache.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="./tmpdir/Tmp_EhCache"/>

    <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"/>
    <!--
       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,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
   -->
</ehcache>

一般用数据库Redis(非关系型数据库)做缓存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值