INSERT INTO mybatis_monster_ (monster_id,age,birthday,email,gender,name,salary)
VALUES (#{monster_id},#{age},#{birthday},#{email},#{gender},#{name},#{salary})
@Test
public void testAdd() {
Monster monster = new Monster();
for(int i=10;i<20;i++) {
monster.setAge(100+i);
monster.setBirthday(new Date());
monster.setEmail("[email protected]");
monster.setGender(1);
monster.setSalary(8928.00);
monster.setName("银角"+i);
monsterMapper.addMonster(monster);
System.out.println("获取添加的对象id" + monster.getMonster_id());
}
}
(1)传入简单类型,比如按照id查Person (单条件查询)
(2)传入POJQ类型,查询时需要有多个筛选条件(将多个条件封装到pojo中,将pojo作为参数传入)
比如 请查询monster_id =1或者 name='白骨精'的妖怪.
注意:当有多个条件时,传入的参数就是Pojo类型的Java对象,比如这里的Monster对象,
当我们传入的是String时,可以使用 ${}接收参数
首先,在MonsterMapper中,写这个方法, 查询名字当中含有“狐狸精”的妖怪
public Monster findByName(String name);
然后,映射文件中写这个语句,注意${name}
我们可以使用HashMap来实现多条件的查询
// 查询id>10而且salary大于40的所有妖怪
public List findMonsterByIdAndSalary(Map map);
@Test
public void findMonsterByIdAndSalary(){
Map map = new HashMap();
map.put("monster_id", 6);
map.put("salary", 1234);
List monsterList = monsterMapper.findMonsterByIdAndSalary(map);
for (Monster monster : monsterList) {
System.out.println(monster);
}
}
// 查询id>10而且salary大于40的所有妖怪
// 第二种写法:参数和返回类型都是Map
public Map findMonsterByIdAndSalary2(Map map);
@Test
public void findMonsterByIdAndSalary(){
Map map = new HashMap();
map.put("monster_id", 6);
map.put("salary", 1234);
List monsterList = monsterMapper.findMonsterByIdAndSalary(map);
for (Monster monster : monsterList) {
System.out.println(monster);
}
}