• 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(statement = "select last_insert_id()" , before = false , keyProperty = "goodsId" , resultType = Integer.class)
        public int insert(Goods goods);
        @Select("select * from t_goods")
        //
        @Results({
                //
              @Result(column = "goods_id" ,property = "goodsId" , id = true) ,
                //
                @Result(column = "title" ,property = "title"),
                @Result(column = "current_price" ,property = "currentPrice")
        })
        public List<GoodsDTO> selectAll();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2.mybatis-config.xml中添加mapper

    <mapper class="dao.GoodsDAO"/>
    
    • 1

    or

    <package name="dao"/>
    
    • 1

    3.测试查询方法selectByPriceRange

     	@Test
        public 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);
    
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.测试插入方法insert

    	@Test
        public 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);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    5.测试结果映射

    	@Test
        public 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);
    
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    大数据学长面试之OPPO面试题
    jvm-sandbox-repeater时间mock插件设计与实现
    2022武汉大学资源与环境学院gis专业arcpy考试:arcpy代码答案及数据输入输出
    Nginx:Tomcat部署及优化(一)
    Dockerfile COPY的奇怪行为:自动解包一级目录
    nginx安装带stream 并处理svn跳转
    每日五问(java)
    Uniapp 跳转回上一页面并刷新页面数据
    超详细的Linux环境如何搭建禅道
    分类网络搭建示例
  • 原文地址:https://blog.csdn.net/nefss_/article/details/132942061