完成一个springboot项目的完整总结------三

这一次的总结是最关键的部分,主要涉及了ORM的三种操作,这些操作是项目的难点,三种操作包括多对一、多对多、一对多三种模式,接下来展示项目代码

1.多对一

clazz表对应grade表和charge表

bean

      extend

                ClazzVM.java

package com.briup.apps.poll.bean.extend;

import com.briup.apps.poll.bean.Grade;
import com.briup.apps.poll.bean.User;

public class ClazzVM {
    private Long id;
    private String name;
    private String description;
    private Grade grade;
    private User charge;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Grade getGrade() {
        return grade;
    }
    public void setGrade(Grade grade) {
        this.grade = grade;
    }
    public User getCharge() {
        return charge;
    }
    public void setCharge(User charge) {
        this.charge = charge;
    }
}

dao

     extend

               ClazzVMMapper.java

package com.briup.apps.poll.dao.extend;

import java.util.List;

import com.briup.apps.poll.bean.extend.ClazzVM;

public interface ClazzVMMapper {
    
    List<ClazzVM> selectAll();
    
    ClazzVM selectById(long id);
}

resources/mapper/extend/ClazzVMMapper.xml                  

和dao层功能相对应,实现查找功能

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.briup.apps.poll.dao.extend.ClazzVMMapper">
     <select id="selectAll" resultMap="ClazzVMResultMap">
         select * from poll_clazz
         <!-- id,name,description,grade_id,charge_id -->
     </select>
     <select id="selectById" parameterType="long" resultMap="ClazzVMResultMap">
         select * from poll_clazz where id = #{id}
         <!-- id,name,description,grade_id,charge_id -->
     </select>
     
     <!-- 定义结果集 -->
     <resultMap type="com.briup.apps.poll.bean.extend.ClazzVM" id="ClazzVMResultMap">
         <id column="id" property="id"/>
         <result column="name" property="name"/>
         <result column="description" property="description"/>
         <association 
             column="grade_id" 
             property="grade"
             select="com.briup.apps.poll.dao.GradeMapper.selectByPrimaryKey">
         </association>
         <association 
             column="charge_id" 
             property="charge"
             select="com.briup.apps.poll.dao.UserMapper.selectByPrimaryKey">
         </association>
     </resultMap>
</mapper>

标记的部分是将clazz表中的属性一一对应,在查找clazz的时候同时查找出clazz对应的年级和班主任信息

service 

        Impl

                  ClazzVMserviceImpl,java    实现所有IClazzVMService定义的功能,调用数据库层

        IClazzVMService    定义方法,具体实现由Impl实现

        

package com.briup.apps.poll.service;

import java.util.List;

import com.briup.apps.poll.bean.Clazz;
import com.briup.apps.poll.bean.extend.ClazzVM;

public interface IClazzService {
    List<Clazz> findAll() throws Exception;
    
    List<ClazzVM> findAllClazzVM() throws Exception;
    
    void saveOrUpdateClazz(Clazz clazz) throws Exception;
    
    void deleteById(long id) throws Exception;
    
    void batchDelete(long[] ids) throws Exception;
}
package com.briup.apps.poll.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.apps.poll.bean.Clazz;
import com.briup.apps.poll.bean.ClazzExample;
import com.briup.apps.poll.bean.extend.ClazzVM;
import com.briup.apps.poll.dao.ClazzMapper;
import com.briup.apps.poll.dao.extend.ClazzVMMapper;
import com.briup.apps.poll.service.IClazzService;

@Service
public class ClazzServiceImpl implements IClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private ClazzVMMapper clazzVMMapper;
    
    @Override
    public List<Clazz> findAll() throws Exception {
        ClazzExample example = new ClazzExample();
        return clazzMapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public List<ClazzVM> findAllClazzVM() throws Exception {
        return clazzVMMapper.selectAll();
    }

    @Override
    public void saveOrUpdateClazz(Clazz clazz) throws Exception {
        if(clazz.getId()!=null){
            clazzMapper.updateByPrimaryKey(clazz);
        } else {
            clazzMapper.insert(clazz);
        }
    }

    @Override
    public void deleteById(long id) throws Exception {
        clazzMapper.deleteByPrimaryKey(id);
        
    }

    @Override
    public void batchDelete(long[] ids) throws Exception {
        for(long id : ids) {
            clazzMapper.deleteByPrimaryKey(id);
        }
    }

}

controller   调用service接口

       ClazzController.java

package com.briup.apps.poll.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.apps.poll.bean.Clazz;
import com.briup.apps.poll.bean.extend.ClazzVM;
import com.briup.apps.poll.service.IClazzService;
import com.briup.apps.poll.util.MsgResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description="班级相关接口")
@RestController
@RequestMapping("/clazz")
public class ClazzController {
    @Autowired
    private IClazzService clazzService;
    
    @ApiOperation(value="保存或修改班级信息",
            notes="如果参数中包含ID表示修改操作,否则表示保存操作")
    @PostMapping("saveOrUpdateClazz")
    public MsgResponse saveOrUpdateClazz(Clazz clazz){
        try {
            clazzService.saveOrUpdateClazz(clazz);
            return MsgResponse.success("保存或更新成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
    }
    @ApiOperation(value="批量删除班级信息",
            notes="参数为数组")
    @PostMapping("batchDeleteClazz")
    public MsgResponse batchDeleteClazz(long[] ids){
        try {
            clazzService.batchDelete(ids);
            return MsgResponse.success("批量删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value="通过ID删除班级信息",
            notes="")
    @GetMapping("deleteClazzById")
    public MsgResponse deleteClazzById(long id){
        try {
            clazzService.deleteById(id);
            return MsgResponse.success("删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value="查询所有班级",
            notes="班级中携带班级所属年级信息以及班主任信息")
    @GetMapping("findAllVM")
    public MsgResponse findAllVM(){
        try {
            List<ClazzVM> list = clazzService.findAllClazzVM();
            return MsgResponse.success("success", list);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value="查询所有班级",notes="单表")
    @GetMapping("findAll")
    public MsgResponse findAll(){
        try {
            List<Clazz> list = clazzService.findAll();
            return MsgResponse.success("success", list);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
    }
}

补充:多对一的保存和更新操作不需要保存班级和班主任信息,只需要保存两者id

2.一对多

    question表对应options表,在查找question时需要把options的信息一起显示,保存问题时需要将问题和该问题的选项同时保存

bean

      extend

              QuestionVM.java

package com.briup.apps.poll.bean.extend;

import java.util.List;

import com.briup.apps.poll.bean.Options;

public class QuestionVM {
    private Long id;
    private String name;
    private String questionType;
    
    private List<Options> options;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getQuestionType() {
        return questionType;
    }

    public void setQuestionType(String questionType) {
        this.questionType = questionType;
    }

    public List<Options> getOptions() {
        return options;
    }

    public void setOptions(List<Options> options) {
        this.options = options;
    }
    
}

dao

     extend

               QuestionVMMapper.java   只实现的时查询功能

package com.briup.apps.poll.dao.extend;

import java.util.List;

import com.briup.apps.poll.bean.extend.QuestionVM;

public interface QuestionVMMapper {
    
    List<QuestionVM> selectAll();
    
}

/resources/extend/QuestionVMMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.briup.apps.poll.dao.extend.QuestionVMMapper">
     <select id="selectAll" 
         resultMap="QuestionVMResultMap">
         select * from poll_question
         <!-- id,name,questionType -->
     </select>

<!-- 定义结果集 --> <resultMap type="com.briup.apps.poll.bean.extend.QuestionVM" id="QuestionVMResultMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="questionType" property="questionType"/> <collection column="id" property="options" javaType="ArrayList" ofType="com.briup.apps.poll.bean.Options" select="selectOptionsByQuestionId"> </collection> </resultMap> <!-- 通过题目id查询属于该题目的选项信息 --> <select id="selectOptionsByQuestionId" parameterType="long" resultType="com.briup.apps.poll.bean.Options"> select * from poll_options where question_id = #{id} </select> </mapper>

service

      impl 

             QuestionVMServiceImpl.java

IQuestionVMService

package com.briup.apps.poll.service;

import java.util.List;

import com.briup.apps.poll.bean.Question;
import com.briup.apps.poll.bean.extend.QuestionVM;

public interface IQuestionService {
    List<Question> findAll() throws Exception;
    
    List<QuestionVM> findAllQuestionVM() throws Exception;
    
    void saveOrUpdateQuestionVM(QuestionVM questionVM) throws Exception;
    
    void deleteById(long id) throws Exception;
    
    void batchDelete(long[] ids) throws Exception;
}
package com.briup.apps.poll.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.apps.poll.bean.Options;
import com.briup.apps.poll.bean.OptionsExample;
import com.briup.apps.poll.bean.Question;
import com.briup.apps.poll.bean.QuestionExample;
import com.briup.apps.poll.bean.extend.QuestionVM;
import com.briup.apps.poll.dao.OptionsMapper;
import com.briup.apps.poll.dao.QuestionMapper;
import com.briup.apps.poll.dao.extend.QuestionVMMapper;
import com.briup.apps.poll.service.IQuestionService;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private QuestionVMMapper questionVMMapper;
    @Autowired
    private OptionsMapper optionsMapper;
    
    @Override
    public List<Question> findAll() throws Exception {
        QuestionExample example = new QuestionExample();
        return questionMapper.selectByExample(example);
    }

    @Override
    public List<QuestionVM> findAllQuestionVM() throws Exception {
        return questionVMMapper.selectAll();
    }

    /**
     * 保存或修改问题(包含选项)
     * */
    @Override
    public void saveOrUpdateQuestionVM(QuestionVM questionVM) throws Exception {
        //1. 分离questionVM,从中获取到Question Options
        List<Options> options = questionVM.getOptions();
        Question question = new Question();
        question.setId(questionVM.getId());
        question.setName(questionVM.getName());
        question.setQuestiontype(questionVM.getQuestionType());
        
        //question 问题对象,options 所有问题的选项
        //2. 判断保存还是修改
        if(question.getId() == null){
            //2.1 保存
            if(question.getQuestiontype().equals("简答题")){
                //2.1.1 保存简答题,只需要保存题目相关信息
                questionMapper.insert(question);
            } else {
                //2.1.2 保存单选和多选题的时候需要先保存题目信息,再保存选项信息
                questionMapper.insert(question);
                //如何获取刚刚插入到问题的ID
                long questionId = question.getId();
                for(Options option : options){
                    //为每个option设置question_id
                    option.setQuestionId(questionId);
                    //保存选项
                    optionsMapper.insert(option);
                }
            }
        } else {
            //2.2 修改
            //2.2.1 修改题目信息
            questionMapper.updateByPrimaryKey(question);
            //2.2.2 修改选项信息(添加新选项,删除旧选项,对原来选项修改)
            //1. 删除该题目原有的选项
            OptionsExample example = new OptionsExample();
            example.createCriteria().andQuestionIdEqualTo(question.getId());
            optionsMapper.deleteByExample(example);
            //2. 重新添加选项
            long questionId = question.getId();
            for(Options option : options){
                //为每个option设置question_id
                option.setQuestionId(questionId);
                //保存选项
                optionsMapper.insert(option);
            }
        }
    }

    @Override
    public void deleteById(long id) throws Exception {
        questionMapper.deleteByPrimaryKey(id);
        
    }

    @Override
    public void batchDelete(long[] ids) throws Exception {
        for(long id : ids) {
            questionMapper.deleteByPrimaryKey(id);
        }
    }

}

补充:在实现保存功能时,没有输入Question的id,所以如果想要获取该id,需要修改QuestionMapper.xml中的insert方法

<insert id="insert" keyProperty="id" useGeneratedKeys="true" parameterType="com.briup.apps.poll.bean.Question">

加入两个属性keyProperty和useGeneratedKeys,这样就可以获取该问题的id了。

 controller

      QuestionController.java

package com.briup.apps.poll.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.apps.poll.bean.Question;
import com.briup.apps.poll.bean.extend.QuestionVM;
import com.briup.apps.poll.service.IQuestionService;
import com.briup.apps.poll.util.MsgResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description="题库相关的接口")
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private IQuestionService questionService;
    
    @ApiOperation(value="通过ID删除问题",
            notes="删除题目的同时会把题目下所有的选项也给删除掉")
    @GetMapping("deleteQuestionById")
    public MsgResponse deleteQuestionById(long id){
        try {
            questionService.deleteById(id);
            return MsgResponse.success("删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value="批量删除问题",
            notes="")
    @PostMapping("batchDeleteQuestion")
    public MsgResponse batchDeleteQuestion(long[] ids){
        try {
            questionService.batchDelete(ids);
            return MsgResponse.success("删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
        
    }
    
    @ApiOperation(value="保存或修改问题",
            notes="当id不为空表示修改,否则表示更新,保存和更新的时候需要提交选项数据")
    @PostMapping("saveOrUpdateQuestion")
    public MsgResponse saveOrUpdateQuestion(QuestionVM questionVM){
        try {
            questionService.saveOrUpdateQuestionVM(questionVM);
            return MsgResponse.success("保存成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
        
    }
    
    
    @ApiOperation(value="查询所有问题",notes="单表")
    @GetMapping("findAllQuestion")
    public MsgResponse findAllQuestion(){
        try {
            List<Question> list = questionService.findAll();
            // 返回成功信息
            return MsgResponse.success("success", list);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回失败信息
            return MsgResponse.error(e.getMessage()) ;
        }
    }
    
    @ApiOperation(value="查询所有问题",notes="问题中包含该问题所有的属性信息")
    @GetMapping("findAllQuestionVM")
    public MsgResponse findAllQuestionVM(){
        try {
            List<QuestionVM> list = questionService.findAllQuestionVM();
            // 返回成功信息
            return MsgResponse.success("success", list);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回失败信息
            return MsgResponse.error(e.getMessage()) ;
        }
    }
}

3.多对多

问卷和问题的关系是多对多关系,他们之间有个关系表qq,用来保存question_id和questionnaire_id

bean

   extend

         QuestionnaireVM.java

package com.briup.apps.poll.bean.extend;

import java.util.List;

import io.swagger.annotations.Api;

@Api(value="问卷模型,问卷中包含多个问题,如果问题是单选和多选题,该问题也应该包含选项信息")
public class QuestionnaireVM {
    private Long id;
    private String name;
    private String description;
    
    private List<QuestionVM> questionVMs;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<QuestionVM> getQuestionVMs() {
        return questionVMs;
    }

    public void setQuestionVMs(List<QuestionVM> questionVMs) {
        this.questionVMs = questionVMs;
    }
}

dao

  extend 

     QuestionnaireVMMapper.java

package com.briup.apps.poll.dao.extend;

import com.briup.apps.poll.bean.extend.QuestionnaireVM;

public interface QuestionnaireVMMapper {
    
    QuestionnaireVM selectById(long id);
}

/resources/extend/QuestionnaireVMMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.briup.apps.poll.dao.extend.QuestionnaireVMMapper">
     <select id="selectById" resultMap="QuestionnaireVMResultMap">
         select * from poll_questionnaire where id = #{id}
         <!-- id,name,description -->
     </select>
     
     <!-- 定义结果集 -->
     <resultMap type="com.briup.apps.poll.bean.extend.QuestionnaireVM" id="QuestionnaireVMResultMap">
         <id column="id" property="id"/>
         <result column="name" property="name"/>
         <result column="description" property="description"/>
         <collection 
             column="id"
             property="questionVMs"
             javaType="ArrayList"
             ofType="com.briup.apps.poll.bean.extend.QuestionVM"
             select="com.briup.apps.poll.dao.extend.QuestionVMMapper.selectByQuestionnaireId">
         </collection>
     </resultMap>
     
</mapper>

补充:注意这里调用了QuestionVMMapper.java中的功能,通过questionnaire的id查找所有的question。

新的QuestionVMMapper.java多声名了一个selectByQuestionnaireId的方法

package com.briup.apps.poll.dao.extend;

import java.util.List;

import com.briup.apps.poll.bean.extend.QuestionVM;

public interface QuestionVMMapper {
    
    List<QuestionVM> selectAll();
    
    List<QuestionVM> selectByQuestionnaireId(long id);
}

新的QuestionVMMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.briup.apps.poll.dao.extend.QuestionVMMapper">
     <select id="selectAll" 
         resultMap="QuestionVMResultMap">
         select * from poll_question
         <!-- id,name,questionType -->
     </select>
     
     <select id="selectByQuestionnaireId" 
         parameterType="long" 
         resultMap="QuestionVMResultMap">
         select * from poll_question where id in (
             select question_id 
             from poll_qq
             where questionnaire_id = #{id}
         )
         
     </select>
     
     <!-- 定义结果集 -->
     <resultMap type="com.briup.apps.poll.bean.extend.QuestionVM" id="QuestionVMResultMap">
         <id column="id" property="id"/>
         <result column="name" property="name"/>
         <result column="questionType" property="questionType"/>
         <collection 
             column="id"
             property="options"
             javaType="ArrayList"
             ofType="com.briup.apps.poll.bean.Options"
             select="selectOptionsByQuestionId">
         </collection>
     </resultMap>
     
     <!-- 通过题目id查询属于该题目的选项信息 -->
     <select id="selectOptionsByQuestionId" 
     parameterType="long"
     resultType="com.briup.apps.poll.bean.Options">
         select * from poll_options where question_id = #{id}
     </select>
     
</mapper>

主要就是调用了QustionVMMapper.xml中标记的部分

service

   impl

        QuestionnaireServiceImpl.java

package com.briup.apps.poll.service;

import java.util.List;

import com.briup.apps.poll.bean.Questionnaire;
import com.briup.apps.poll.bean.extend.QuestionnaireVM;

public interface IQuestionnaireService {
    List<Questionnaire> findAll() throws Exception;
    
    QuestionnaireVM findById(long id) throws Exception;
    
    void saveOrUpdate(Questionnaire questionnaire , long[] questionIds) throws Exception;

    void deleteById(long id) throws Exception;
    
    void batchDelete(long[] ids) throws Exception;
}
package com.briup.apps.poll.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.apps.poll.bean.Questionnaire;
import com.briup.apps.poll.bean.QuestionnaireExample;
import com.briup.apps.poll.bean.QuestionnaireQuestion;
import com.briup.apps.poll.bean.QuestionnaireQuestionExample;
import com.briup.apps.poll.bean.extend.QuestionnaireVM;
import com.briup.apps.poll.dao.QuestionnaireMapper;
import com.briup.apps.poll.dao.QuestionnaireQuestionMapper;
import com.briup.apps.poll.dao.extend.QuestionnaireVMMapper;
import com.briup.apps.poll.service.IQuestionnaireService;

@Service
public class QuestionnaireServiceImpl implements IQuestionnaireService {
    @Autowired
    private QuestionnaireMapper qnMapper;
    @Autowired
    private QuestionnaireVMMapper qnVMMapper;
    @Autowired
    private QuestionnaireQuestionMapper qqMapper;
    
    
    @Override
    public List<Questionnaire> findAll() throws Exception {
        QuestionnaireExample example = new QuestionnaireExample();
        return qnMapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public QuestionnaireVM findById(long id) throws Exception {
        return qnVMMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(Questionnaire questionnaire,
            long[] questionIds) throws Exception {
        //1. 判断是保存操作还是更新
        if(questionnaire.getId() == null){
            //1.1 保存
            //1.1.1 保存问卷信息 {id:null,name:'',description:''}
            qnMapper.insert(questionnaire);
            long  questionnaireId = questionnaire.getId();
            //1.1.2 维护问卷和问题的关系 qq
            for(long questionId : questionIds){
                QuestionnaireQuestion qq = new QuestionnaireQuestion();
                qq.setQuestionId(questionId);
                qq.setQuestionnaireId(questionnaireId);
                qqMapper.insert(qq);
            }
        } else {
            //1.2 修改
            //1.2.1 修改问卷信息
            qnMapper.updateByPrimaryKey(questionnaire);
            long questionnaireId = questionnaire.getId();
            //1.2.2 删除问卷下所有的问卷问题关系
            // delete from poll_qq where questionnaire_id = #{id}
            QuestionnaireQuestionExample example = new QuestionnaireQuestionExample();
            example.createCriteria().andQuestionnaireIdEqualTo(questionnaireId);
            qqMapper.deleteByExample(example);
            //1.2.3 保存新的问卷问题关系
            for(long questionId : questionIds){
                QuestionnaireQuestion qq = new QuestionnaireQuestion();
                qq.setQuestionId(questionId);
                qq.setQuestionnaireId(questionnaireId);
                qqMapper.insert(qq);
            }
        }
    }

    @Override
    public void deleteById(long id) throws Exception {
        qnMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void batchDelete(long[] ids) throws Exception {
        for(long id : ids) {
            qnMapper.deleteByPrimaryKey(id);
        }
    }

}

controller

    QuestionnaireController.java

package com.briup.apps.poll.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.apps.poll.bean.Questionnaire;
import com.briup.apps.poll.bean.extend.QuestionnaireVM;
import com.briup.apps.poll.service.IQuestionnaireService;
import com.briup.apps.poll.util.MsgResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description="问卷相关的接口")
@RestController
@RequestMapping("/questionnaire")
public class QuestionnaireController {
    @Autowired
    private IQuestionnaireService qnService;
    
    @ApiOperation(value="批量删除问卷",
            notes="删除问卷的同时会把问卷和问题的关系解除掉,而问题保留")
    @PostMapping("batchDeleteQuestion")
    public MsgResponse batchDeleteQuestion(long[] ids){
        try {
            qnService.batchDelete(ids);
            return MsgResponse.success("批量删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
        
    }
    @ApiOperation(value="根据ID删除问卷信息",
            notes="删除问卷的同时会把问卷和问题的关系解除掉,而问题保留")
    @GetMapping("deleteQuestionnaireById")
    public MsgResponse deleteQuestionnaireById(long id){
        try {
            qnService.deleteById(id);
            return MsgResponse.success("删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value="保存或修改问卷信息",
            notes="如果问卷参数中包含id执行更新操作,否则执行修改操作")
    @PostMapping("saveOrUpdateQuestionnaire")
    public MsgResponse saveOrUpdateQuestionnaire(Questionnaire questionnaire,long[] questionIds){
        try {
            qnService.saveOrUpdate(questionnaire, questionIds);
            return MsgResponse.success("保存或修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value="通过ID查询问卷",notes="问卷下具有问题信息")
    @GetMapping("findQuestionnaireVMById")
    public MsgResponse findQuestionnaireVMById(long id){
        try {
            QuestionnaireVM qnVM = qnService.findById(id);
            // 返回成功信息
            return MsgResponse.success("success", qnVM);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回失败信息
            return MsgResponse.error(e.getMessage()) ;
        }
    }
    
    @ApiOperation(value="查询所有问卷",notes="单表")
    @GetMapping("findAllQuestionnaire")
    public MsgResponse findAllQuestionnaire(){
        try {
            List<Questionnaire> list = qnService.findAll();
            // 返回成功信息
            return MsgResponse.success("success", list);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回失败信息
            return MsgResponse.error(e.getMessage()) ;
        }
    }
}

至此,三个ORM框架例子就是这样,但是要是想学好ORM框架仍然需要继续努力!!!

                             

         

猜你喜欢

转载自www.cnblogs.com/zzuli/p/9250646.html