一、前言
在前一篇博客中,小编曾经向大家分享了【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}
</when>
<when test=
"criterion.betweenValue" >
and ${criterion.condition}
</when>
<when test=
"criterion.listValue" >
and ${criterion.condition}
<foreach collection=
"criterion.value" item=
"listItem" open=
"(" close=
")" separator=
"," >
</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}
</when>
<when test=
"criterion.betweenValue" >
and ${criterion.condition}
</when>
<when test=
"criterion.listValue" >
and ${criterion.condition}
<foreach collection=
"criterion.value" item=
"listItem" open=
"(" close=
")" separator=
"," >
</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 =
</select>
<
delete id=
"deleteByPrimaryKey" parameterType=
"java.lang.Long" >
delete from tb_content
where id =
</
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 (
</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" >
</
if>
<
if test=
"categoryId != null" >
</
if>
<
if test=
"title != null" >
</
if>
<
if test=
"subTitle != null" >
</
if>
<
if test=
"titleDesc != null" >
</
if>
<
if test=
"url != null" >
</
if>
<
if test=
"pic != null" >
</
if>
<
if test=
"pic2 != null" >
</
if>
<
if test=
"created != null" >
</
if>
<
if test=
"updated != null" >
</
if>
<
if test=
"content != null" >
</
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 =
</
if>
<
if test=
"record.categoryId != null" >
category_id =
</
if>
<
if test=
"record.title != null" >
title =
</
if>
<
if test=
"record.subTitle != null" >
sub_title =
</
if>
<
if test=
"record.titleDesc != null" >
title_desc =
</
if>
<
if test=
"record.url != null" >
url =
</
if>
<
if test=
"record.pic != null" >
pic =
</
if>
<
if test=
"record.pic2 != null" >
pic2 =
</
if>
<
if test=
"record.created != null" >
created =
</
if>
<
if test=
"record.updated != null" >
updated =
</
if>
<
if test=
"record.content != null" >
content =
</
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 =
category_id =
title =
sub_title =
title_desc =
url =
pic =
pic2 =
created =
updated =
content =
<
if test=
"_parameter != null" >
<
include refid=
"Update_By_Example_Where_Clause" />
</
if>
</update>
<update id=
"updateByExample" parameterType=
"map" >
update tb_content
set id =
category_id =
title =
sub_title =
title_desc =
url =
pic =
pic2 =
created =
updated =
<
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 =
</
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>
</
set>
where id =
</update>
<update id=
"updateByPrimaryKeyWithBLOBs" parameterType=
"com.taotao.pojo.TbContent" >
update tb_content
set category_id =
title =
sub_title =
title_desc =
url =
pic =
pic2 =
created =
updated =
content =
where id =
</update>
<update id=
"updateByPrimaryKey" parameterType=
"com.taotao.pojo.TbContent" >
update tb_content
set category_id =
title =
sub_title =
title_desc =
url =
pic =
pic2 =
created =
updated =
where id =
</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的。加油!