這一次的總結是最關鍵的部分,主要涉及了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框架仍然需要繼續努力!!!