mabatis的增加与查询

新增狗狗,动态新增Dog:
1.在接口IDogDao中返回一个 Interger add(Dog dog),在dog中实现接口add方法;

2.在DogDao中输入代码
<insert id="add">
   insert into dog()
     <trim prefix="("suffix=")" suffixOverrides=",">
<if test="name!=null and name !='' ">
       name,
</if>
<if test="health!=null ">
      health,
</if>
<if test="love!=null">
      love,
</if>
<if test="strain!=null and strain!='' ">
      strain,
</if>
<if test="datetime!=null">
      datetime,
</if>
</trim>
   values
<trim prefix="("suffix=")" suffixOverrides=",">
  <if test="name!=null and name !='' ">
       #{name},
</if>
<if test="health!=null ">
      #{health},
</if>
<if test="love!=null">
      #{love},
</if>
<if test="strain!=null and strain!='' ">
      #{strain},
</if>
<if test="datetime!=null">
      #{datetime},
</if>
</trim>
</insert>

3.在Dogtest类中输入运行代码:
@Test
public void testAdd(){
Dog dog =new dog();
dog.setName("农夫山泉");
dog.setStrain("泰迪");
Interger num=dogDao.add(dog);
System.out.println(num);}

运行结果:在数据库的dog种类中增加了一条名字为农夫山泉,种类为泰迪的狗;

@test
public void testAdd(){
Dog dog=new Dog();
dog.setName("有点甜");
dog.setLove(100);
dog.setHealth(100);
dog.setStrain("藏獒");
dog.setDatetime(new Date(System.currentTimeMillis()));
Interger num=dogDao.add(dog);
System.out.println(num);}

运行结果:在数据库中增加了一条名字为有点甜,亲密度为100,健康值为100,种类为藏獒的狗狗

动态更新Dog数据:
1.在IDogDao接口中返回一个Integer update2(Dog dog)接口,在Dog中实现这个方法;

2.在DogDao中输入代码:
<update id="update2">
update dog
<set>
<if test="name!=null and name!='' ">
name=#{name},
</if>
<if test="health!=null">
health=#{health},
</if>
<if test="love!=null">
love=#{love}
</if>
</set>
where id=#{id}
</update>

3.在DogTest中输入运行代码:
@Test
public voidtestUpdate(){
Dog dog=new Dog()
dog.setId(11);
dog.setName("黑皮");
dog.setLove(98);
dog.setHealth(60);
Integer num=dogDao.update2(dog);
System.out.println(num);}

运行结果:在数据库中将id为11的狗改为名字为黑皮,亲密度为98,健康值为60的狗狗


根据dogId,删除多条狗狗
1.在IDogDao接口中中返回Integer delete2(List<Integer> dogIds),在Dog中实现方法

2.<delete id=delete2>
       delete from dog
where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>

3.在DogTest中测试:
@Test
public void testDelete2(){
ArrayList<Integer> ids=new ArrayList<integer>();
ids.add(10);
ids.add(11);
ids.add(12);
Integer num =dogDao.detele2(ids);
System.out.println(num);}

运行结果:返回的结果为3,在数据库中,id为10,11,12的三个数据被删除

 // 动态查询
1.在IDogDao接口中输入:
    List<Dog> getDogByNameAndLove(
            @Param("dogName") String dogName,
            @Param("dogLove") Integer love,
            @Param("orderType") Integer orderType);
2.在DogDao.xml中输入:
<select id="getDogByNameAndLove" resultType="dog">
      select d.id,d.name,d.health,d.love,d.strain,d.datetime 
      from dog d
      <where>
          <if test="dogName!=null and dogName!='' ">
                d.name like concat('%',#{dogName},'%')
         <if test="dogLove!=null">
             and love=#{dogLove}
         </if>
          </if>
      </where>
      <choose>
          <when test="orderType==1">
              order by d.datetime asc
          </when>
          <when test="orderType==2">
              order by d.datetime desc
          </when>
          <otherwise>
              order by d.id desc
          </otherwise>

      </choose>
    </select>
3.在Dogtest中输出代码:
@Test
    public void testGetDogByNameAndLove(){
        List<Dog> dogs = dogDao.getDogByNameAndLove("黑", null, 2);
        for (Dog dog:
                dogs
             ) {
            System.out.println(dog);
        }
    }

输出结果:含带黑字的狗的名字
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值