MyBatis
MyBatis特性
- MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架
- MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集
- MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录
- MyBatis 是一个 半自动的ORM(Object Relation Mapping)框架
下载
下载地址


持久化层技术对比
- JDBC
- SQL 夹杂在Java代码中耦合度高
- 维护不易且实际开发需求中 SQL 有变化,频繁修改的情况多见
- 代码冗长,开发效率低
- Hibernate 和 JPA
- 操作简便,开发效率高
- 程序中的长难复杂 SQL 需要绕过框架
- 内部自动生产的 SQL,不容易做特殊优化
- 基于全映射的全自动框架,大量字段的 POJO 进行部分映射时比较困难
- 反射操作太多,导致数据库性能下降
- MyBatis
- 轻量级,性能出色
- SQL 和 Java 编码分开,功能边界清晰。Java代码专注业务、SQL语句专注数据
- 开发效率稍逊于HIbernate,但是完全能够接受
开发环境
idea:2019.3.5
MySQL:8.0
MyBatis:3.5.10
Maven:3.6.1
开始写代码

1.创建Maven工程
2.配置打包方式为jar
3.引入依赖
pom.xml- <dependencies>
-
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.5.10</version>
- </dependency>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.30</version>
- </dependency>
- </dependencies>
复制代码 4.mybatis配置文件
mybatis-config.xml5.创建数据库建表(略)并在Java代码中创建对应实体类
Users.java- package com.xust.mybatis.pojo;
- public class Users {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private Integer id;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String username;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String password;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private Integer age;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String sex;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String email;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public Users() {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public Users(Integer id, String username, String password, Integer age, String sex, String email) {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.id = id;
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.username = username;
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.password = password;
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.age = age;
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.sex = sex;
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.email = email;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public Integer getId() {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>return id;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void setId(Integer id) {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.id = id;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public String getUsername() {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>return username;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void setUsername(String username) {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.username = username;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public String getPassword() {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>return password;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void setPassword(String password) {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.password = password;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public Integer getAge() {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>return age;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void setAge(Integer age) {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.age = age;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public String getSex() {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>return sex;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void setSex(String sex) {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.sex = sex;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public String getEmail() {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>return email;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void setEmail(String email) {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>this.email = email;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Override
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public String toString() {
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>return "Users{" +
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>"id=" + id +
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>", username='" + username + '\'' +
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>", password='" + password + '\'' +
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>", age=" + age +
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>", sex='" + sex + '\'' +
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>", email='" + email + '\'' +
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>'}';
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 6.创建mapper接口(不需要实现类)
UserMapper.java- package com.xust.mybatis.mapper;
- /**
- * mybatis面向接口编程两个一致
- * 1.映射文件的namespace要和mapper的全类名保持一致
- * 2.映射文件中SQL语句的id要和mapper接口中的方法名一致
- */
- public interface UserMapper {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>/**
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins> * 添加用户信息
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins> */
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>int insertUser();
- }
复制代码 7.创建MyBatis映射文件
- 映射文件的namespace要和mapper的全类名保持一致
- 映射文件中SQL语句的id要和mapper接口中的方法名一致
UserMapper.xml- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.xust.mybatis.mapper.UserMapper">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><insert id="insertUser">
- insert into t_user values(null,'张三','123',23,'女','123456@qq.com')
- </insert>
- </mapper>
复制代码 8.junit测试
SqlSession:代表Java程序和数据库之间的会话。HttpSession是Java程序和浏览器之间的会话
SqlSessionFactory:是生产SqlSession的工厂
MyBatisTest.java上面是手动提交事务,SqlSession默认不自动提交事务,自动提交可以在获取sqlSession对象时,使用SqlSession sqlSession = sqlSessionFactory.openSession(true),传入一个Boolean类型的参数,值为true,这样就可以自动提交。
9.加入log4j日志功能
引入依赖- <dependency>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><groupId>log4j</groupId>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><artifactId>log4j</artifactId>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><version>1.2.17</version>
- </dependency>
复制代码 log4j.xml- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><appender name="STDOUT" >
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><param name="Encoding" value="UTF-8" />
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><layout >
- <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings></layout>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins></appender>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><logger name="java.sql">
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><level value="debug" />
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins></logger>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><logger name="org.apache.ibatis">
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><level value="info" />
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins></logger>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><root>
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><level value="debug" />
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><appender-ref ref="STDOUT" />
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins></root>
- </log4j:configuration>
复制代码日志的级别:FATAL(致命)>ERROR(错误)>WARN(警告)>INFO(信息)>DEBUG(调试)
日志级别越低,打印越详细
增删改查
查询功能必须指定resultType或resultMap
resultType:设置默认映射(字段名和属性名一致)
resultMap:设置自定义映射(一对多,多对一,字段名和属性名不一致)
MyBatis核心配置文件
标签顺序
properties、settings、typeAliases、typeHandlers、objectFactory、objectWrapperFactory、reflectorFactory、plugins、environments、databaseIdProvider、mappers
mybatis-config.xmljdbc.properties- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/mybatis
- jdbc.username=root
- jdbc.password=xxxxxxx
复制代码 idea中设置核心配置文件模板

MyBatis中mapper获取参数值的两种方式
${}:字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号
{}:使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号
单个字面量类型的参数
{}中username可替换为任意值,${}要手动添加''
Test.java- public class ParameterTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetUserByUsername(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Users user=mapper.GetUserByUsername("李四");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(user);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 #{}- <select id="GetUserByUsername" resultType="users">
- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select> where username=#{username}
- </select>
复制代码 ${}- <select id="GetUserByUsername" resultType="users">
- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select> where username='${username}'
- </select>
复制代码 多个字面量类型的参数
若mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合中
- 以arg0,arg1…为键,以参数为值;
- 以param1,param2…为键,以参数为值;
{}中必须按照上面的参数写
Test.java- public class ParameterTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testCheckLogin(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Users user=mapper.checkLogin("李四","123");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(user);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 #{}- <select id="checkLogin" resultType="users">
- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select> where username=#{arg0} and password=#{arg1}
- </select>
复制代码 ${}- <select id="checkLogin" resultType="User">
- select * from t_user where username ='${param1}' and password ='${param2}'
- </select>
复制代码 map集合类型的参数
mapper接口中的方法需要的参数为多个时,此时可以手动创建map集合,将这些数据放在map中只需要通过${}和#{}访问map集合的键就可以获取相对应的值
Test.java- public class ParameterTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testCheckLoginByMap(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Map<String,Object> map=new HashMap<>();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>map.put("username","李四");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>map.put("password","123");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Users user=mapper.checkUserByMap(map);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(user);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 #{}- <select id="GetUserByUsername" resultType="users">
- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select> where username=#{username}
- </select> and password=#{password}
复制代码 ${}略
实体类类型的参数
通过访问实体类对象中的属性名获取属性值
Test.java- public class ParameterTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testInsertUser(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>int result=mapper.insertUser(new Users(null,"王五","123",23,"男","123@q.com"));
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(result);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 #{}- <insert id="insertUser">
- <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
- </insert>
- </insert>
复制代码 ${}略
使用@Param注解命名参数
可以通过@Param注解标识mapper接口中的方法参数,此时,会将这些参数放在map集合中
- 以@Param注解的value属性值为键,以参数为值
- 以param1,param2…为键,以参数为值
Test.java- public class ParameterTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testChesckLoginByParam(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Users user=mapper.checkLoginByParam("李四","123");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(user);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 mapper.java- Users checkLogdinByParam(@Param("username") String username,@Param("password") String password);
复制代码 mapper.xml- <select id="GetUserByUsername" resultType="users">
- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select> where username=#{username}
- </select> and password=#{password}
复制代码 MyBatis查询功能
查询一个实体类对象
- Users getUserById(@Param("id") Integer id);
复制代码- <select id="getUserById" resultType="users">
- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select> where id=#{id}
- </select>
复制代码- public class SelectMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void getUsereById(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(mapper.getUserById(3));
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 查询List集合
- List<Users> getAllUser();
复制代码- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select>
复制代码- public class SelectMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void getAllUsers(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(mapper.getAllUser());
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 查询单个数据
- <select id="getCount" resultType="java.lang.Integer">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select count(*) from t_user
- </select>
复制代码- public class SelectMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetCount(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(mapper.getCount());
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 查询一条数据为map集合
- Map<String,Object> getUserByIdToMap(@Param("id") Integer id);
复制代码- <select id="getUserById" resultType="users">
- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select> where id=#{id}
- </select>
复制代码- public class SelectMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetUserByIdToMap(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(mapper.getUserByIdToMap(3));
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 查询多条数据为map集合
法一:
- List<Map<String,Object>> getAllUserToMap();
复制代码- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select>
复制代码- public class SelectMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetAllUserToMap(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(mapper.getAllUserToMap());
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 法二:
- @MapKey("id")
- Map<String,Object> getAllUserToMap1();
复制代码- <select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select>
复制代码- public class SelectMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetAllUserToMap1(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(mapper.getAllUserToMap1());
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 特殊SQL的执行
模糊查询
第三种方法最常用
- List<Users> getUserByLike(@Param("username") String username);
复制代码- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><select id="getAllUser" resultType="users">
- <select id="getAllUserToMap" resultType="map">
- <select id="getAllUserToMap1" resultType="map">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_user
- </select>
- </select>
- </select> where username like "%"#{username}"%"
复制代码- public class SQLMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetUserByLike(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>List<Users> list=mapper.getUserByLike("a");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(list);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 批量删除
- int deleteMore(@Param("ids") String ids);
复制代码- <delete id="deleteMore">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>delete from t_user where id in (${ids})
- </delete>
复制代码- public class SQLMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testDeleteMore(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>int result=mapper.deleteMore("1,2,3");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(result);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 动态设置表名
- List<Users> getUserByTableName(@Param("tableName") String tableName);
复制代码- <select id="getUserByTableName" resultType="users">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from ${tableName}
- </select>
复制代码- public class SQLMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetUserByTableName(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>List<Users> list=mapper.getUserByTableName("t_user");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(list);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 添加功能获取自增的主键
在插入一条数据的同时就可以获得对应的id,不需要插入后再去查询对应数据的id
- useGeneratedKeys:设置使用自增的主键
- keyProperty:因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参数user对象的某个属性中
- void insertUser(Users user);
复制代码- <insert id="insertUser">
- <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
- </insert>
- </insert>
复制代码- public class SQLMapperTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testInsertUser(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Users user=new Users(null,"赵六","123",23,"男","123@qq.com");
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>mapper.insertUser(user);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(user);
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 自定义映射resultMap
字段名与属性名不一致
数据库

Emp.java- public class ResultMapTest {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetAllEmp(){
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>EmpMapper mapper=sqlSessaion.getMapper(EmpMapper.class);
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>List<Emp> list=mapper.getAllEmp();
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>list.forEach(emp-> System.out.println(emp));
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}
- }
复制代码 方法一:为字段起别名,使之与属性名称相同
EmpMapper.xml- <select id="getAllEmp" resultType="emp">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select eid,emp_name empNaem,age,sex,email from t_emp
- </select>
复制代码 方法二:配置映射
在mybatis-config.xml中添加配置后自动将下划线映射为驼峰
mybatis-config.xml- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码 EmpMapper.xml- <select id="getAllEmp" resultType="emp">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_emp
- </select>
复制代码 方法三:自定义映射resultMap
所有属性都要列出来
EmpMapper.xml- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><select id="getAllEmp" resultType="emp">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_emp
- </select>
复制代码 多对一映射
方法一:级联操作
Emp.java- public class Emp {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private Integer eid;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String empName;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private Integer age;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String sex;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String email;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private Dept dept;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>......
- }
复制代码 Dept.java- package com.xust.mybatis.pojo;
- public class Dept {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private Integer did;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String deptName;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>......
- }
复制代码 EmpMapper.xml- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.eid=#{eid}
复制代码 方法二:association
association用来处理多对一的映射关系
javaType中是类型名
EmpMapper.xml- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
复制代码 方法三:分步查询
EmpMapper.xml- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>select * from t_emp where eid=#{eid}
复制代码 DeptMapper.xml- <select id="getEmpAndDeptByStepTwo" resultType="Dept">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_dept where did=#{did}
- </select>
复制代码- public class ResultMapTest {<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetEmpAndDeptByStep(){<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>EmpMapper mapper=sqlSessaion.getMapper(EmpMapper.class);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Emp emp=mapper.getEmpAndDeptByStepOne(1);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(emp);<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}}
复制代码 一对多映射
Dept.java- public class Dept {
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private Integer did;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private String deptName;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>private List<Emp> emps;
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>......
- }
复制代码 方法一:collection
collection:用来处理一对多的映射关系
ofType:表示该属性对饮的集合中存储的数据的类型
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_dept left join t_emp on t_dept.did=t_emp.did where t_dept.did=#{did}
复制代码- public class ResultMapTest {<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetDeptAndEmp(){<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>DeptMapper mapper=sqlSessaion.getMapper(DeptMapper.class);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Dept dept=mapper.getDeptAndEmp(1);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(dept);<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}}
复制代码 方法二:分步查询
DeptMapper.xml- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>select * from t_dept where did=#{did}
复制代码 EmpMapper.xml- <select id="getAllEmp" resultType="emp">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_emp
- </select> where did=#{did}
复制代码- public class ResultMapTest {<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetDeptAndEmpByStep(){<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>DeptMapper mapper=sqlSessaion.getMapper(DeptMapper.class);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Dept dept=mapper.getDeptAndEmpByStepOne(1);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(dept);<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}}
复制代码 动态SQL
Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题
if标签
if标签可以对test中接收到的属性值进行判断,为true则拼接if标签下的内容
where后加1=1为了更好实现拼接效果,防止出现拼接错误
- List<Emp> getEmpByCondition(Emp emp);
复制代码- <select id="getAllEmp" resultType="emp">
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>select * from t_emp
- </select> where 1=1<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>and emp_name=#{empName}<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>and age=#{age}<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>and sex=#{sex}<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>and email=#{email}<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
复制代码- public class DynamicSQLMapperTest {<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testGetEmpByCondition(){<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>DynamicSQLMapper mapper=sqlSessaion.getMapper(DynamicSQLMapper.class);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>List list=mapper.getEmpByCondition(new Emp(null,"张三",12,"男","123@qq.com"));<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(list);<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}}
复制代码 where标签
相比直接使用if标签,where标签会自动清除多余的and和or,条件全不满足时where不起作用,不会出现拼接错误
但是where标签只能将内容之前多余的and和or清除,如果and和or放在拼接的语句之后,则不能清除
trim标签
prefix/suffix:将trim标签中内容前面/后面添加指定内容
prefixOverrides/suffixOverrides:将trim标签中内容前面/后面去掉指定内容
choose,when,otherwise标签
相当于if...else if...else,只会执行一个
when至少要有一个,otherwise至多只有一个
foreach标签
collection:设置要循环的数组或集合
item:表示集合或数组中的每一个数据
separator:设置循环体之间的分隔符,分隔符前后默认有一个空格
open:开始符
close:结束符
- int deleteMoreByArray(@Param("eids") Integer[] eids);
复制代码- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>delete from t_emp where eid in<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>#{eid}<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码- public class DynamicSQLMapperTest {<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testDeleteMoreByArray(){<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>DynamicSQLMapper mapper=sqlSessaion.getMapper(DynamicSQLMapper.class);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>int result=mapper.deleteMoreByArray(new Integer[]{6,7,8});<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(result);<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}}
复制代码 批量删除
,换成or
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>delete from t_emp where eid in<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>#{eid}<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码 批量添加
- int insertMoreByList(@Param("emps") List<Emp> emps);
复制代码- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>insert into t_emp values<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>(null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
复制代码- public class DynamicSQLMapperTest {<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void insertMoreByList() {<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>SqlSession sqlSession = SqlSessionUtils.getSqlSession();<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Emp emp1 = new Emp(null,"a",1,"男","123@qq.com");<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Emp emp2 = new Emp(null,"b",1,"男","123@321.com");<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>Emp emp3 = new Emp(null,"c",1,"男","123@321.com");<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>List emps = Arrays.asList(emp1, emp2, emp3);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>mapper.insertMoreByList(emps);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>System.out.println(emps);<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}}
复制代码 SQL标签
sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入
- <sql id="empColumns">eid,emp_name,age,sex,email</sql>
- <select id="getEmpByCondition" resultType="Emp">
- select <include refid="empColumns"></include> from t_emp
- </select>
复制代码 MyBatis缓存
MyBatis一级缓存
一级缓存是SqlSession级别的,通过同一个SqlSession查询的数据会被缓存,下次查询相同的数据,就会从缓存中直接获取,不会从数据库重新访问
使一级缓存失效的四种情况:
- 不同的SqlSession对应不同的一级缓存
- 同一个SqlSession但是查询条件不同
- 同一个SqlSession两次查询期间执行了任何一次增删改操作
- 同一个SqlSession两次查询期间手动清空了缓存
MyBatis二级缓存
二级缓存是SqlSessionFactory级别,通过同一个SqlSessionFactory创建的SqlSession查询的结果会被缓存;此后若再次执行相同的查询语句,结果就会从缓存中获取
二级缓存开启的条件:
- 在核心配置文件中,设置全局配置属性cacheEnabled=“true”,默认为true,不需要设置
- 在xml映射文件中设置标签
- 二级缓存必须在SqlSession关闭或提交之后有效
- 查询的数据所转换的实体类类型必须实现序列化的接口
- 使二级缓存失效的情况:两次查询之间执行了任意的增删改,会使一级和二级缓存同时失效
二级缓存相关配置
在mapper配置文件中添加的cache标签可以设置一些属性:
eviction属性:缓存回收策略
- LRU(Least Recently Used) – 最近最少使用的:移除最长时间不被使用的对象。
- FIFO(First in First out) – 先进先出:按对象进入缓存的顺序来移除它们。
- SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。
- WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。
- 默认的是 LRU
flushInterval属性:刷新间隔,单位毫秒
- 默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句(增删改)时刷新
size属性:引用数目,正整数
- 代表缓存最多可以存储多少个对象,太大容易导致内存溢出
readOnly属性:只读,true/false
- true:只读缓存;会给所有调用者返回缓存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。
- false:读写缓存;会返回缓存对象的拷贝(通过序列化)。这会慢一些,但是安全,因此默认是false
MyBatis缓存查询顺序
- 先查询二级缓存,因为二级缓存中可能会有其他程序已经查出来的数据,可以拿来直接使用
- 如果二级缓存没有命中,再查询一级缓存
- 如果一级缓存也没有命中,则查询数据库
- SqlSession关闭之后,一级缓存中的数据会写入二级缓存
整合第三方缓存
向pom.xml中添加依赖
- <dependency>
- <groupId>org.mybatis.caches</groupId>
- <artifactId>mybatis-ehcache</artifactId>
- <version>1.2.1</version>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- <version>1.2.3</version>
- </dependency>
复制代码 resources下创建ehcache.xml文件
- <settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码 设置二级缓存类型
mapper.xml的cache标签中- [/code][size=3]加入logback日志[/size]
- [code]<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
复制代码 MyBatis逆向工程
正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的。
逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成Java实体类,Mapper接口,Mapper映射文件。
创建逆向工程的步骤
1.添加依赖和插件
pom.xml2.创建MyBatis核心配置文件
Mybatis.xml3.将log4j.xml和jdbc.properties复制过来
4.创建逆向工程配置文件
文件名必须是generatorConfig.xml
MyBatis3Simple: 生成基本的CRUD
MyBatis3: 生成带条件的CRUD(使用最多)
generatorConfig.xml5.执行MBG插件的generate目标
双击mybatis-generator:generate
6.通过Example进行查询演示
分页插件
步骤
添加依赖
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper</artifactId>
- <version>5.2.0</version>
- </dependency>
复制代码 配置插件
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
复制代码 开启分页功能
- public class PageHelperTest {<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>@Test<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>public void testPageHelper(){<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>try {<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>InputStream is= Resources.getResourceAsStream("mybatis-config.xml");<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>SqlSession sqlSession=sqlSessionFactory.openSession(true);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>EmpMapper mapper=sqlSession.getMapper(EmpMapper.class);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>PageHelper.startPage(1,4);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>List list = mapper.selectByExample(null);<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>list.forEach(emp-> System.out.println(emp));<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>} catch (IOException e) {<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>e.printStackTrace();<settings>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>}<plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
- </plugins>}}
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |