当前位置: 首页 > news >正文

MyBatis注解开发

MyBatis常用注解

注解对应XML说明
@Insert< insert>新增SQL
@Update< update>更新SQL
@Delete< delete>删除SQL
@Select< select>查询SQL
@Param参数映射
@Results< resultMap>结果映射
@Result< id>< result>字段映射

开发流程:
1.dao包下创建接口

public interface GoodsDAO {@Select("select * from t_goods where current_price between  #{min} and #{max} order by current_price limit 0,#{limt}")public List<Goods> selectByPriceRange(@Param("min") Float min ,@Param("max") Float max ,@Param("limt") Integer limt);@Insert("INSERT INTO t_goods(title, sub_title, original_cost, current_price, discount, is_free_delivery, category_id) VALUES (#{title} , #{subTitle} , #{originalCost}, #{currentPrice}, #{discount}, #{isFreeDelivery}, #{categoryId})")//<selectKey>@SelectKey(statement = "select last_insert_id()" , before = false , keyProperty = "goodsId" , resultType = Integer.class)public int insert(Goods goods);@Select("select * from t_goods")//<resultMap>@Results({//<id>@Result(column = "goods_id" ,property = "goodsId" , id = true) ,//<result>@Result(column = "title" ,property = "title"),@Result(column = "current_price" ,property = "currentPrice")})public List<GoodsDTO> selectAll();

2.mybatis-config.xml中添加mapper

<mapper class="dao.GoodsDAO"/>

or

<package name="dao"/>

3.测试查询方法selectByPriceRange

 	@Testpublic void testSelectByPriceRange() throws Exception {SqlSession session = null;try{session = MyBatisUtils.openSession();GoodsDAO goodsDAO = session.getMapper(GoodsDAO.class);List<Goods> list = goodsDAO.selectByPriceRange(100f, 500f, 20);System.out.println(list.size());}catch (Exception e){throw e;} finally {MyBatisUtils.closeSession(session);}}

4.测试插入方法insert

	@Testpublic void testInsert() throws Exception {SqlSession session = null;try{session = MyBatisUtils.openSession();Goods goods = new Goods();goods.setTitle("测试商品");goods.setSubTitle("测试子标题");goods.setOriginalCost(200f);goods.setCurrentPrice(100f);goods.setDiscount(0.5f);goods.setIsFreeDelivery(1);goods.setCategoryId(43);GoodsDAO goodsDAO = session.getMapper(GoodsDAO.class);//insert()方法返回值代表本次成功插入的记录总数int num = goodsDAO.insert(goods);session.commit();//提交事务数据System.out.println(goods.getGoodsId());}catch (Exception e){if(session != null){session.rollback();//回滚事务}throw e;}finally {MyBatisUtils.closeSession(session);}}

5.测试结果映射

	@Testpublic void testSelectAll() throws Exception {SqlSession session = null;try{session = MyBatisUtils.openSession();GoodsDAO goodsDAO = session.getMapper(GoodsDAO.class);List<GoodsDTO> list = goodsDAO.selectAll();System.out.println(list.size());}catch (Exception e){throw e;} finally {MyBatisUtils.closeSession(session);}}
http://www.lryc.cn/news/171484.html

相关文章:

  • C# Onnx Yolov8 Cls 分类
  • Fiddler常用的快键键
  • 【Linux】生产消费模型 + 线程池
  • 基于springboot+vue的爱心助农网站(前后端分离)
  • “华为杯”研究生数学建模竞赛2019年-【华为杯】D题:汽车行驶工况构建(附获奖论文和MATLAB代码实现)
  • v-cloak的作用和原理
  • pip pip3安装库时都指向python2的库
  • 和逸云 RK3229 如何进入maskrom强刷模式
  • 防静电离子风扇的应用及优点
  • git中无法使用方向键的问题
  • 负载均衡中间件---Nginx
  • Linux硬链接、软链接
  • React面试题总结(一)
  • 一句话设计模式12:适配器模式
  • iOS加固保护技术:保护你的iOS应用免受恶意篡改
  • 阿里云产品试用系列-云桌面电脑
  • vue3使用vue-virtual-scroller虚拟滚动遇到的问题
  • c#用Gnuplot画图源码
  • 【前端设计模式】之工厂模式
  • Hive 的函数介绍
  • 【Linux基础】第31讲 Linux用户和用户组权限控制命令(三)
  • html form表单高级用法
  • openssl升级
  • 【数据结构】图的遍历:广度优先(BFS),深度优先(DFS)
  • Mysql 学习总结(89)—— Mysql 库表容量统计
  • virtualBox安装配置使用
  • 北斗导航 | RTD、RTK完好性之B值、VPL与HPL计算(附B值计算matlab源代码)
  • more often than not 的含义
  • 【Linux】Linux环境配置安装
  • 从零学习开发一个RISC-V操作系统(二)丨GCC编译器和ELF格式