前言

  相信大家在使用mybatis写mapper接口的时候,最常用且简单的方法就是增删改查了。我也是刚开始做项目,在本篇文章中,我将根据自己在vhr微人力项目中的mapper接口方法为实例,记录一下接口中常用的增删改查方法

1.insert

  1.1 insert的使用

    1.1.1 接口方法

int insert(Nation record);

    1.1.2 SQL实现

<insert id="insert" parameterType="com.ku.vhr.model.Nation">
insert into vhr_project.nation
(id, name) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
</insert>
  1. SQL语句中的插入操作,我们使用<insert></insert>,id的名称必须于mapper中的插入方法名保持一致,不然会报错;
  2. parameterType是指你传入的参数的参数类型,在mapper接口中的insert方法中我传入的是Nation实体类对象,因此我的插入参数类型为我定义的Nation类
  3. 在数据库中插入数据语句格式:insert into `数据库名.表名` (`字段名1`, `字段名2`, ...) values (#{属性名1,jdbcType="", #{属性名2,jdbcType=""}, ...})
  4. 其中jdbcType的作用是防止在插入过程中因属性名为空而报错,在编写SQL语句的时候最好加上
  5. jdbcType的常用类型
JDBC Type            Java Type
CHAR String
VARCHAR String
LONGVARCHAR String
NUMERIC java.math.BigDecimal
DECIMAL java.math.BigDecimal
BIT boolean
BOOLEAN boolean
TINYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT double
DOUBLE double
BINARY byte[]
VARBINARY byte[]
LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
CLOB Clob
BLOB Blob
ARRAY Array
DISTINCT mapping of underlying type
STRUCT Struct
REF Ref
DATALINK java.net.URL[color=red][/color]

jdbcType常用类型

  1.2 insert测试

    1.2.1 测试代码

@Test
public void insert(){
// System.out.println(nationMapper);
Nation nation = new Nation();
nation.setId(55);
nation.setName("珞巴族");
int rows = nationMapper.insert(nation);
System.out.println(rows);
}

insertTest

    1.2.2 测试结果

mapper接口中常见的增删改查_SQL


2.insertSelective

  2.1 insertSelective的使用

    2.1.1 接口方法

int insertSelective(Nation record);

    2.1.2 SQL实现

<insert id="insertSelective" parameterType="com.ku.vhr.model.Nation">
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR}
</if>
</trim>
</insert>

insertSelective

  1. 在insertSelective方法中,是不允许插入的内容为空的,这是insertSelective方法与insert方法的本质区别
  2. 在insertSelective方法中,标签<trim></trim>可以用于增删改以及条件判断,在该方法中的作用就是判断数据库字段以及属性是否为空,如果为空,插入报错
  3. prefix 是指在字段前面添加什么,suffix是指在字段插入之后加上什么,suffixOverrides是指最后去掉什么
  4. SQL插入语句格式为insert into `数据库名.表名` (`字段名1`, `字段名2`, ...) values (#{属性名1,jdbcType="", #{属性名2,jdbcType=""}, ...}),
  5. 从into开始就是用<trim></trim>标签来执行,所以第一个<trim></trim>标签prefix指"(",suffix指最后一个字段之后的")",suffixOverrides指去掉最后的","
  6. 第二个<trim></trim>标签prefix指"values (",suffix指最后一个字段之后的")",suffixOverrides指去掉最后的","
  7. <if test= "t条件判断"></if>标签,主要是用于条件判断;主要注意的是,条件中的变量必须是实体类的属性

  2.2 insertSelective测试

    2.2.1 测试代码

@Test
public void insertSelective(){
Nation nation = new Nation();
nation.setId(56);
nation.setName("基诺族");
int rows = nationMapper.insertSelective(nation);
System.out.println(rows);
}

insertSelectiveTest

    2.2.2 测试结果

mapper接口中常见的增删改查_字段_02

 

3.deleteById

  3.1 deleteById的使用

    3.1.1 接口方法

int deleteById(Integer id);

    3.1.2 SQL实现

<delete id="deleteById" paramentType="java.lang.Integer">
delete from vhr_project.nation
where id = #{id,jdbcType=Integer}
</delete>
  1. 根据id删除的SQL语句格式为:delete from `数据名.表名` where id = #{id,jdbcType=Integer}

  3.2 deleteById测试

    3.2.1 测试方法

@Test
public void deleteById(){
int rows = nationMapper.deleteById(57);
System.out.println(rows);
}

deleteByIdTest

4.updateById

  4.1 updateById的使用

    4.1.1 接口方法

int updateById(Nation record);

    4.1.2 SQL实现

<update id="updateById" parameterType="com.ku.vhr.model.Nation">
update vhr_project.nation
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>

updateById

  1. 修改的SQL语句格式:update `数据库名.表名` set `字段1` = #{属性1,jdbcType=""}, `字段2` = #{属性2,jdbcType=""},...

  4.2 updateById测试

    4.2.1 测试方法

@Test
public void updateById(){
Nation nation = new Nation();
nation.setId(55);
nation.setName("什么族");
int rows = nationMapper.updateById(nation);
System.out.println(rows);
}

updateByIdTest

    4.2.2 测试结果

mapper接口中常见的增删改查_SQL_03

 

5.updateByIdSelective

  5.1 updateByIdSelective的使用

    5.1.1 接口方法

int updateByIdSelective(Nation record);

    5.1.2 SQL实现

<update id="updateByIdSelective" parameterType="com.ku.vhr.model.Nation">
update vhr_project.nation
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR}
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>

updateByIdSelective

  1. 在updateByIdSelective的SQL中使用<set></set>类似于<trim></trim>用于写条件判断,但是<set></set>只能用于修改SQL

5.2 updateByIdSelective测试

    5.2.1 测试方法

@Test
public void updateByIdSelective(){
Nation nation = new Nation();
nation.setId(55);
nation.setName("珞巴族");
int rows = nationMapper.updateById(nation);
System.out.println(rows);
}

updateByIdSelectiveTest

    5.2.2 测试结果

mapper接口中常见的增删改查_bc_04

 

6.selectById

  6.1 selectById的使用

    6.1.1 接口方法

Nation selectById();

    6.1.2 SQL实现

<select>
select <include ref = "Base_Column_List"/>
from `vhr_project.nation`
where id = #{id,jdbcType=INTEGER}
</select>

selectById

  1. 根据id查询数据的SQL语句格式:select * from `数据库名.表名` where `字段主键` = #{属性id,jdbcType=INTEGER}
  2. <include=ref="别名"/>是与<sql id = "别名">字段1, 字段2, 字段3, 字段4, 字段5</sql>一起联用的,
  3. <sql></sql>是用来提取重用的SQL片段,以此定义,多次使用,非常方便
  4. sql id = "别名"></sql>中的id是<include ref=""/>中的ref的值

  6.2 selectById测试

    6.2.1 测试方法

@Test
public void selectById(){
Nation nation = nationMapper.selectById(2);
System.out.println(nation);
}

selectByIdTest

    6.2.2 测试结果

mapper接口中常见的增删改查_SQL_05

 

7.getAll

  7.1 getAll的使用

    7.1.1 接口方法

List<Nation> getAllNations();

    7.1.2 SQL实现

<select id="getAllNations" resultMap="BaseResultMap">
select * from vhr_project.nation
</select>

getAll

  1. 获取各类的全部信息SQL格式:select * from `数据库名.表名`

  7.2 getAll测试

    7.2.1 测试方法

@Test
public void getAllNations(){
List<Nation> allNations = nationMapper.getAllNations();
for (Nation nation : allNations) {
System.out.println(nation);
}
}

getAllTest

    7.2.2 测试结果

mapper接口中常见的增删改查_SQL_06

 

8.参考链接

9.如果我写的有什么问题,请大家帮我指正!我会持续修改的