MyBatis开发文档:

mybatis - MyBatis 3​mybatis.org

mapper接口开发的四个规范:

XxxMapper.xml配置文件的名称命名空间值必须是mapper接口的全类名

  1. XxxMapper.xml配置文件中id值必须是接口中的方法名
  2. XxxMapper.xml配置文件中的parameterType参数类型必须和接口的方法参数类型一致
  3. XxxMapper.xml配置文件中的resultType返回值类型必须和接口的方法返回值类型一致(JavaBean才有需要)

遵守规范之后,可以不用写接口实现类

XML配置:

<settings>设置自动驼峰命名规则映射作用在于在数据库列名有下划线等特殊字符时,会自动去掉后的第二个首字母大写来取代别名的设置

没有设置:

类型别名(typeAliases)

类型别名是为 Java 类型设置一个短的名字。 它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余。例如:

<typeAliases><typeAlias alias="Author" type="domain.blog.Author"/><typeAlias alias="Blog" type="domain.blog.Blog"/><typeAlias alias="Comment" type="domain.blog.Comment"/><typeAlias alias="Post" type="domain.blog.Post"/><typeAlias alias="Section" type="domain.blog.Section"/><typeAlias alias="Tag" type="domain.blog.Tag"/>
</typeAliases>

也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,比如:

<typeAliases><package name="com.review.DynamicSql.domain"/>
</typeAliases>

XML映射文件:

1.#{}和${}的区别:

#{}是占位符 =====>>> ?

${}是把表示的参数值原样输出,然后和sql语句的字符串做拼接操作,${value}里面必须是value,不能是其他的值,除非设置param注解之后,写注解里的内容

代码如下:

//接口:
//根据姓名模糊查询
public List<User> queryUsersLikeName(String name);//xml配置文件:
<select id="queryUsersLikeName" resultType="user"><!--select id,last_name,sex from t_user where last_name like #{param1};-->select id,last_name,sex from t_user where last_name like '%${value}%';</select>//测试:
@Testpublic void testQueryUsersLikeName(){SqlSession session = sqlSessionFactory.openSession();UserDao mapper = session.getMapper(UserDao.class);try {String name="vin";mapper.queryUsersLikeName(name).forEach(System.out::println);}finally {session.close();}}

2.可以使用concat很好的解决模糊查询的问题

<select id="queryUsersLikeName" resultType="user">select id,last_name,sex from t_user where last_name like concat('%',#{name},'%");
</select>

3.resultMap标签的作用

<resultMap/>标签可以把可以把查询到的resultSet结果集转换成为复杂的JavaBean对象。

原来的resultType属性只能把查询到的结果集转换成为简单的javaBean对象

所谓简单的javaBean对象:是指它的属性里没有javaBean或集合类型的对象,反之亦然。

//代码举例如下:
//javaBean:
public class Key {private Integer id;private String name;private Lock lock;
}
public class Lock {private Integer id;private String name;
}//接口:public interface KeyMapper {public Key queryKeyByIdForSample(Integer id);
}//xml配置文件:
<mapper namespace="com.review.mybatisResultMap.dao.KeyMapper">
<!--    public Key queryKeyByIdForSample(Integer id);-->
<!--resultMap标签可以把查询到的resultSet结果集转换成为复杂的JavaBean对象type属性:设置resultMap需要转换出来的Bean对象的全类名id属性:设置一个唯一的标识,给别人应用--><resultMap id="queryKeyByIdForSample_resultMap" type="key">
<!--id标签负责把主键列转换到bean对象的属性column属性:设置将数据库中的哪个主键列转换到指定的对象属性中property属性:设置将值注入到哪个对象的属性中--><id column="id" property="id"></id><!--result标签负责把非主键列转换到bean对象的属性--><result column="name" property="name"></result><!--association标签映射子对象property属性:设置association配置哪个子对象javaType属性:设置association配置子对象的具体全类名--><association property="lock" javaType="lock"><id column="lock_id" property="id"></id><result column="lock_name" property="name"></result></association>
<!--将lock_id列注入到lock对象的id属性中写法:子对象.属性名     这种写法叫级联属性
-->
<!--        <result column="lock_id" property="lock.id"></result>-->
<!--        <result column="lock_name" property="lock.name"></result>--></resultMap><select id="queryKeyByIdForSample" resultMap="queryKeyByIdForSample_resultMap">select t_key.*,t_ock.name lock_namefrom t_key left join t_lockon t_key.lock_id=t_lock.idwhere t_key.id=1;</select>
</mapper>

4.<association />标签作用

<mapper namespace="com.review.mybatisResultMap.dao.LockMapper"><!--public Lock queryLockById(Integer id);--><select id="queryLockById" resultType="lock">select id,name from t_lock where id=#{id};</select>
</mapper>
<association />标签可以实现分步查询
<resultMap id="queryKeyByIdForStep_resultMap" type="key"><id column="id" property="id"></id><result column="name" property="name"></result><association property="lock" column="lock_id"
<!--select标签里的内容是:另外一张关联表对应接口的包路径.sql语句的id属性-->select="com.review.mybatisResultMap.dao.LockMapper.queryLockById"/></resultMap><select id="queryKeyByIdForStep" resultMap="queryKeyByIdForStep_resultMap">select id,name,lock_id from t_key where id=#{id};</select>
</mapper>

动态SQL

标签中的test条件的值是设置Param注解的属性名称

<if/>标签:用来判断一个条件是否成立,成立就执行

<where/>标签: 可以动态的去掉包含在内容前面的and、or关键字

<trim/> 标签:通过自定义 trim 元素来定制 where 元素的功能

trim的属性:

prefix 添加前缀

prefixOverrides 删除前缀

suffix 添加后缀

suffixOverrides 删除后缀

//Javabean类
public class User {private Integer id;private String lastName;private Integer sex;//接口/*** 根据名称和性别查询* 希望lastName值不能为Null,性别只能是0,1有效值*/public List<User> queryUserForNameAndSex(@Param("name") String name, @Param("sex") Integer sex);<mapper namespace="com.review.DynamicSql.dao.UserMapper">
<!--    /**-->
<!--    * 根据名称和性别查询-->
<!--    */-->
<!--    public List<User> queryUserForNameAndSex();--><select id="queryUserForNameAndSex" resultType="user">selectid,last_name,sexfromt_user<!--where标签可以动态的去掉包含在内容前面的and、or关键字if标签用来判断一个条件是否成立,成立就执行--><trim suffixOverrides="and" prefix="where"><if test="name!=null">last_name like concat('%',#{name},'%') and</if><if test="sex==0||sex==1">sex=#{sex};</if></trim></select>
</mapper>

<choose/>,<when/>,<otherwise/>标签

choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

<!--    /**-->
<!--    * 如果lastName有值,则使用它查询-->
<!--    * 如果lastName没有值,sex值有效,则使用sex值查询-->
<!--    * 否则,自己添加一个查询条件-->
<!--    */-->
<!--    public List<User> queryUserByNameAndSexChoose(User user);--><select id="queryUserByNameAndSexChoose" resultType="user">select id,last_name,sexfrom t_user<where><choose><when test="lastName!=null">last_name like concat('%',#{lastName},'%')</when><when test="sex==0||sex==1">sex=#{sex}</when><otherwise>last_name='jack' and sex=1</otherwise></choose></where></select>

<set>标签:删除条件后的逗号

<!--public int updateUser(User user);--><update id="updateUser" parameterType="user" >updatet_user<set><if test="lastName!=null">last_name=#{lastName},</if><if test="sex==1||sex==0">sex=#{sex}</if></set><where>id=#{id};</where></update>

<foreach/>标签:对一个集合进行遍历,通常是在构建 IN 条件语句的时候

<foreach/>属性值:

collection:你要遍历的数据源(必须是list)

item:表示当前遍历的数据,取一个名称

open:表示遍历之前添加的元素

close:表示遍历之后添加的元素

separator:给遍历的每个元素中间添加的内容

<!--    public List<User> queryUserByIdForeach(List<Integer> ids);--><select id="queryUserByIdForeach" resultType="user">selectid,last_name,sexfromt_userwhereid in<!--foreach遍历输出:collection:你要遍历的数据源(必须是list)item:表示当前遍历的数据,取一个名称open:表示遍历之前添加的元素close:表示遍历之后添加的元素separator:给遍历的每个元素中间添加的内容--><foreach collection="list" item="i" open="(" close=")" separator=",">#{i}</foreach></select>

Mybatis缓存

缓存的使用顺序说明:

  1. 每执行一次查询,首先会到二级缓存中去查看有没有数据,如果二级缓存中没有,再到一级缓存中去查看有没有数据
  2. 如果都没有,发送sql语句到数据库中去查询,把结果放到一级缓存中。
  3. 当关闭SqlSession的时候一级缓存同步到二级缓存中。

缓存:

  1. 缓存是指把经常需要读取的数据保存到一个高速缓冲区中,这个行为叫缓存。
  2. 缓存也可以是指被保存到高速缓冲区中的数据,也叫缓存。

一级缓存:是指把数据保存到SqlSession中

二级缓存:是指把数据保存到SqlSessionFactory中

对一级缓存的理解:在测试类中测试查询语句时,首先到一级缓存中去查询是否有该数据,如果缓存中有就直接返回,没有的话发送sql语句到数据库中去查询,把查到的结果保存在缓存中,下次查询该语句时,直接从缓存中拿数据返回。

一级缓存有失效的四种情况:

1.不在同一个SqlSession对象中

public void testFailCache1(){
//        不在同一个SqlSession对象中SqlSession session = sqlSessionFactory.openSession();try {UserMapper mapper = session.getMapper(UserMapper.class);System.out.println(mapper.queryUserById(2));}finally {session.close();}}@Testpublic void test1(){testFailCache1();testFailCache1();}

2.执行语句的参数不同.缓存中也不存在数据

@Testpublic void testFailCache2(){//执行语句的参数不同.缓存中也不存在数据SqlSession session = sqlSessionFactory.openSession();try {UserMapper mapper = session.getMapper(UserMapper.class);System.out.println(mapper.queryUserById(2));System.out.println(mapper.queryUserById(10));}finally {session.close();}}

3.执行增,删,改语句,会清空缓存

@Testpublic void testFailCache3(){//执行增,删,改语句,会清空缓存SqlSession session = sqlSessionFactory.openSession();try {UserMapper mapper = session.getMapper(UserMapper.class);System.out.println(mapper.queryUserById(2));mapper.updateUser(new User(12,"mark",0));session.commit();System.out.println(mapper.queryUserById(2));}finally {session.close();}

4.手动清空缓存数据

@Testpublic void testFailCache4(){//手动清空缓存数据SqlSession session = sqlSessionFactory.openSession();try {UserMapper mapper = session.getMapper(UserMapper.class);System.out.println(mapper.queryUserById(2));session.clearCache();System.out.println(mapper.queryUserById(2));}finally {session.close();}}

二级缓存的使用:

mybatis的二级缓存默认是不开启的

  1. 我们需要在mybatis的核心配置文件中配置setting选项
<setting name="cacheEnabled" value="true"/>

2.在Mapper的配置文件中加入cache标签

<cache/>

3.并且需要被二级缓存的对象必须要实现java的序列号接口

public class User implements Serializable {private Integer id;private String lastName;private Integer sex;
}

  • useCache属性的作用:在select标签中,useCache属性表示是否使用二级缓存。默认是true,使用二级缓存。(每次查询完后,也会把这个查询的数据放到二级缓存中)
<select id="queryUserById" resultType="user" useCache="true">select id,last_name,sex from t_user where id = #{id};</select>

  • update 的时候如果 flushCache="false",则当你更新后,查询的数据数据还是老的数据。因为是从缓存中拿的数据,并没有去数据库中查询来实时更新。flushCache默认是true。
<update id="updateUser" parameterType="user" flushCache="true">update t_user set last_name=#{lastName},sex=#{sex} where id=#{id};</update>@Testpublic void testTwoCache(){SqlSession session = sqlSessionFactory.openSession();try {UserMapper mapper = session.getMapper(UserMapper.class);System.out.println(mapper.queryUserById(2));mapper.updateUser(new User(2,"vans",0));session.commit();System.out.println(mapper.queryUserById(2));}finally {session.close();}}

mybatis逆向工程

MyBatis逆向工程,简称MBG。是一个专门为MyBatis框架使用者定制的代码生成器。可以快速的根据表生成对应的映射文件,接口,以及Bean类对象。

在MyBatis中,有一个可以自动对单表生成增、删、改、查代码的插件:mybatis-generator-core.

MyBatis逆向工程在Idea中的使用步骤:

  1. 新建一个maven工程,导入一下包
<dependencies><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version><scope>compile</scope></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.4</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies>

2.新建一个MyBatis Generator(MBG)的XML配置文件驱动,放在main目录下。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!--targetRuntime表示你要生成的版本MyBatis3:豪华版本MyBatis3Simple:CRUD标准版--><context id="DB2Tables" targetRuntime="MyBatis3Simple"><!--去掉全部注释--><commentGenerator><property name="suppressAllComments" value="true"/></commentGenerator><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/mybatis_mbg"userId="root"password="123456"></jdbcConnection><javaTypeResolver ><property name="forceBigDecimals" value="false" /></javaTypeResolver><!--javaModelGenerator配置生成JavaBeantargetPackage生成的javaBean的包名targetProject生成之后在哪个工程目录下--><javaModelGenerator targetPackage="com.review.mybatisMbg.domain" targetProject="src/main/java"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!--sqlMapGenerator生成sql的mapper.xml配置文件targetPackage生成的mapper.xml配置文件放的包名--><sqlMapGenerator targetPackage="com.mybatisMbg.mapper"  targetProject="src/main/resources"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!--配置生成的mapper接口--><javaClientGenerator type="XMLMAPPER" targetPackage="com.review.mybatisMbg.mapper"  targetProject="src/main/java"><property name="enableSubPackages" value="true" /></javaClientGenerator><!--一个table标签,一个表--><table tableName="t_user" domainObjectName="User" ></table><table tableName="t_book" domainObjectName="Book" ></table></context>
</generatorConfiguration>

3.导入mybatis-config.xml配置文件,放在resources资源目录下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><!--是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。--><setting name="mapUnderscoreToCamelCase" value="true"/><!--        全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存--><setting name="cacheEnabled" value="true"/></settings><!--别名设置--><typeAliases><package name="com.review.mybatisMbg.domain"/></typeAliases><!--配置环境--><environments default="mysql"><environment id="mysql"><!--配置事务类型--><transactionManager type="JDBC"></transactionManager><!--数据连接池--><dataSource type="POOLED"><!--配置连接数据库的四个信息--><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis_mbg"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><!--指定映射文件位置,映射文件是每个dao独立配置的文件--><mappers><mapper resource="com/mybatisMbg/mapper/BookMapper.xml"></mapper></mappers>
</configuration>

4.写一个java类,将转换执行代码写进去。File文件一定要写MyBatis Generator(MBG)的XML配置文件驱动名称

List<String> warnings = new ArrayList<String>();boolean overwrite = true;File configFile = new File("src/main/mbg.xml");ConfigurationParser cp = new ConfigurationParser(warnings);Configuration config = cp.parseConfiguration(configFile);DefaultShellCallback callback = new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);

mybatis trim标签_MyBatis学习笔记相关推荐

  1. mybatis 鉴别其_Mybatis学习笔记9 - 鉴别器discriminator

    鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为. 示例如下: DeptmentMapper接口定义: package com.mybatis.dao ...

  2. textarea中插入标签_HTMLCSS学习笔记(二)-- HTML表单标签

    表单 1 : 表单标签 <form></form> 属性 : action = '接口地址' method = 'get / post' name = '表单名称' 2 : 表 ...

  3. mybatis 入门([尚硅谷]_张晓飞_Mybatis 学习笔记一)

    目录 一.创建数据库 二.添加log4j日志 三.定义java类和配置xml 3.1 定义xml,xml 中的id作为操作sql的唯一标识 3.2 定义接口,接口方法作为操作sql 的唯一标识 一.创 ...

  4. 狂神说mysql笔记_Mybatis学习笔记(狂神说)

    简介 分享自写源码和笔记 配置用的 jdk13.0.2 (jdk1.7以上均可) Maven 3.6.3 MySQL 5.7 (mysql5.6以上均可) 1. 配置 pom.xml xmlns:xs ...

  5. mybatis oracle trim,Mybatis trim标签

    trim代替where/set标签 trim是更灵活用来去处多余关键字的标签,它可以用来实现where和set的效果. SELECT * FROM user u u.username LIKE CON ...

  6. SpringBoot和MyBatis集成案例(学习笔记)

    声明:本案例学习http://blog.csdn.net/je_ge,在此感谢je_ge提供的学习用的资料 1.项目目录结构 2.pom.xml的内容如下 <project xmlns=&quo ...

  7. mybatis collection标签_MyBatis第二天(结果映射+动态sql+关联查询)

    笑不出莱:MyBatis第一天(介绍+文件配置+Mapper动态代理)​zhuanlan.zhihu.com 一.导包+配置文件+pojo类 1.jar包:mybatis的核心包和依赖包+连接数据库的 ...

  8. JSTL标签库学习笔记

    JSTL概述 apache的东西,依赖于EL 使用jstl需要导入jstl.jar包 标签库 core 核心标签库 fmt 格式化标签库 sql 数据库标签库,过时 xml xml标签库,过时 JST ...

  9. EL表达式和JSTL标签库学习笔记

    先说一些数据库的知识,在进行表的操作时,若有一对多的关系,则在建外键时应选择多的一方.如user和group的对应关系,应在一个user属于多少个group,而不是一个group有多少用户,虽然这样也 ...

最新文章

  1. GoogleFusionTablesAPI初探地图与云计算
  2. NAND FLASH
  3. 转换图像分辨率c++代码_哈工大等提出轻量级盲超分辨模型LESRCNN,代码已开源...
  4. 深度学习解决多视图非线性数据特征融合问题
  5. 5.Redis 发布订阅
  6. [MySQL基础]MySQL常见命令介绍
  7. mysql慢查询开启语句分析_linux下开启mysql慢查询,分析查询语句
  8. mysql替换json的key_mysql中json_replace函数的使用?通过json_replace对json对象的值进行替换...
  9. 【2017年第4期】大数据平台的基础能力和性能测试
  10. .net mysql 参数,在MySQL .NET Provider中使用命名参数
  11. php fpm 平滑重启,nginx、php-fpm平滑重启和重载配置
  12. JavaScript:说看懂了就懂了闭包,看懂了,还是不懂...
  13. VS C++改变窗体背景色
  14. oracle修改只读数据库中,如何在oracle中创建只读数据库链接
  15. java输入日期计算天数_Java输入日期得到天数
  16. 洛谷p5710答案C语言,洛谷题单 101【入门2】分支结构
  17. 你不会因为实施了Scrum而变敏捷
  18. SAP ABAP BDC录屏 数据导入和检验-实例
  19. 强智教务系统验证码识别 OpenCV
  20. protobuf repeated数组类型的使用

热门文章

  1. 根据下拉框生成控件列表
  2. 软考经过 之 天助自助者
  3. linux下Vi编辑器命令大全(上)
  4. 中小企业网络结构设计1(华为版)
  5. 一刷leetcode——计算几何
  6. centos 7手动更改源
  7. 2017年9月19日
  8. 制造业采购审批流程设计示例
  9. sock_dgram 可以用listen吗_洗脸皂可以天天用吗
  10. 搜python编程题_100+Python编程题给你练~(附答案)