后端开发技巧

  1. 新增和修改可以写在同一个controller方法,也就是用同一个请求地址处理,因为传递的值基本一致,利用id是否为空进行区分调用哪一个service,新增id自增,不需要传id
 public void saveOrUpdateSection(HttpServletRequest request ,HttpServletResponse response){

        try {
            //1.获取参数  从域对象中获取
            Map<String,Object> map = (Map)request.getAttribute("map");

            //2.创建Course_Section
            Course_Section section = new Course_Section();

            //3.使用BeanUtils工具类,将map中的数据封装到 section
            BeanUtils.populate(section,map);

            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();

            //判断是否携带id
            if(section.getId() == 0){
                //新增操作
                String result = contentService.saveSection(section);
                //5.响应结果
                response.getWriter().print(result);

            }else{
                //修改操作
                String result = contentService.updateSection(section);
                response.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  1. 多表查询不写链表查询,而是用 在dao循环嵌套两个单表查询代替
@Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {

        try {
            //1.创建QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            //2.编写SQL
            String sql = "SELECT \n" +
                    "id,\n" +
                    "course_id,\n" +
                    "section_name,\n" +
                    "description,\n" +
                    "order_num,\n" +
                    "STATUS\n" +
                    "FROM course_section WHERE course_id = ?";

            //3.执行查询
            List<Course_Section> sectionList = qr.query(sql, new BeanListHandler<Course_Section>(Course_Section.class), courseId);

            //4.根据章节ID查询课时信息
            for (Course_Section section : sectionList) {

                //调用方法 获取章节对应的课时
                List<Course_Lesson> lessonList = findLessonBySectionId(section.getId());

                //将课时数据封装到 章节对象中
                section.setLessonList(lessonList);
            }

            return sectionList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }


    //根据章节ID查询课时信息
    @Override
    public List<Course_Lesson> findLessonBySectionId(int sectionId) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "SELECT \n" +
                    "id,\n" +
                    "course_id,\n" +
                    "section_id,\n" +
                    "theme,\n" +
                    "duration,\n" +
                    "is_free,\n" +
                    "order_num,\n" +
                    "STATUS\n" +
                    "FROM course_lesson WHERE section_id = ?";

            List<Course_Lesson> lessonList = qr.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class), sectionId);

            return lessonList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

或者创建实体类直接接收

 <resultMap id="ad_space" type="com.lagou.domain.PromotionAd">

        <id property="id" column="id"></id>
        <result property="name" column="name"/>
        <result property="spaceId" column="spaceId"/>
        <result property="keyword" column="keyword"/>
        <result property="htmlContent" column="htmlContent"/>
        <result property="text" column="text"/>
        <result property="link" column="link"/>
        <result property="startTime" column="startTime"/>
        <result property="endTime" column="endTime"/>
        <result property="createTime" column="createTime"/>
        <result property="updateTime" column="updateTime"/>
        <result property="status" column="status"/>
        <result property="priority" column="priority"/>
        <result property="img" column="img"/>

        <association property="promotionSpace" select="com.lagou.dao.PromotionSpaceMapper.findPromotionSpaceById" column="spaceId" javaType="com.lagou.domain.PromotionSpace"></association>

    </resultMap>


    <!--分页查询广告信息-->
    <select id="findAllPromotionAdByPage" resultMap="ad_space">
        select * from promotion_ad
    </select>

    // 标识
    private Integer id;
    // 广告名
    private String name;
    // 广告位id
    private Integer spaceId;
    // 精确搜索关键词
    private String keyword;
    // 静态广告的内容
    private String htmlContent;
    // 文字一
    private String text;
    // 链接一
    private String link;
    // 开始时间

    private Date startTime;
    // 结束时间
    private Date endTime;
    private Date createTime;
    private Date updateTime;
    private Integer status;
    // 优先级
    private Integer priority;
    private String img;

    //声明一方关系:PromotionSpace
    private PromotionSpace promotionSpace;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值