网站首页 > 教程分享 正文
首先肯定还是引入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-09-09 SQL Server优化50法(sql server 优化)
- 2024-09-09 SQLServer-高级篇(sqlserver ag)
- 2024-09-09 2022-12-17:订单最多的客户。以下数据,结果输出3。请问sql语句
- 2024-09-09 Qt的数据库(Driver类、Query类、Model类、View类)
- 2024-09-09 VBA+ADO+SQL语句,小试牛刀。(vba的sql)
- 2024-09-09 MS SQL Server——SQL语句导入导出大全
- 2024-09-09 mysql根据条件执行sql(mysql根据条件查询)
- 2024-09-09 MyBatis3-动态SQL语句(navicat怎么写sql语句)
- 2024-09-09 SQL优化——IN和EXISTS谁的效率更高
- 2024-09-09 面试官:编写一个 SQL 查询,找出每个部门工资第二高的员工
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)