文章主要是记录一次demo的搭建过程,方便以后回顾。只贴出了关键代码和指明了关键位置。不过也算是比较完整了。
目录
springboot配置
i. pom.xml
主要是这几个依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
ii. 启动项Application
@MapperScan的路径,存放的是mapper接口
package cn.line.cart;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.line.cart.mapper")
public class CartSysApplication {
public static void main(String[] args) {
SpringApplication.run(CartSysApplication.class, args);
}
}
iii. mapper接口
接口的名字,跟后续的mapper.xml的sql要对应
ProductBrief是自定义的entity,用于存放查询结果。注意需要实现getter/setter。本文直接通过lombok注解添加了。
@Param("name")注解,用于传递参数到mapper.xml。通过#{name}来取得对应的值;还可以用来一次传递多个参数,例如updateById这个函数。
package cn.line.cart.mapper;
import cn.line.cart.entity.ProductBrief;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
* 简单的增删改查
*/
@Repository
public interface ProductBriefMapper {
void insert(@Param("brief") ProductBrief brief);
void deleteById(@Param("pid") int id);
void updateById(@Param("pid") int id, @Param("tar") ProductBrief brief);
ProductBrief queryById(@Param("pid") int id);
}
iiii. mapper.xml
<mappper>标签的namespace属性,和iii中定义的mapper接口对应
<sql>标签和<include>标签成对,用于定义定值。类似C中的define
<resultMap>顾名思义,用于保存结果。type即是iii中提到的entity。其中,column属性是对应db-table中的列名,property属性是对应entity中定义的变量名。
其他的下面再说。
<?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 namespace="cn.line.cart.mapper.ProductBriefMapper">
<sql id="tableName">tbl_product_brief</sql>
<sql id="tableFields">product_id, product_name, product_price, product_desc</sql>
<resultMap id="ProductBriefMap" type="cn.line.cart.entity.ProductBrief">
<id column="product_id" jdbcType="INTEGER" property="prodId" />
<result column="product_name" jdbcType="VARCHAR" property="prodName" />
<result column="product_price" jdbcType="INTEGER" property="prodPrice" />
<result column="product_desc" jdbcType="VARCHAR" property="prodDesc" />
</resultMap>
<select id="queryById" resultMap="ProductBriefMap">
select <include refid="tableFields"/>
from <include refid="tableName"/>
where product_id = #{pid};
</select>
<insert id="insert">
insert into <include refid="tableName"/>
(<include refid="tableFields"/>)
values
(#{brief.prodId}, #{brief.prodName}, #{brief.prodPrice}, #{brief.prodDesc});
</insert>
<delete id="deleteById">
delete from <include refid="tableName"/>
where product_id = #{pid};
</delete>
<update id="updateById">
update <include refid="tableName"/>
<set>
<if test="tar != null and tar.prodId != null and tar.prodId != ''">
product_id = #{tar.prodId},
</if>
<if test="tar != null and tar.prodName != null and tar.prodName != ''">
product_name = #{tar.prodName},
</if>
<if test="tar != null and tar.prodPrice != null and tar.prodPrice != ''">
product_price = #{tar.prodPrice},
</if>
<if test="tar != null and tar.prodDesc != null and tar.prodDesc != ''">
product_desc = #{tar.prodDesc},
</if>
</set>
where product_id = #{pid}
</update>
</mapper>
iiiii. application.yml
mapper-locations是mapper.xml的存放路径
spring:
datasource:
username: root
#password: 1234
url: jdbc:mysql://localhost:3306/shoponline?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
增删改查
本文的案例比较简单,主要说下updateById吧。
这个接口我有意让他传两个参数进来,一个是简单参数integer,一个是复杂的自定义参数。这个通过@Param注解来实现。
当传入ProductBrief对象的某个变量不为空时,才修改这个值;为空时,则不修改,而不是将数据库中对应的值改为空。这个通过set-if标签组合来实现。
两个参数(多个参数)
两个参数的时候,最好就直接用@Param + name来传参了,不然会比较麻烦。
因此干脆统一一下,一个参数的,也通过@Param来传参。
用@Param要注意,在mapper接口定义的时候,就把参数名name定好,mapper.xml中,再通过#{name}来获取
复杂数据类型(自定义数据类型)
若@Param标注了一个复杂数据类型,在mapper.xml中,则可以通过#{name.field}来获取具体的成员变量,field是变量名。
条件判断set-if
这个参考下面的链接吧。
参考
一定要注意的是,尽量用复制粘贴,不然某个名字没对应上,排查的时候会比较麻烦。
欢迎讨论交流~