记录mybatis学习
普通动态插入方式(数据库不含键,可任选字段插入):
<insert id="ins" parameterType="mybatistest1.Books">
insert into books
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="bno!=null">
bno,
</if>
<if test="bname!=null and bname!=''">
bname,
</if>
<if test="bauthor!=null and bauthor!=''">
bauthor,
</if>
<if test="byear!=null">
byear
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="bno!=null">
#{bno},
</if>
<if test="bname!=null and bname!=''">
#{bname},
</if>
<if test="bauthor!=null and bauthor!=''">
#{bauthor},
</if>
<if test="byear!=null">
#{byear}
</if>
</trim>
</insert>
动态插入数据库配置文件mapper.xml,需要注意…中的小括号
<mapper namespace="book" >
<insert id="ins" parameterType="mybatistest1.Books">
insert into books
values
<foreach collection="list" item="m" separator=",">
(#{m.bno},#{m.bname},#{m.bauthor},#{m.byear})//此时集合中为一个对象,本次运行对象名为m 需要将对象中的属性对应到表中字段名上去
//**请高手讲解m.xx能映射到对对应字段上的原因**不胜感激
</foreach>
</insert>
</mapper>
相关测试没问题
@Test
public void testt() {
List<Books> list=new ArrayList<Books>();
list.add(new Books(1,"a","a",null));
list.add(new Books(2,"b","a",null));
list.add(new Books(3,"c","a",null));
session.insert("book.ins", list);
}
插入数据量不宜过大,查到如果超过一兆会报错需要 通过调整MySQL安装目录下的my.ini文件中[mysqld]段的"max_allowed_packet = 1M"来调整
可以配合一起使用
逐条删除
<delete id="del">
delete from books where bno = #{bno}//普通逐条删除
</delete>
批量删除
<!-- forEach : 用来循环 collection : 用来指定循环的数据的类型 可以填的值有:array,list,map item
: 循环中为每个循环的数据指定一个别名 index : 循环中循环的下标 open : 开始 close : 结束 separator : 数组中元素之间的分隔符 -->
<delete id="dell">
delete from books
<where>
bno in
<foreach collection="list" open="(" close=")" separator="," item="m" >
#{m} <!-- #{}中内容与item名字一致, -->
</foreach>
</where>
</delete>
测试
@Test
public void testt() {
List<Books> list=new ArrayList<Books>();
list.add(new Books(1,"a","a",null));
list.add(new Books(2,"b","a",null));
list.add(new Books(3,"c","a",null));
int [] aa={1,2,3};
List<Integer> ls=new ArrayList<Integer>();
ls.add(1);
ls.add(2);
// session.delete("book.dell", ls);
//session.delete("book.del", aa);
session.insert("book.ins", list);
}
更新学习
首先是无条件全部更新,没意义纯属练习语法(并且set中的if应该用","分割而不是and,但语法确实能够跑通)
<update id="upd">
update books
<set>//更改属性设置
<trim suffixOverrides="and">//在更改条件间自动加入and 连接
<if test="bno!=null">//判断是否需要更新该字段
bno=#{bno}
</if>
<if test="bname!=null and bname!=''">
and bname=#{bname}
</if>
<if test="bauthor!=null and bauthor!=''">
and bauthor=#{bauthor}
</if>
<if test="byear!=null and byear!=''">
and byear=#{byear}
</if>
</trim>
</set>
//该语句能跑通,但没什么意义仅仅是语法上没什么问题而已,语句应该根据实际需要编写
<where>
<trim suffixOverrides="and">
<if test="bno!=null">
bno=#{bno}
</if>
<if test="bname!=null and bname!=''">
and bname=#{bname}
</if>
<if test="bauthor!=null and bauthor!=''">
and bauthor=#{bauthor}
</if>
<if test="byear!=null and byear!=''">
and byear=#{byear}
</if>
</trim>
</where>
下面是有意义更新并且 Set语句中if采用","分隔
<update id="upd">
update books
<set>
<trim suffixOverrides=",">
<!-- <if test="bno!=null">
bno=#{bno}
</if> -->
<if test="bname!=null and bname!=''">
<!-- and --> bname=#{bname},
</if>
<if test="bauthor!=null and bauthor!=''">
bauthor=#{bauthor},
</if>
<if test="byear!=null and byear!=''">
byear=#{byear},
</if>
</trim>
</set> where bno=#{bno}
到这里增删改都做过一遍了,
首先要注意"一定要熟悉数据库语句一定要熟悉数据库语句*一定要熟悉数据库语句"
其次是对于mybatis中的标记多多联系,组合方式有很多种,前提是你能在数据库中写出正确的语句,然后利用标记拼mapper中的sql就可以了