Mybatis标签使用回顾

useGeneratedKeys及keyProperty参数的区别

举个例子

<insert id="addNewArticle" parameterType="org.sang.bean.Article" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO article SET title=#{title},mdContent=#{mdContent},htmlContent=#{htmlContent},summary=#{summary},cid=#{cid},uid=#{uid},publishDate=#{publishDate},state=#{state},editTime=#{editTime}
    </insert>

look here
添加链接描述

parameterType及resultType的区别

举个例子

 <insert id="addNewArticle" parameterType="org.sang.bean.Article" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO article SET title=#{title},mdContent=#{mdContent},htmlContent=#{htmlContent},summary=#{summary},cid=#{cid},uid=#{uid},publishDate=#{publishDate},state=#{state},editTime=#{editTime}
    </insert>
<select id="getArticleCountByState" resultType="int">
        SELECT count(*) FROM article   
    </select>

resultType是sql语句查询结果集的封装类型,也就是说把sql查询的结果封装在bean里返回回去,是存数据用的。 paramType是从传过来的Bean中取数据放进例如insert语句的values中当实参用,是取数据用的

总之就是parameterType用于增删改,resultType用于查

collection用法

collection 定义集合类型的属性封装规则

  • property 对应封装类中某个集合对象
  • ofType 对应封装类中某个集合对象的泛型类

举个例子

//工厂实体类
public class Department {
private Integer id;
private String departmentName;
	//所有员工
    private List<Employer> emps
   //下边还有些get,set属性的方法,为省篇幅就不贴啦
}

如何把查询结果封装成上边的工厂实体类
如下图:
在这里插入图片描述

resultMap如何自定义结果集

所需参数

将查询出的结果映射到自定义的这么一个map里,所以所需参数

  • column : 查询结果中对应的字段名
  • property : 实体类中对应的对象名称

举个例子
实体类:

public class Article {
    private Long id;
    private String title;
    private String mdContent;
    private String htmlContent;
    private String summary;
    private Long cid;
    private Long uid;
    private Timestamp publishDate;
    private Integer state;
    private Integer pageView;
    private Timestamp editTime;
    private String[] dynamicTags;
    private String nickname;
    private String cateName;
    private List<Tags> tags;
    private String stateStr;

   //下边还有些get,set属性的方法,为省篇幅就不贴啦
}

xml实现类

 <resultMap id="BaseResultMap" type="org.sang.bean.Article">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="cid" property="cid"/>
        <result column="uid" property="uid"/>
        <result column="publishDate" property="publishDate"/>
        <result column="editTime" property="editTime"/>
        <result column="state" property="state"/>
        <result column="pageView" property="pageView"/>
        <result column="mdContent" property="mdContent"/>
        <result column="htmlContent" property="htmlContent"/>
        <result column="summary" property="summary"/>
        <result column="nickname" property="nickname"/>
        <result column="cateName" property="cateName"/>
        <collection property="tags" ofType="org.sang.bean.Tags" column="tagName">
            <id property="id" column="tid"/>
            <result property="tagName" column="tagName"/>
        </collection>
    </resultMap>

if标签用法

比如我们常常在查询时,想在where后边加上不确定的条件束缚,此时我们便可以使用if便签,举例
where标签与if标签必须组合使用,mybatis会自动组合符合条件的查询语句

<select id="getArticleCountByState" resultType="int">
        SELECT count(*) FROM article
        <where>
            <if test="state!=-1">
                AND state=#{state}
            </if>
            <if test="uid!=null">
                AND uid=#{uid}
            </if>
         </where>
    </select>

if else条件在mybatis如何表示

使用when 和otherwise进行判断,举例
when和otherwise要在choose标签内使用,表示一种情况
在这里插入图片描述

foreach标签循环插入内容

举例

//mapper层接口
  int saveTags(@Param("tags") String[] tags);
//mapper实现xml文件
 <insert id="saveTags">
        INSERT IGNORE INTO tags(tagName) VALUES
        <foreach collection="tags" item="tag" separator=",">
            (#{tag})
        </foreach>
    </insert>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值