七、MyBatis参数处理
7.1 单个简单参数
parameterType表示传入参数的类型,官方文档有很多别名可以参考
<insert id="insertStu" parameterType="long">
insert into t_account(id,name,age,height,birth,sex)
values (null,#{name},#{age},#{height},#{birth},#{sex})
</insert>
7.2 多个参数
- 不用Param的情况下:
<!--可以填写arg0-->
<select id="selectByAgeAndSex" resultType="student">
select * from t_student where age=#{arg0} and sex=#{arg1}
</select>
/**
* 通过年龄和性别查学生
* @param age
* @param sex
* @return
*/
List<Student> selectByAgeAndSex(Integer age,Character sex);
- 使用Param的情况下:
<select id="selectByAgeAndSex" resultType="student">
select * from t_student where age=#{age} and sex=#{sex}
</select>
/**
* 通过年龄和性别查学生
* @param age
* @param sex
* @return
*/
List<Student> selectByAgeAndSex(@Param("age") Integer age,@Param("sex") Character sex);
- 简单总结
使用@Param(“别名”),多参数传参时可以直接输入别名
7.3 Param注解源码分析
八、查询结果专题
8.1 简单结果返回Car
8.2 返回List《Car》
8.3 返回Map
8.4 返回List《Map》
8.3 返回Map《String,Map》
/**
* 把数据表中所有数据返回给map嵌套
* 将查询结果的id值作为整个map集合的key
* @return
*/
@MapKey("id")
Map<String,Map<String,Object>> selectByAllRetMap();
/**结果:
* {
* 1={sex=男, name=张三, birth=1980-10-11, id=1, age=20, height=1.811},
* 2={sex=女, name=李四, birth=1988-10-11, id=2, age=20, height=1.61},
* 4={sex=男, name=哇嘎, birth=2022-10-17, id=4, age=20, height=1.8},
* 5={sex=女, name=瓦西, birth=2022-10-17, id=5, age=20, height=1.86}
* }
*/
8.4 结果映射ResultMap
property是pojo中的属性名称
column是数据表中的字段名
<!--结果映射-->
<resultMap id="stuResultMap" type="student">
<!--有主键就用id标签-->
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="height" column="height"/>
<result property="birth" column="birth"/>
<result property="sex" column="sex"/>
</resultMap>
<!--用resultMap就不需要用as了-->
<select id="selectAllByResultMap" resultMap="stuResultMap">
select * from t_student
</select>
8.5 开启驼峰式自动命名
<settings>
<!--开启驼峰式自动命名,即数据库中a_column转换成pojo的aColumn-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
九、动态SQL查询
9.1 if标签
<!--<if test="这里面为true表示if加载填入,反之不加载">-->
<select id="selectByMultiCases" resultType="car">
select * from t_car where 1=1
<if test="brand != null and brand != ''">
and brand like "%"#{brand}"%"
</if>
<if test="guidePrice != null and guidePrice != ''">
and guide_price > #{guidePrice}
</if>
<if test="carType != null and carType != ''">
and car_type = #{carType}
</if>
</select>
@Test
public void testSelectByMultiCases(){
SqlSession session = SqlSessionUtil.openSession();
CarMapper mapper = session.getMapper(CarMapper.class);
//这三个参数可以动态调整
List<Car> cars = mapper.selectByMultiCases("比亚迪", 20.0, "新能源");
cars.forEach(car -> System.out.println(car));
session.close();
}
9.2 where标签
- 如果没有条件成立,则不生成where子句
- 自动去除某些条件前面多余的and或or
9.3 Trim标签
<select id="selectByMultiCasesByTrim" resultType="car">
select * from t_car
<!--
在trim标签的前面或者后面->
prefix:增加前缀
suffix:增加后缀
prefixOverrides:删除前缀
suffixOverrides:删除后缀
-->
<trim prefix="where" prefixOverrides="and|or" suffixOverrides="and|or">
<if test="brand != null and brand != ''">
and brand like "%"#{brand}"%"
</if>
<if test="guidePrice != null and guidePrice != ''">
and guide_price > #{guidePrice}
</if>
<if test="carType != null and carType != ''">
and car_type = #{carType}
</if>
</trim>
</select>
9.4 set标签
- 主要使⽤在update语句当中,⽤来⽣成set关键字,同时去掉最后多余的“,”
- ⽐如我们只更新提交的不为空的字段,如果提交的数据是空或者"",那么这个字段我们将不更新。
<update id="updateById">
update t_car
<!--set标签可以去除末尾的逗号,当内部有if被执行的时候会自动在前面加上set标签-->
<set>
<if test="carNum != null and carNum != ''">car_num = #{carNum},</if>
<if test="brand != null and brand != ''">brand = #{brand},</if>
<if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if>
<if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if>
<if test="carType != null and carType != ''">car_type = #{carType},</if>
</set>
where id = #{id}
</update>
9.5 chose、when、otherwise
这三个标签是在⼀起使⽤的:
<choose>
<when></when>
<when></when>
<when></when>
<otherwise></otherwise>
</choose>
等同于
if(){
}else if(){
}else if(){
}else if(){
}else{
}
<select id="selectByChose" resultType="car">
select * from t_car
<where>
<choose>
<when test="brand != null and brand != ''">
brand like "%"#{brand}"%"
</when>
<when test="guidePrice != null">
guide_price > 2
</when>
<otherwise>
car_type = #{carType}
</otherwise>
</choose>
</where>
</select>
9.6 foreach标签
1.批量删除
<!--
foreach标签的属性:
collection:指定数组或者集合
item:代表数据或集合中的元素
separator:循环之间的分隔符
open:foreach循环拼接的所有sql语句的最前面以什么开始
close:foreach循环拼接的所有sql语句的最后面以什么结束
注意:要在接口处添加Param("ids")才能接收到ids
-->
<delete id="deleteByIds">
delete from t_car where id in
<foreach collection="ids" separator="," open="(" close=")" item="id">
#{id}
</foreach>
</delete>
2. 批量插入
<insert id="insertMatch">
insert into t_car values
<foreach collection="cars" item="car" separator=",">
(null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
</foreach>
</insert>
int insertMatch(@Param("cars") List<Car> cars);
9.7 sql标签与include标签
sql标签⽤来声明sql⽚段
include标签⽤来将声明的sql⽚段包含到某个sql语句当中
<sql id="carCols">
id,
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
</sql>
<select id="selectAllRetMap" resultType="map">
select <include refid="carCols"/> from t_car
</select>
十、MyBatis的高级映射及延迟加载
10.1 数据库表的准备
准备数据库表:⼀个班级对应多个学⽣。班级表:t_clazz。学⽣表:t_stu
10.2 多对一映射
判断哪个是主表的方法:哪个在前
比如多对一就是多在前,多个学生对应一个班级,此时学生就是主表
多对一如何在jvm里解释
将学生类中添加班级类即可
left join左连接,把左边的全部查出来,右边有的则匹配,没有则为null
记得要在表后面跟上代称,如t_stu s
select s.sid,s.sname,c.cid,c.cname
from t_stu s left join t_clazz c on s.cid=c.cid
where s.sid = 1
查询结果:
问题:根据ID查询学生信息,并返回班级信息
1. 第一种方式(级连属性映射)
<mapper namespace="com.powernode.mybatis.mapper.StuMapper">
<resultMap id="stuResultMap" type="student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<result property="clazz.cid" column="cid"/>
<result property="clazz.cname" column="cname"/>
</resultMap>
<select id="selectById" resultMap="stuResultMap">
select s.sid,s.sname,s.cid,c.cname from t_stu s left join t_clazz c on s.cid=c.cid where s.sid = #{sid}
</select>
</mapper>
2. 第二种方式(Association)
将resultMap中的类设置为Association(即另一个类)
<resultMap id="stuResultMapAssociation" type="student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<association property="clazz" column="clazz">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
</association>
</resultMap>
3. 第三种方式(分步查询)
两条SQL语句,分步查询。(这种方式常用:优点一是可复用。优点二是支持懒加载)
<!--分步查询-->
<resultMap id="stuResultMapStep" type="student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<!--
property:pojo类中的属性名称
select:执行select语句,给他传参column
column:给select传参
-->
<association property="clazz" select="com.powernode.mybatis.mapper.ClazzMapper.selectByCid" column="cid">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
</association>
</resultMap>
<select id="selectByIdStep" resultMap="stuResultMapStep">
select sid,sname,cid from t_stu where sid=#{sid}
</select>
4. 延迟加载(懒加载)
用到的时候加载,不用到的时候不加载
<!--在association中设置的仅是局部设置-->
<association fetchType="lazy">
....
一般在mybatis-config.xml核心配置文件中设置全局懒加载选项
<settings>
<!--开启全局延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
10.3 一对多映射原理
一对多:一个班级对应多个学生(班级是主表,学生是副表)
实现方法:在Clazz班级类中设置一个数组,这样可以容纳多个学生
1.第一种方式(collection方式)
在select中增加collection标签,表示集合
<resultMap id="clazzResultMapCollection" type="clazz">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
<!--
property:集合变量名称
ofType:集合中元素的名称
-->
<collection property="students" ofType="student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
</collection>
</resultMap>
<select id="selectByCidCollection" resultMap="clazzResultMapCollection">
select c.cid,c.cname,s.sid,s.sname from t_clazz c left join t_stu s on c.cid=s.cid where c.cid=#{cid}
</select>
2. 第二种方式(分步式加载)
<resultMap id="clazzResultMapStep" type="clazz">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
<collection property="students" ofType="student" select="com.powernode.mybatis.mapper.StuMapper.selectByCid" column="cid" >
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
</collection>
</resultMap>
<select id="selectByCidStep" resultMap="clazzResultMapStep">
select cid,cname from t_clazz where cid=#{cid}
</select>
十一、MyBatis缓存cache机制
11.1 了解缓存机制
MyBatis的缓存机制:
在执行DQL(select语句)的时候,MyBatis把语句加载到jvm虚拟机中,若是下次又执行完全一样的语句,则直接从缓存中读取,若是期间执行了增删改(改动数据库)的操作则自动清空缓存,必须重新从硬盘中读取
缓存:cache
缓存的作⽤:通过减少IO的⽅式,来提⾼程序的执⾏效率。
mybatis的缓存:将select语句的查询结果放到缓存(内存)当中,下⼀次还是这条select语句的话,直接从缓存中取,不再查数据库。⼀⽅⾯是减少了IO。另⼀⽅⾯不再执⾏繁琐的查找算法。效率⼤⼤提升。
mybatis缓存包括:
- ⼀级缓存:将查询到的数据存储到SqlSession中。
- ⼆级缓存:将查询到的数据存储到SqlSessionFactory中。
- 或者集成其它第三⽅的缓存:⽐如EhCache【Java语⾔开发的】、Memcache【C语⾔开发的】
等。 - 缓存只针对于DQL语句,也就是说缓存机制只对应select语句。
11.2 一级缓存
⼀级缓存默认是开启的。不需要做任何配置。
原理:只要使⽤同⼀个SqlSession对象执⾏同⼀条SQL语句,就会⾛缓存。
模块名:mybatis-010-cache
@Test
public void selectById(){
SqlSession session = SqlSessionUtil.openSession();
CarMapper mapper1 = session.getMapper(CarMapper.class);
Car car1 = mapper1.selectById(20L);
System.out.println(car1);
CarMapper mapper2 = session.getMapper(CarMapper.class);
Car car2 = mapper2.selectById(20L);
System.out.println(car2);
session.close();
}
Preparing: select * from t_car where id = ?
Car{id=20, carNum=‘3001’, brand=‘比亚迪比’, guidePrice=30.0, produceTime=‘2030-01-02’, carType=‘新能源’}
Car{id=20, carNum=‘3001’, brand=‘比亚迪比’, guidePrice=30.0, produceTime=‘2030-01-02’, carType=‘新能源’}
可以发现只执行了一次sql语句,因为两句sql一样所以直接从缓存中拿
11.3 一级缓存失效
两种情况下一级缓存会失效
//执行清空缓存
session.clearCache();
- 执行了sqlSession.clearCache(); (清空缓存)
- 执行了INSERT DELETE UPDATE任意一句(因为要保证数据是真实的)
11.4 二级缓存
⼆级缓存的范围是SqlSessionFactory。
使⽤⼆级缓存需要具备以下⼏个条件:
- 全局性地开启或关闭所有映射器配置⽂件中已配置
的任何缓存。默认就是true,⽆需设置。
<setting name="cacheEnabled" value="true">
- 在需要使⽤⼆级缓存的SqlMapper.xml⽂件中添加配置:
<cache />
- 使⽤⼆级缓存的实体类对象必须是可序列化的,也就是必须实现java.io.Serializable接⼝
- SqlSession对象关闭或提交之后,⼀级缓存中的数据才会被写⼊到⼆级缓存当中。此时⼆级缓存才
可⽤。
public void selectById2() throws Exception{
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
SqlSession session1 = sessionFactory.openSession();
CarMapper mapper1 = session1.getMapper(CarMapper.class);
Car car1 = mapper1.selectById2(19L);
System.out.println(car1);
//只有当SqlSession关闭的时候,一级缓存才会到二级缓存里
session1.close();
SqlSession session2 = sessionFactory.openSession();
CarMapper mapper2 = session2.getMapper(CarMapper.class);
Car car2 = mapper2.selectById2(19L);
System.out.println(car2);
session2.close();
}
11.5 二级缓存的相关配置
⼆级缓存的失效:只要两次查询之间出现了增删改操作。⼆级缓存就会失效。【⼀级缓存也会失效】
- eviction:指定从缓存中移除某个对象的淘汰算法。默认采⽤LRU策略。
LRU:Least Recently Used。最近最少使⽤。优先淘汰在间隔时间内使⽤频率最低的对象。(其
实还有⼀种淘汰算法LFU,最不常⽤。)
FIFO:First In First Out。⼀种先进先出的数据缓存器。先进⼊⼆级缓存的对象最先被淘汰。
SOFT:软引⽤。淘汰软引⽤指向的对象。具体算法和JVM的垃圾回收算法有关。
WEAK:弱引⽤。淘汰弱引⽤指向的对象。具体算法和JVM的垃圾回收算法有关。 - flushInterval:
⼆级缓存的刷新时间间隔。单位毫秒。如果没有设置。就代表不刷新缓存,只要内存⾜够⼤,⼀直会向⼆级缓存中缓存数据。除⾮执⾏了增删改。 - readOnly:
true:多条相同的sql语句执⾏之后返回的对象是共享的同⼀个。性能好。但是多线程并发可能
会存在安全问题。
false:多条相同的sql语句执⾏之后返回的对象是副本,调⽤了clone⽅法。性能⼀般。但安
全。 - size:
设置⼆级缓存中最多可存储的java对象数量。默认值1024。
11.6 集成Ehcache
集成EhCache是为了代替mybatis⾃带的⼆级缓存。⼀级缓存是⽆法替代的。
mybatis对外提供了接⼝,也可以集成第三⽅的缓存组件。⽐如EhCache、Memcache等。都可以。
EhCache是Java写的。Memcache是C语⾔写的。所以mybatis集成EhCache较为常⻅,按照以下步骤操
作,就可以完成集成:
第⼀步:引⼊mybatis整合ehcache的依赖。
<!--mybatis集成ehcache的组件-->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.2</version>
</dependency>
<!--ehcache需要slf4j的⽇志组件,log4j不好使-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
<scope>test</scope>
</dependency>
第⼆步:在类的根路径下新建echcache.xml⽂件,并提供以下配置信息
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<!--磁盘存储:将缓存中暂时不使⽤的对象,转移到硬盘,类似于Windows系统的虚拟内存-->
<diskStore path="e:/ehcache"/>
<!--defaultCache:默认的管理策略-->
<!--eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有
效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断-->
<!--maxElementsInMemory:在内存中缓存的element的最⼤数⽬-->
<!--overflowToDisk:如果内存中数据超过内存限制,是否要缓存到磁盘上-->
<!--diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false-
->
<!--timeToIdleSeconds:对象空闲时间(单位:秒),指对象在多⻓时间没有被访问就会失
效。只对eternal为false的有效。默认值0,表示⼀直可以访问-->
<!--timeToLiveSeconds:对象存活时间(单位:秒),指对象从创建到失效所需要的时间。
只对eternal为false的有效。默认值0,表示⼀直可以访问-->
<!--memoryStoreEvictionPolicy:缓存的3 种清空策略-->
<!--FIFO:first in first out (先进先出)-->
<!--LFU:Less Frequently Used (最少使⽤).意思是⼀直以来最少被使⽤的。缓存的元
素有⼀个hit 属性,hit 值最⼩的将会被清出缓存-->
<!--LRU:Least Recently Used(最近最少使⽤). (ehcache 默认值).缓存的元素有⼀
个时间戳,当缓存容量满了,⽽⼜需要腾出地⽅来缓存新的元素的时候,那么现有缓存元素中时间戳
离当前时间最远的元素将被清出缓存-->
<defaultCache eternal="false" maxElementsInMemory="1000" overflowToDis
k="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStor
eEvictionPolicy="LRU"/>
</ehcache>
第三步:修改SqlMapper.xml⽂件中的标签,添加type属性。
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
十二、MyBatis的逆向工程
12.1 概述
逆向工程可以通过sql表自动生成pojo类、Mapper类,自动生成增删改查代码
12.2 搭建环境
pom中添加环境:
<!--定制构建过程-->
<build>
<!--可配置多个插件-->
<plugins>
<!--其中的一个插件:mybatis逆向工程插件-->
<plugin>
<!--插件的GAV坐标-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.1</version>
<!--允许覆盖-->
<configuration>
<overwrite>true</overwrite>
</configuration>
<!--插件的依赖-->
<dependencies>
<!--mysql驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
新建generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--
targetRuntime有两个值:
MyBatis3Simple:生成的是基础版,只有基本的增删改查。
MyBatis3:生成的是增强版,除了基本的增删改查之外还有复杂的增删改查。
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--防止生成重复代码-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>
<commentGenerator>
<!--是否去掉生成日期-->
<property name="suppressDate" value="true"/>
<!--是否去除注释-->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--连接数据库信息-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/powernode"
userId="root"
password="root">
</jdbcConnection>
<!-- 生成pojo包名和位置 -->
<javaModelGenerator targetPackage="com.powernode.mybatis.pojo" targetProject="src/main/java">
<!--是否开启子包-->
<property name="enableSubPackages" value="true"/>
<!--是否去除字段名的前后空白-->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成SQL映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.powernode.mybatis.mapper" targetProject="src/main/resources">
<!--是否开启子包-->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成Mapper接口的包名和位置 -->
<javaClientGenerator
type="xmlMapper"
targetPackage="com.powernode.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 表名和对应的实体类名-->
<table tableName="t_car" domainObjectName="Car"/>
</context>
</generatorConfiguration>
运行插件
12.3 QBC查询风格
public void test(){
SqlSession session = SqlSessionUtil.openSession();
CarMapper mapper = session.getMapper(CarMapper.class);
//查询一个结果
Car car = mapper.selectByPrimaryKey(20L);
System.out.println(car);
//查询所有结果
List<Car> cars = mapper.selectByExample(null);
cars.forEach(car1 -> System.out.println(car));
// 根据条件查询结果(QBC query by criteria查询风格
// 添加查询条件
CarExample carExample = new CarExample();
carExample.createCriteria()
.andBrandLike("比亚迪")
.andGuidePriceBetween(new BigDecimal(0),new BigDecimal(40));
//添加or
carExample.or().andCarTypeLike("技术车");
//最终sql语句:select * from t_car where (brand like "%比亚迪%" and guide_price>=0 and guide_price<40) or (car_type like "%技术车%")
List<Car> cars1 = mapper.selectByExample(carExample);
cars1.forEach(car1 -> System.out.println(car1));
session.close();
}
十三、MyBatis使用PageHelper
13.1 limit分页
mysql的limit后面两个数字:
- 第一个数字:startIndex(起始下标。下标从0开始。)
- 第二个数字:pageSize(每页显示的记录条数)
假设已知页码pageNum,还有每页显示的记录条数pageSize,第一个数字可以动态的获取吗?
- startIndex = (pageNum - 1) * pageSize
所以,标准通用的mysql分页SQL:
select
*
from
tableName ......
limit
(pageNum - 1) * pageSize, pageSize
13.2 PageHelper插件
PageHelper插件可以很方便的管理和使用分页
- 引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.1</version>
</dependency>
- 在mybatis-config.xml配置插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
- 编写java代码
@Test
public void test(){
SqlSession session = SqlSessionUtil.openSession();
CarMapper mapper = session.getMapper(CarMapper.class);
//查询第二页,前三条记录
PageHelper.startPage(2,3);
List<Car> cars = mapper.selectAll();
//查看分页信息
PageInfo<Car> pageInfo = new PageInfo<>(cars);
System.out.println(pageInfo);
session.close();
}
十四、注解式开发
注解式开发适合简单的单标CRUD操作,不适合复杂操作
@Insert( "insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
int insertCar(Car car);
@Delete("delete from t_car where id=#{id}")
int deleteById(Long id);
@Update("update t_car set car_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType} where id=#{id}")
int updateById(Car car);
@Select("select * from t_car where id=#{id}")
Car selectById(Long id);
@Test
public void insertCar(){
SqlSession session = SqlSessionUtil.openSession();
CarMapper mapper = session.getMapper(CarMapper.class);
Car car = new Car(null,"10377","报道33车1",35.0,"2022-12-05","技术车");
mapper.insertCar(car);
session.commit();
session.close();
}