//指定参数名获取参数:不再通过索引
@Select("select * from foods where foodname=#{foodName} and price=#{price}")
public abstract List<Map> selectFood(@Param("foodName") String foodName,@Param("price") String price);
@Select("select * from foods where foodname like '%${foodName}%'")
public abstract List<Food> selectFoodByFoodName(@Param("foodName") String foodName);
@SelectKey(before=true,keyProperty="foodId",statement="select foods_scr.nextval from dual", resultType = int.class)
@Insert("insert into foods values(#{foodId},#{foodName},#{price})")
public abstract void saveFood(Food food);
private SqlSession getSession() throws IOException {
// mybatis核心配置文件路径
String resource = "cn/et/lesson2/annotion/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// session操作的是指向sql语句的一个唯一标识符
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
@Test
public void selectFood() throws IOException {
SqlSession openSession = getSession();
FoodInter mapper = openSession.getMapper(FoodInter.class);
List selectFood = mapper.selectFood("dd", "55");
System.out.println(selectFood);
}
@Test
public void selectFoodByName() throws IOException {
SqlSession openSession = getSession();
FoodInter mapper = openSession.getMapper(FoodInter.class);
List selectFood = mapper.selectFoodByFoodName("c");
System.out.println(selectFood);
}
@Select("select * from foods where foodname=#{foodName} and price=#{price}")
public abstract List<Map> selectFood(@Param("foodName") String foodName,@Param("price") String price);
@Select("select * from foods where foodname like '%${foodName}%'")
public abstract List<Food> selectFoodByFoodName(@Param("foodName") String foodName);
@SelectKey(before=true,keyProperty="foodId",statement="select foods_scr.nextval from dual", resultType = int.class)
@Insert("insert into foods values(#{foodId},#{foodName},#{price})")
public abstract void saveFood(Food food);
private SqlSession getSession() throws IOException {
// mybatis核心配置文件路径
String resource = "cn/et/lesson2/annotion/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// session操作的是指向sql语句的一个唯一标识符
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
@Test
public void selectFood() throws IOException {
SqlSession openSession = getSession();
FoodInter mapper = openSession.getMapper(FoodInter.class);
List selectFood = mapper.selectFood("dd", "55");
System.out.println(selectFood);
}
@Test
public void selectFoodByName() throws IOException {
SqlSession openSession = getSession();
FoodInter mapper = openSession.getMapper(FoodInter.class);
List selectFood = mapper.selectFoodByFoodName("c");
System.out.println(selectFood);
}