三(二)mybatis接口映射sql使用注解

//指定参数名获取参数:不再通过索引
@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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值