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

xiaoxiao2021-02-28  66

一、前言

      在前一篇博客中,小编曾经向大家分享了【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的。加油!

转载请注明原文地址: https://www.6miu.com/read-48929.html

最新回复(0)