网站首页 > 教程文章 正文
首先肯定还是引入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[
因为mybatis的xml本质还是属于xml的,所以xml语法中的
<![CDATA[的作用也可以带过来,有些mybatis无法解析的东西,可以使用
<![CDATA[ ]]>包一下,代表这一块内容不做转义
mapper对应的xml中的if语句的使用
可以看到类似这种foreach和if这种mybatis方法中使用变量的时候是不需要使用#{}包裹的,直接写变量名就行
使用map-underscore-to-camel-case/db-column-underline在插入数据的时候将实体类的驼峰格式转成下划线格式
只要设置db-column-underline与map-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
方法一
配置 useGeneratedKeys 和 keyProperty
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
来源:https://blog.csdn.net/weixin_43944305/article/details/106300945
猜你喜欢
- 2024-12-28 《黑屏死机》 电脑黑屏死机
- 2024-12-28 SQL轻松入门(1):增删改与简单查询
- 2024-12-28 【数据库】Upsert = Update or Insert
- 2024-12-28 SQL查询语句大全(一) sql查询语句命令大全
- 2024-12-28 大数据开发基础之SQL语句基本操作
- 2024-12-28 SQL CASE WHEN的用法 sql case when 的用法详解
- 2024-12-28 高并发下如何防重? 如何防止高并发
- 2024-12-28 最全汇总|SQL Server与Access数据库sql语法差异
- 2024-12-28 SQL left join 左表合并去重技巧总结
- 2024-12-28 为什么一条UPDATE语句有索引反而更慢
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)