手写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="cn.edu.neu.hello_mybatis.mapper.ArticleMapper">
<select id="selectArticle" resultType="article">
SELECT * FROM article where aid=#{aid}
</select>


<insert id="addArticle" parameterType="article">
insert into article values(null,#{title},#{content},#{titlepic},#{video})
</insert>

<insert id="addPK" parameterType="article" useGeneratedKeys="true" keyProperty="aid">
insert into article values(null,#{title},#{content},#{titlepic},#{video})
</insert>

<insert id="insertPKBack" parameterType="article">
<selectKey keyProperty="aid" resultType="int" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into article values(null,#{title},#{content},#{titlepic},#{video})
</insert>


<update id="update" parameterType="article">
update article set title=#{title},content=#{content} where aid=#{aid}
</update>

<delete id="delete" parameterType="int">
delete from article where aid=#{aid}
</delete>

<resultMap id="custResultMap" type="article">
<!-- property:需要映射到JavaBean 的属性名称。column:数据表的列名或者标签别名。 -->
<id property="id" column="id" />
<result property="custId" column="cid"/>
<result property="name" column="name"/>
</resultMap>

<select id="custResultMapList" resultMap="custResultMap">
select id, custid cid,name name,age age from customer
</select>


<sql id="articleColumns">
aid,title,content,titlepic,video
</sql>

<select id="getSql" resultType="article">
select
<include refid="articleColumns"/>
from article
</select>

<select id="getArticleAndComment" resultMap="articleWithComment">
SELECT a.*,c.cid c_id,c.content c_content,c.author
FROM article a,comment c
WHERE a.aid=c.aid AND a.aid = #{aid}
</select>
<resultMap id="articleWithComment" type="Article">
<id property="aid" column="aid" />
<result property="title" column="title" />
<result property="content" column="content" />
<collection property="commentList" ofType="Comment">
<id property="cid" column="c_id" />
<result property="content" column="c_content" />
<result property="author" column="author" />
</collection>
</resultMap>

 

第二种
<update id="update">
update sys_user
<set>
<if test="username != null">
username = #{username},
</if>
<!-- <if test="password != null">-->
<!-- password = #{password}-->
<!-- </if>-->
<if test="nickname != null">
nickname = #{nickname},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="phone != null">
phone = #{phone},
</if>
<if test="address != null">
address = #{address}
</if>
</set>
<where>
id = #{id}
</where>
</update>
</mapper>

posted @ 2024-02-24 10:04  XiangdxDu  阅读(32)  评论(0编辑  收藏  举报