在前一篇博客中,小编曾经向大家分享了【Mybatis】深入浅出Mybatis(十)——逆向工程,简单介绍了如何通过逆向工程,生成我们需要的实体和mapper映射文件。在这篇博客中,小编将向大家介绍一下如何调用。
我们对一个表进行相应得到增删改查,首先看一下mapper映射文件:
xml version="1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
"com.taotao.mapper.TbContentMapper" >
"BaseResultMap" type="com.taotao.pojo.TbContent" >
"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" />
"ResultMapWithBLOBs" type="com.taotao.pojo.TbContent" extends="BaseResultMap" >
<result column="content" property="content" jdbcType="LONGVARCHAR" />
"Example_Where_Clause" >
"oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
"(" suffix=")" prefixOverrides="and" >
"criteria.criteria" item="criterion" >
"criterion.noValue" >
and ${criterion.condition}
"criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
"criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
"criterion.listValue" >
and ${criterion.condition}
"criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
if>
"Update_By_Example_Where_Clause" >
"example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
"(" suffix=")" prefixOverrides="and" >
"criteria.criteria" item="criterion" >
"criterion.noValue" >
and ${criterion.condition}
"criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
"criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
"criterion.listValue" >
and ${criterion.condition}
"criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
if>
"Base_Column_List" >
id, category_id, title, sub_title, title_desc, url, pic, pic2, created, updated
"Blob_Column_List" >
content
<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" 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})
"insertSelective" parameterType="com.taotao.pojo.TbContent" >
insert into tb_content
"(" 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>
"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>
"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>
"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>
"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>
"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}
"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}
"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}
在这个文件中,包含了所有的增删改查的方法。它们对应着不同的sql语句,我们要做的就是要选择合适得到语句来执行我们的操作。下面一次展示增删改查操作。
在这里我们直接调用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();
}
}
@Override
public TaotaoResult deleteContent(Long id) {
int count = contentMapper.deleteByPrimaryKey(id);
if (count>0) {
return TaotaoResult.ok();
}
return null;
}
@Override
public TaotaoResult updateContent(TbContent content) {
content.setUpdated(new Date());
//更新数据
contentMapper.updateByPrimaryKey(content);
return TaotaoResult.ok();
}
这里我们使用按照条件进行查询,TbContentExample ,通过criteria.andCategoryIdEqualTo(id)来确定,查询条件是CategoryId=id的操作。当然我们还可以采用其他的操作。
@Override
public List getContentList(Long id) {
TbContentExample example = new TbContentExample();
Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(id);
//查询
List contentList = contentMapper.selectByExample(example);
return contentList;
}
不知道朋友们有没有发现,我们调用的增删改查都是针对单表操作的,所以这个就是逆向功能的一个弊端吧,如果我们需要进行多表操作,就得对mapper文件进行添加sql,自己写sql语句了。对于这些操作,小编会在下一篇博客中向大家介绍。
逆向工程的的使用还是比较方便的,当我们在使用的过程中,还是有很多的需要进行单表操作的,下面只要我们找到合适的方法,写出合适的方法还是很666的。加油!