• MyBatis的使用2(使用注解配置mapper)


    配置mapper的type使用package(使用注解来配置mapper):

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <properties resource="jdbc.properties"/>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${driver}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${username}"/>
                    <property name="password" value="${password}"/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <package name="com.imooc.cake.mapper"/>      <!---使用注解来配置mapper---->
        </mappers>
    </configuration>

    编写接口:

    package com.imooc.cake.mapper;
    
    import com.imooc.cake.entity.Cake;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Result;
    import org.apache.ibatis.annotations.Results;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    /**
     *
     * 蛋糕
     *
     * @version 1.0
     */
    public interface CakeMapper {
    
        /**
         * 分页查询蛋糕
         * @param skip  跳过的记录数,也就是从哪条开始查询
         * @param size  要查询的记录数
         * @return  蛋糕集合
         */
        @Select("select * from cake order by create_time desc limit #{skip}, #{size}")
        @Results({
                    @Result(id = true, column = "id", property = "id"),
                    @Result(column = "category_id", property = "categoryId"),
                    @Result(column = "name", property = "name"),
                    @Result(column = "level", property = "level"),
                    @Result(column = "price", property = "price"),
                    @Result(column = "create_time", property = "createTime"),
                    @Result(column = "update_time", property = "updateTime")
                })
        List<Cake> getCakes(@Param("skip") Integer skip, @Param("size") Integer size);
    
        /**
         * 根据分类分页查询蛋糕
         * @param categoryId    蛋糕分类ID
         * @param skip  跳过的记录数,也就是从哪条开始查询
         * @param size  要查询的记录数
         * @return  蛋糕集合
         */
        @Select("select id, category_id categoryId, name, level, price, create_time createTime, update_time updateTime " +
                "from cake where category_id = #{categoryId} order by create_time desc limit #{skip}, #{size}")
        List<Cake> getCakesByCategoryId(@Param("categoryId")Long categoryId, @Param("skip") Integer skip,  @Param("size") Integer size);
    
        /**
         * 根据分类ID进行蛋糕数量的统计
         * @param categoryId    分类ID
         * @return  分类下蛋糕数量
         */
        @Select("select count(*) from cake where category_id = #{categoryId}")
        int countCakesByCategoryId(@Param("categoryId")Long categoryId);
    
        /**
         * 保存蛋糕信息
         * @param cake  蛋糕信息
         */
        @Insert("insert into cake(category_id, name, level, price, small_img, create_time, update_time) " +
                "value (#{cake.categoryId}, #{cake.name}, #{cake.level}, #{cake.price}, #{cake.smallImg}, " +
                "#{cake.createTime}, #{cake.updateTime})")
        void addCake(@Param("cake") Cake cake);
    
        /**
         * 查询蛋糕图片信息
         * @param id    蛋糕ID
         * @return  只包含图片的蛋糕实体
         */
        @Select("select small_img smallImg from cake where id = #{id} for update")
        Cake getImg(@Param("id")Long id);
    
    }
    package com.imooc.cake.mapper;
    
    import com.imooc.cake.entity.Category;
    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    /**
     *
     * 分类
     *
     * @version 1.0
     */
    public interface CategoryMapper {
    
        /**
         * 查询全部蛋糕分类
         * @return  全部蛋糕分类
         */
        @Select("select id, name, create_time createTime, update_time updateTime from category")
        List<Category> getCategories();
    
        /**
         * 根据ID删除某一具体分类
         * @param id    要删除的分类ID
         */
        @Delete("delete from catetory where id = #{id}")
        void deleteById(Long id);
    
        /**
         * 插入蛋糕分类信息
         * @param category  蛋糕分类实体
         */
        @Insert("insert into category(name, create_time, update_time) values(#{name}, #{createTime}, #{updateTime})")
        void addCategory(Category category);
    
    }
  • 相关阅读:
    汇编--基础分析:数据段在内存中的存放及空间
    汇编语言(王爽)第六章检测点与实验5
    C语言经典例题100(22~40)
    C:数组小结(3)
    随机不重复的取数组元素,并赋值给div使用
    关于Apple设备私有的apple-touch-icon属性详解
    测试你女友是否跟你结婚插件的源码
    CSS子元素设置margin-top作用于父容器?
    hybrid app开发中:苹果移动设备实用Meta标签
    chromium 修改chromium的设置选项
  • 原文地址:https://www.cnblogs.com/shouyaya/p/12495616.html
Copyright © 2020-2023  润新知