【Mybatis】深入浅出Mybatis(十二)——逆向工程后增删改查调用

一、前言

      在前一篇博客中,小编曾经向大家分享了【Mybatis】深入浅出Mybatis(十)——逆向工程,简单介绍了如何通过逆向工程,生成我们需要的实体和mapper映射文件。在这篇博客中,小编将向大家介绍一下如何调用。

二、增删改查

      我们对一个表进行相应得到增删改查,首先看一下mapper映射文件:

2.1 mapper映射

<?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.taotao.mapper.TbContentMapper" >
  <resultMap id="BaseResultMap" type="com.taotao.pojo.TbContent" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="category_id" property="categoryId" jdbcType="BIGINT" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="sub_title" property="subTitle" jdbcType="VARCHAR" />
    <result column="title_desc" property="titleDesc" jdbcType="VARCHAR" />
    <result column="url" property="url" jdbcType="VARCHAR" />
    <result column="pic" property="pic" jdbcType="VARCHAR" />
    <result column="pic2" property="pic2" jdbcType="VARCHAR" />
    <result column="created" property="created" jdbcType="TIMESTAMP" />
    <result column="updated" property="updated" jdbcType="TIMESTAMP" />
  </resultMap>
  <resultMap id="ResultMapWithBLOBs" type="com.taotao.pojo.TbContent" extends="BaseResultMap" >
    <result column="content" property="content" jdbcType="LONGVARCHAR" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    id, category_id, title, sub_title, title_desc, url, pic, pic2, created, updated
  </sql>
  <sql id="Blob_Column_List" >
    content
  </sql>
  <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="com.taotao.pojo.TbContentExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from tb_content
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.taotao.pojo.TbContentExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from tb_content
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from tb_content
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from tb_content
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.taotao.pojo.TbContentExample" >
    delete from tb_content
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.taotao.pojo.TbContent" >
    insert into tb_content (id, category_id, title, 
      sub_title, title_desc, url, 
      pic, pic2, created, 
      updated, content)
    values (#{id,jdbcType=BIGINT}, #{categoryId,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, 
      #{subTitle,jdbcType=VARCHAR}, #{titleDesc,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, 
      #{pic,jdbcType=VARCHAR}, #{pic2,jdbcType=VARCHAR}, #{created,jdbcType=TIMESTAMP}, 
      #{updated,jdbcType=TIMESTAMP}, #{content,jdbcType=LONGVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.taotao.pojo.TbContent" >
    insert into tb_content
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="categoryId != null" >
        category_id,
      </if>
      <if test="title != null" >
        title,
      </if>
      <if test="subTitle != null" >
        sub_title,
      </if>
      <if test="titleDesc != null" >
        title_desc,
      </if>
      <if test="url != null" >
        url,
      </if>
      <if test="pic != null" >
        pic,
      </if>
      <if test="pic2 != null" >
        pic2,
      </if>
      <if test="created != null" >
        created,
      </if>
      <if test="updated != null" >
        updated,
      </if>
      <if test="content != null" >
        content,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="categoryId != null" >
        #{categoryId,jdbcType=BIGINT},
      </if>
      <if test="title != null" >
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="subTitle != null" >
        #{subTitle,jdbcType=VARCHAR},
      </if>
      <if test="titleDesc != null" >
        #{titleDesc,jdbcType=VARCHAR},
      </if>
      <if test="url != null" >
        #{url,jdbcType=VARCHAR},
      </if>
      <if test="pic != null" >
        #{pic,jdbcType=VARCHAR},
      </if>
      <if test="pic2 != null" >
        #{pic2,jdbcType=VARCHAR},
      </if>
      <if test="created != null" >
        #{created,jdbcType=TIMESTAMP},
      </if>
      <if test="updated != null" >
        #{updated,jdbcType=TIMESTAMP},
      </if>
      <if test="content != null" >
        #{content,jdbcType=LONGVARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.taotao.pojo.TbContentExample" resultType="java.lang.Integer" >
    select count(*) from tb_content
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    update tb_content
    <set >
      <if test="record.id != null" >
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.categoryId != null" >
        category_id = #{record.categoryId,jdbcType=BIGINT},
      </if>
      <if test="record.title != null" >
        title = #{record.title,jdbcType=VARCHAR},
      </if>
      <if test="record.subTitle != null" >
        sub_title = #{record.subTitle,jdbcType=VARCHAR},
      </if>
      <if test="record.titleDesc != null" >
        title_desc = #{record.titleDesc,jdbcType=VARCHAR},
      </if>
      <if test="record.url != null" >
        url = #{record.url,jdbcType=VARCHAR},
      </if>
      <if test="record.pic != null" >
        pic = #{record.pic,jdbcType=VARCHAR},
      </if>
      <if test="record.pic2 != null" >
        pic2 = #{record.pic2,jdbcType=VARCHAR},
      </if>
      <if test="record.created != null" >
        created = #{record.created,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updated != null" >
        updated = #{record.updated,jdbcType=TIMESTAMP},
      </if>
      <if test="record.content != null" >
        content = #{record.content,jdbcType=LONGVARCHAR},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExampleWithBLOBs" parameterType="map" >
    update tb_content
    set id = #{record.id,jdbcType=BIGINT},
      category_id = #{record.categoryId,jdbcType=BIGINT},
      title = #{record.title,jdbcType=VARCHAR},
      sub_title = #{record.subTitle,jdbcType=VARCHAR},
      title_desc = #{record.titleDesc,jdbcType=VARCHAR},
      url = #{record.url,jdbcType=VARCHAR},
      pic = #{record.pic,jdbcType=VARCHAR},
      pic2 = #{record.pic2,jdbcType=VARCHAR},
      created = #{record.created,jdbcType=TIMESTAMP},
      updated = #{record.updated,jdbcType=TIMESTAMP},
      content = #{record.content,jdbcType=LONGVARCHAR}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    update tb_content
    set id = #{record.id,jdbcType=BIGINT},
      category_id = #{record.categoryId,jdbcType=BIGINT},
      title = #{record.title,jdbcType=VARCHAR},
      sub_title = #{record.subTitle,jdbcType=VARCHAR},
      title_desc = #{record.titleDesc,jdbcType=VARCHAR},
      url = #{record.url,jdbcType=VARCHAR},
      pic = #{record.pic,jdbcType=VARCHAR},
      pic2 = #{record.pic2,jdbcType=VARCHAR},
      created = #{record.created,jdbcType=TIMESTAMP},
      updated = #{record.updated,jdbcType=TIMESTAMP}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.taotao.pojo.TbContent" >
    update tb_content
    <set >
      <if test="categoryId != null" >
        category_id = #{categoryId,jdbcType=BIGINT},
      </if>
      <if test="title != null" >
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="subTitle != null" >
        sub_title = #{subTitle,jdbcType=VARCHAR},
      </if>
      <if test="titleDesc != null" >
        title_desc = #{titleDesc,jdbcType=VARCHAR},
      </if>
      <if test="url != null" >
        url = #{url,jdbcType=VARCHAR},
      </if>
      <if test="pic != null" >
        pic = #{pic,jdbcType=VARCHAR},
      </if>
      <if test="pic2 != null" >
        pic2 = #{pic2,jdbcType=VARCHAR},
      </if>
      <if test="created != null" >
        created = #{created,jdbcType=TIMESTAMP},
      </if>
      <if test="updated != null" >
        updated = #{updated,jdbcType=TIMESTAMP},
      </if>
      <if test="content != null" >
        content = #{content,jdbcType=LONGVARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.taotao.pojo.TbContent" >
    update tb_content
    set category_id = #{categoryId,jdbcType=BIGINT},
      title = #{title,jdbcType=VARCHAR},
      sub_title = #{subTitle,jdbcType=VARCHAR},
      title_desc = #{titleDesc,jdbcType=VARCHAR},
      url = #{url,jdbcType=VARCHAR},
      pic = #{pic,jdbcType=VARCHAR},
      pic2 = #{pic2,jdbcType=VARCHAR},
      created = #{created,jdbcType=TIMESTAMP},
      updated = #{updated,jdbcType=TIMESTAMP},
      content = #{content,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.taotao.pojo.TbContent" >
    update tb_content
    set category_id = #{categoryId,jdbcType=BIGINT},
      title = #{title,jdbcType=VARCHAR},
      sub_title = #{subTitle,jdbcType=VARCHAR},
      title_desc = #{titleDesc,jdbcType=VARCHAR},
      url = #{url,jdbcType=VARCHAR},
      pic = #{pic,jdbcType=VARCHAR},
      pic2 = #{pic2,jdbcType=VARCHAR},
      created = #{created,jdbcType=TIMESTAMP},
      updated = #{updated,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>

      在这个文件中,包含了所有的增删改查的方法。它们对应着不同的sql语句,我们要做的就是要选择合适得到语句来执行我们的操作。下面一次展示增删改查操作。

2.2 增

      在这里我们直接调用dao的insert方法,insert使用的sql语句可以在mapper映射文件中找到。


/**
 * 内容管理service
 * @author Ares
 *
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper contentMapper;

    @Override
    public TaotaoResult insertContent(TbContent content) {
        content.setCreated(new Date());
        content.setUpdated(new Date());
        //插入数据
        contentMapper.insert(content);

        return TaotaoResult.ok();
    }
}

2.3 删

@Override
    public TaotaoResult deleteContent(Long id) {
        int count = contentMapper.deleteByPrimaryKey(id);
        if (count>0) {
            return TaotaoResult.ok();
        }
        return null;
    }

2.4 改

@Override
    public TaotaoResult updateContent(TbContent content) {
        content.setUpdated(new Date());
        //更新数据
        contentMapper.updateByPrimaryKey(content);
        return TaotaoResult.ok();
    }

2.5 查

      这里我们使用按照条件进行查询,TbContentExample ,通过criteria.andCategoryIdEqualTo(id)来确定,查询条件是CategoryId=id的操作。当然我们还可以采用其他的操作。

    @Override
    public List<TbContent> getContentList(Long id) {
        TbContentExample example = new TbContentExample();
        Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        //查询
        List<TbContent> contentList = contentMapper.selectByExample(example);
        return contentList;
    }

三、注意事项

      不知道朋友们有没有发现,我们调用的增删改查都是针对单表操作的,所以这个就是逆向功能的一个弊端吧,如果我们需要进行多表操作,就得对mapper文件进行添加sql,自己写sql语句了。对于这些操作,小编会在下一篇博客中向大家介绍。

四、小结

      逆向工程的的使用还是比较方便的,当我们在使用的过程中,还是有很多的需要进行单表操作的,下面只要我们找到合适的方法,写出合适的方法还是很666的。加油!

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你个佬六

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值