1.引入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
2.写application.yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.qiku.wego.domain
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pagehelper:
offset-as-page-num: true
row-bounds-with-count: true
support-methods-arguments: true
reasonable: true
params: count=countSql
3.写实体类
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Dept implements Serializable {
private static final long serialVersionUID = -8585766265105736658L;
private Integer deptno;
private String dname;
private String loc;
}
4.写mapper
@Mapper
public interface DeptMapper {
int deleteByPrimaryKey(Integer deptno);
int insert(Dept record);
int insertSelective(Dept record);
Dept selectByPrimaryKey(Integer deptno);
int updateByPrimaryKeySelective(Dept record);
int updateByPrimaryKey(Dept record);
int batchInsert(@Param("list") List<Dept> list);
}
5.写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 namespace="com.qiku.wego.mapper.DeptMapper">
<resultMap id="BaseResultMap" type="com.qiku.wego.domain.Dept">
<id column="deptno" jdbcType="TINYINT" property="deptno" />
<result column="dname" jdbcType="VARCHAR" property="dname" />
<result column="loc" jdbcType="VARCHAR" property="loc" />
</resultMap>
<sql id="Base_Column_List">
deptno, dname, loc
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tb_dept
where deptno = #{deptno,jdbcType=TINYINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from tb_dept
where deptno = #{deptno,jdbcType=TINYINT}
</delete>
<insert id="insert" parameterType="com.qiku.wego.domain.Dept">
insert into tb_dept (deptno, dname, loc
)
values (#{deptno,jdbcType=TINYINT}, #{dname,jdbcType=VARCHAR}, #{loc,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.qiku.wego.domain.Dept">
insert into tb_dept
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="deptno != null">
deptno,
</if>
<if test="dname != null">
dname,
</if>
<if test="loc != null">
loc,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="deptno != null">
#{deptno,jdbcType=TINYINT},
</if>
<if test="dname != null">
#{dname,jdbcType=VARCHAR},
</if>
<if test="loc != null">
#{loc,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.qiku.wego.domain.Dept">
update tb_dept
<set>
<if test="dname != null">
dname = #{dname,jdbcType=VARCHAR},
</if>
<if test="loc != null">
loc = #{loc,jdbcType=VARCHAR},
</if>
</set>
where deptno = #{deptno,jdbcType=TINYINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.qiku.wego.domain.Dept">
update tb_dept
set dname = #{dname,jdbcType=VARCHAR},
loc = #{loc,jdbcType=VARCHAR}
where deptno = #{deptno,jdbcType=TINYINT}
</update>
<insert id="batchInsert" parameterType="map">
insert into tb_dept
(deptno, dname, loc)
values
<foreach collection="list" item="item" separator=",">
(#{item.deptno,jdbcType=TINYINT}, #{item.dname,jdbcType=VARCHAR}, #{item.loc,jdbcType=VARCHAR}
)
</foreach>
</insert>
</mapper>
6.测试
@SpringBootTest
class DeptMapperTest {
@Resource
DeptMapper deptMapper;
@Test
void selectByPrimaryKey() {
Dept dept = deptMapper.selectByPrimaryKey(10);
System.out.println(dept);
}
}