首先肯定还是引入mybatis依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>

在启动类处扫描mybatis的mapper类

在这里插入图片描述

写一个mapper接口类

定义好查询方法
在这里插入图片描述

在resources下编写mapper接口类对应的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--上面是头部命名空间-->
<!--mapper开始就是自己定义的了-->
<mapper namespace="com.chan.wechatshop.dataobject.mapper.ProductCategoryMapper">    <!--对应的mapper类的包路径-->

    <resultMap id="baseResultMap" type="com.chan.wechatshop.dataobject.ProductCategory"> <!--返回查询结果对应的实体类-->
        <id column="category_id" property="categoryId" jdbcType="INTEGER"/> <!--这里的id代表主键-->
        <id column="category_name" property="categoryName" jdbcType="VARCHAR"/>
        <id column="category_type" property="categoryType" jdbcType="INTEGER"/>
    </resultMap>

    <select id="selectByCategoryType" resultMap="baseResultMap" parameterType="java.lang.Integer"> <!--传参是对象的话parameterType就写那个对象的路径,这边是int的type-->
        select category_id,
        category_name,
        category_type,
        create_time,
        update_time
        from product_category
        where category_type = #{category_type, jdbcType=INTEGER }
    </select>

</mapper>

在这里插入图片描述

在yml中配置,可以扫描到xml文件

mybatis:
  mapper-locations: classpath:mapper/*.xml    #配置mybatis扫描mapper文件xml的路径

在这里插入图片描述

传入List类型数据在xml中的拼接

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

xml中的<![CDATA[

因为mybatisxml本质还是属于xml的,所以xml语法中的
<![CDATA[的作用也可以带过来,有些mybatis无法解析的东西,可以使用
<![CDATA[ ]]>包一下,代表这一块内容不做转义

mapper对应的xml中的if语句的使用

可以看到类似这种foreachif这种mybatis方法中使用变量的时候是不需要使用#{}包裹的,直接写变量名就行
在这里插入图片描述

在这里插入图片描述

使用map-underscore-to-camel-case/db-column-underline在插入数据的时候将实体类的驼峰格式转成下划线格式

只要设置db-column-underlinemap-underscore-to-camel-case任意一个参数为true,实体类的字段都会自动转下划线的格式.

map-underscore-to-camel-case > db-column-underline 为了歧义新版去掉 db-column-underline

mybatis-plus:
	global-config:
		db-column-underline: true
	configuration:
    	map-underscore-to-camel-case: true	

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

Mybatis 在 insert 插入操作后返回主键 id

方法一

配置 useGeneratedKeyskeyProperty

  • useGeneratedKeys="true" 表示给主键设置自增长。
  • keyProperty="sid" 表示将自增长后的 Id 赋值给实体类中的 sid 字段。
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="sid">
    insert into student(name, age)
    VALUES (#{name} , #{age})
</insert>

方法二
insert 标签中编写 selectKey 标签

<insert id="insertStudent" parameterType="Student">
    insert into student(name, age)
    VALUES (#{name} , #{age})

    <selectKey keyProperty="sid" order="AFTER" resultType="int">
        SELECT LAST_INSERT_ID()
    </selectKey>
</insert>
  • < insert> 标签中没有 resultType 属性,但是 < selectKey> 标签是有的。
  • order="AFTER" 表示先执行插入语句,之后再执行查询语句。
  • keyProperty="sid" 表示将自增长后的 Id 赋值给实体类中的 sid 字段。
  • SELECT LAST_INSERT_ID() 表示 MySQL 语法中查询出刚刚插入的记录自增长 Id

方法三

这种方法需要在一定条件下才能使用,就是 name(也可以是其他唯一字段如订单号) 需要是 unique,不可重复的。

<insert id="insertStudent" parameterType="Student">
    insert into student(name, age)
    VALUES (#{name} , #{age})

    <selectKey keyProperty="sid" order="AFTER" resultType="int">
        select sid from student where name = #{name}
    </selectKey>
</insert>

原理和上面查id是一样的,就是在执行插入语句之后,再执行查询语句,将 sid 查出来

bind标签

<select id="selectUserByBind" resultType="com.po.MyUser" parameterType= "com.po.MyUser">
    <!-- bind 中的 uname 是 com.po.MyUser 的属性名-->
    <bind name="paran_uname" value="'%' + uname + '%'"/>
        select * from user where uname like #{paran_uname}
</select>
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐