mybatis系列教程:单表数据操作
mybatis系列教程:单表数据操作
以文章列表为例。
文章表结构设计
id :编号
article_title :标题
article_content :内容
article_type :文章类型
................:略了几个字段
代码
1:Controller
1.1 查询 List<DrArticles> list = drArticlesService.selectDrArticlesList(drArticles);
2:Bean
@Data
public class DrArticles extends BaseEntity
{
private static final long serialVersionUID = 1L;
private Integer id;
private String articleTitle;
private String articleType;
private String articleContent;
private String createNickname;
}
3:Service
4:Mapper
5:mybatis xml文件
<?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.system.mapper.DrArticlesMapper">
<resultMap type="DrArticles" id="DrArticlesResult">
<result property="id" column="id" />
<result property="articleTitle" column="article_title" />
<result property="articleType" column="article_type" />
<result property="articleContent" column="article_content" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<result property="createNickname" column="create_nickname" />
</resultMap>
<sql id="selectDrArticlesVo">
select id, article_title, article_type, article_content, create_by, create_time, update_by, update_time, remark, create_nickname from dr_articles
</sql>
<select id="selectDrArticlesList" parameterType="DrArticles" resultMap="DrArticlesResult">
<include refid="selectDrArticlesVo"/>
<where>
<if test="articleTitle != null and articleTitle != ''"> and article_title = #{articleTitle}</if>
<if test="articleType != null and articleType != ''"> and article_type = #{articleType}</if>
<if test="articleContent != null and articleContent != ''"> and article_content = #{articleContent}</if>
<if test="createNickname != null and createNickname != ''"> and create_nickname like concat('%', #{createNickname}, '%')</if>
</where>
</select>
<select id="selectDrArticlesById" parameterType="Integer" resultMap="DrArticlesResult">
<include refid="selectDrArticlesVo"/>
where id = #{id}
</select>
<insert id="insertDrArticles" parameterType="DrArticles" useGeneratedKeys="true" keyProperty="id">
insert into dr_articles
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="articleTitle != null">article_title,</if>
<if test="articleType != null">article_type,</if>
<if test="articleContent != null">article_content,</if>
<if test="createBy != null">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="remark != null">remark,</if>
<if test="createNickname != null">create_nickname,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="articleTitle != null">#{articleTitle},</if>
<if test="articleType != null">#{articleType},</if>
<if test="articleContent != null">#{articleContent},</if>
<if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="remark != null">#{remark},</if>
<if test="createNickname != null">#{createNickname},</if>
</trim>
</insert>
<update id="updateDrArticles" parameterType="DrArticles">
update dr_articles
<trim prefix="SET" suffixOverrides=",">
<if test="articleTitle != null">article_title = #{articleTitle},</if>
<if test="articleType != null">article_type = #{articleType},</if>
<if test="articleContent != null">article_content = #{articleContent},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="createNickname != null">create_nickname = #{createNickname},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteDrArticlesById" parameterType="Integer">
delete from dr_articles where id = #{id}
</delete>
<delete id="deleteDrArticlesByIds" parameterType="String">
delete from dr_articles where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>