新增狗狗,动态新增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);
}
}
输出结果:含带黑字的狗的名字