mybatis和mybatis-plus驼峰命名以及mapper.xml文件常用属性及标签

一、mybatis驼峰式命名

1.1、简述

mybatis驼峰式命名规则自动转换:

使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”;
使用好处:省去mapper.xml文件中繁琐编写表字段列表与表实体类属性的映射关系,即resultMap。
示例:

<resultMap id ="UserInfoMap" type="com.example.mybaitsxml.dao.entity.User">
    <result column="name_" property="name"/>
    <result column="sex" property="sex"/>
    <result column="age" property="age"/>
    <result column="class_no" property="classNo"/>
</resultMap>

我们来看一下加入驼峰命名法的好处
加入驼峰命名法后在进行sql查询和初始化实体时mybatis会为我们自动转化,写sql语句的时候也不必为有下划线的字段设置与实体类相同的别名。
如未加配置之前的sql查询语句为(使用别名):

select id, user_name as userName, user_sex as userSex, user_age as userAge from user

加入配置之后的sql语句为(直接使用数据库字段名):

select id, user_name, user_sex, user_age from user

SpringBoot整合mybatis,开启mybatis驼峰式命名规则自动转换,通常根据配置文件不同分为两种方式。

1.2、使用方法

1.2.1、方式一

直接application.yml文件中配置开启

#mybatis配置

mybatis:
  typeAliasesPackage: com.example.mybaitsxml.dao.entity
  mapperLocations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

在.properties文件中添加:

mybatis.configuration.map-underscore-to-camel-case=true 

但如果已经在.properties中配置了mybatis.config-location=classpath:mybatis/mybatis-config.xml这样的语句,就应该使用下一种方式,即把配置信息写在.xml中。

1.2.2、方式二

mybatis-config.xml文件中配置开启,application.yml文件指定配置文件。
application.yml文件:
#mybatis配置

mybatis:
  typeAliasesPackage: com.example.mybaitsxml.dao.entity
  mapperLocations: classpath:mapper/*.xml
  configLocation: classpath:/mybatis-config.xml

mybatis-config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!--开启驼峰命名规则自动转换-->
    <settings>
    <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

1.3、小结

第一点:

1.mybatis配置文件设置了这项后,查询出来的字段如果带下划线,那么就会去掉下划线,
然后采用java驼峰规则。

比如数据库字段parent_id,那么查询出来后,会转为parentId,然后去实体类Category匹配对应的字段。 这个时候实体类里就不能写有下划线,不然就匹配不上。会出现查询结果都是null值。

第二点:

1.当进行单表简单查询时且返回值类型是基本类型时,一般mapper的返回类型尽量使用resultType=”xxxx”;

2.当进行多表关联查询时,或者说xml中定义了相关的resultMap标签,那么就一般尽量使用resultMap=”xxxx”;

3.在mapper.xml当中,resultType和resultMap是不能同时使用。

第三点:

关于数据库字段和实体类名称不相同的时候,要么采用resultMap标签, 要么启用驼峰规则,但是两者不能同时使用。

二、mybatis-plus驼峰命名

数据库表字段为:last_name

实体类属性名为 lastName 

默认情况下 mybatis-plus会自动将实体类的属性名中的 大写字母 变成 小写 并加上 下换线_ (last_name)当做数据库表字段

如果实体类的属性名和数据库表的字段名没有遵循驼峰命名,那么每个实体类单独指定字段名

@Data
@TableName("tb_employee")
public class Employee  {
    
    
	、、、、、、
    @TableField(value = "lastName")
    private String lastName;
	、、、、、、
}

或者也可以自己配置文件转换利用,配置文件配置resultMap,关闭驼峰命名法的方法如下:

mybatis-plus:
  # MyBatis映射器文件的位置
  mapper-locations: classpath*:mapper/*Mapper.xml
  global-config:
    db-config:
      # 实体类属性名和数据库表字段名是否开启驼峰命名法
      table-underline: false

三、mapper.xml文件常用属性及标签

无论如何我们基本上都会写mapper.xml文件。下面对mapper.xml语法进行讲解

3.1、${}和#{}的区别

#{}会自动在你要插入字段两端 加上引号。例如:你写的是order by #{username},传的是 zhangsan,那么会解析成order by “zhangsan”。

${}是将传入的数据直接显示生成在sql中。如:order by ${user_id},如果传入的值是111,那么解析成sql时的值为order by 111 如果传入的值是id,则解析成的sql为order by id.

#{}: 解析为一个 JDBC 预编译语句(prepared statement)的参数标记符,一个 #{ } 被解析为一个参数占位符 。
$ {}: 仅仅为一个纯碎的 string 替换,在动态 SQL 解析阶段将会进行变量替换。在使用order by 时,就需要使用$;

3.2、常见的属性

在这里插入图片描述

3.3、常见标签

3.3.1、简述

< sql >标签
该标签主要定义复用的sql语句片段,在执行的sql语句标签直接引用即可。可以提高编码效率、简化代码和提高可读性。

需要配置id熟悉,表示该sql片段的唯一标识。

3.3.2、 if标签

< if >:条件判断标签,配置属性test=" 条件字符串 ",判断是否满足条件,满足则执行,不满足则跳过。
在mybatis中也能用 if 啦:

<select id="findUserById" resultType="user">
    select * from user where 
        <if test="id != null">
               id=#{id}
        </if>
    and deleteFlag=0;
</select>

3.3.3、 where标签

< where > : 主要用来替换sql语句中的where字段,他的作用主要是用来简化sql语句中where条件判断的书写的

 <select id="selectByParams" parameterType="map" resultType="user">
    select * from user
    <where>
      <if test="id != null ">id=#{id}</if>
      <if test="name != null and name.length()>0" >and name=#{name}</if>
      <if test="age != null and age.length()>0">and age = #{age}</if>
    </where>
  </select>   

如果当id值为空时,此时打印的sql应是:select * from user where name=“xx” and age=“xx”

where 标记会自动将其后第一个条件的and或者是or给忽略掉

3.3.4、 trim

< trim > : 是一个格式化的标记,可以完成set或者是where标记的功能。

   select * from user 

  <trim prefix="WHERE" prefixoverride="AND |OR">
    <if test="name != null and name.length()>0"> AND name=#{name}</if>
    <if test="age != null and age.length()>0"> AND age=#{age}</if>

  </trim>

假如说name和age的值都不为null的话打印的SQL为:select * from user where name = ‘xx’ and age = ‘xx’

在where的后面是不存在第一个and的,上面两个属性的意思如下:
  prefix:前缀      
  prefixoverride:去掉第一个and或者是or

示例2

  update user
  <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
    <if test="name != null and name.length()>0"> name=#{name} , </if>
    <if test="age!= null and age.length()>0"> age=#{age} ,  </if>
  </trim>

假如说name和age的值都不为null的话打印的SQL为:update user set name=‘xx’ , age=‘xx’ where id=‘x’

在age='xx’的后面不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,其中prefix意义如上:
   suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
   suffix:后缀

3.3.5、 set

< set > : 主要用来替换sql语句中的set字段,一般在update中使用。

  <update>
    update user 
    <set>
      	<if test="name != null and name.length()>0">name = #{name},</if>
      	<if test="age != null and age .length()>0">age = #{age },</if>
    </set>
    where id = #{id}
  </update> 

在上述的代码片段当中,假如说现在三个字段都有值得话,那么上面打印的SQL语句如下:

update user set name=‘xxx’ , age=‘xx’ where id=‘x’

在上面age="xx"的后是没有逗号的,也就是说set标记已经自动帮助我们把最后一个逗号给去掉了

set 标记会自动将其后第一个条件后的逗号忽略掉

3.3.6、 foreach:

java中有for, 可通过for循环, 同样, mybatis中有foreach, 可通过它实现循环,循环的对象当然主要是java容器和数组。

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

将一个 List 实例或者数组作为参数对象传给 MyBatis:当这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。

同样,当循环的对象为map的时候,index其实就是map的key。

3.3.7、 choose

Java中有switch, mybatis有choose。

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

以上例子中:当title和author都不为null的时候, 那么选择二选一(前者优先), 如果都为null, 那么就选择 otherwise中的, 如果tilte和author只有一个不为null, 那么就选择不为null的那个。

猜你喜欢

转载自blog.csdn.net/lijiaming_99/article/details/120863442