ORM框架三种映射在Springboot上的使用
ORM(对象/关系映射)是数据库层非常重要的一部分,有三种常用的映射关系
1.多对一
tbl_clazz
clazz{
id
name
description
grade_id
charge_id
}
clazz {
id
name
description
grade:{
id:
name:
...
},
charge:{
id
name
gender
}
}
提供多对一的查询 -------班级表对应年级表和班主任表
bean
extend
ClazzVM.java
dao
extend
ClazzVMMapper.java(接口)
mapper
extend
ClazzVMMapper.xml
ClazzVMMapper主要实现sql语句,是非常重要的一部分,关键代码如下
<select id="selectByPrimaryKey" parameterType="long" resultMap="ClazzVMMapper">
select * from poll_clazz
where id=#{id}
</select>
<resultMap type="com.briup.apps.poll.bean.extend.ClazzVM" id="ClazzVMMapper">
<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>
2.一对多
一对多 查询 ------一个问题包括多个问题选项
查找所有问题,包括问题选项
bean:
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:
public interface QuestionVMMapper {
List<QuestionVM> selectAll() throws Exception;
List<QuestionVM> selectByQuestionnaireId(long id) throws Exception;
}
QuestionVMMapper.xml(对应实现dao层的selectAll功能)
<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"
resultType="com.briup.apps.poll.bean.Options"
parameterType="long">
select * from poll_options where question_id = #{id}
</select> </mapper>
service层:
接口
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<QuestionVM> findAll() throws Exception; //更新或者保存问题,并保存更新问题选项
void saveOrUpdateQuestionVM(QuestionVM questionVM) throws Exception; //删除问题,级联删除选项
void deleteById(long id) throws Exception; //关键字查询
List<Question> query(String keywords) throws Exception; //批量删除题目 void deleteBach(Long[] id)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 QuestionVMMapper questionVMMapper;
@Autowired
private OptionsMapper optionsMapper;
@Autowired
private QuestionMapper questionMapper;
@Override
public List<QuestionVM> findAll() throws Exception { return questionVMMapper.selectAll();
}
/*
* 保存或者修改问题,带选项
*/
@Override
public void saveOrUpdateQuestionVM(QuestionVM questionVM) throws Exception {
/*
* 1.得到question和options实体,因为保存是分别保存的
* 2.判断是否是保存还是修改操作
* 3.判断题目类型(简答还是选择)
*
*/ Question question=new Question();
question.setId(questionVM.getId());
question.setName(questionVM.getName());
question.setQuestiontype(questionVM.getQuestionType());
List<Options> options=questionVM.getOptions();
if(questionVM.getId()==null){
/*保存操作
* 1.保存题目
* 2.保存选型
*/
if(questionVM.getQuestionType().equals("简答题")){
//仅仅保存题目
questionMapper.insert(question);
}else{
//先保存题目
questionMapper.insert(question);
//获取question的id
long id=question.getId();
//保存选项
for(Options option : options){
//遍历所有选项,进行保存
option.setQuestionId(id);
optionsMapper.insert(option);
}
} }else{
//修改
/*
* 1.修改题目信息
* 2.删除选项
* 3.重新添加选项
*/
questionMapper.updateByPrimaryKey(question); OptionsExample example=new OptionsExample();
example.createCriteria().andQuestionIdEqualTo(question.getId());
optionsMapper.deleteByExample(example);
long id=question.getId();
for(Options option : options){
//遍历所有选项,进行保存
option.setQuestionId(id);
optionsMapper.insert(option);
} } }
@Override
public void deleteById(long id) throws Exception { questionMapper.deleteByPrimaryKey(id);
}
@Override
public List<Question> query(String keywords) throws Exception { QuestionExample example=new QuestionExample();
example.createCriteria().andNameLike(keywords);
return questionMapper.selectByExample(example);
}
@Override
public void deleteBach(Long[] ids) throws Exception {
for(long id : ids){
questionMapper.deleteByPrimaryKey(id);
} } }
Controll层
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="删除问题",notes="同时级联删除options")
@GetMapping("deleteQuestionById")
public MsgResponse deleteQuestionQuestionById(long id){
try{
questionService.deleteById(id);
return MsgResponse.success("删除成功", null);
}catch(Exception e){
return MsgResponse.error(e.getMessage());
}
}
@ApiOperation(value="保存或者更新问题",notes="如果id为空,执行保存操作,如果id不为空,执行更新操作")
@PostMapping("saveOrUpdateQuestion")
public MsgResponse saveOrUpdateQuestion(QuestionVM questionVM){
try {
questionService.saveOrUpdateQuestionVM(questionVM);
return MsgResponse.success("success", null);
} catch (Exception e) {
e.printStackTrace();
return MsgResponse.error(e.getMessage());
}
}
@ApiOperation(value="查找所有问题",notes="并查找出所有问题的选项")
@GetMapping("findAllQuestion")
public MsgResponse findAllQuestion(){
try{
List<QuestionVM> list=questionService.findAll();
return MsgResponse.success("success", list);
}catch(Exception e){
e.printStackTrace();
return MsgResponse.error(e.getMessage());
}
}
@ApiOperation(value="批量删除问题",notes="输入问题id")
@GetMapping("deleteBatch")
public MsgResponse deleteBatch(Long[] ids){
try{
questionService.deleteBach(ids);
return MsgResponse.success("success", null);
}catch(Exception e){
e.printStackTrace();
return MsgResponse.error(e.getMessage());
}
}
@ApiOperation(value="关键字查询",notes="输入题目的关键字")
@GetMapping("findByQuery")
public MsgResponse findByQuery(String keywords){
try{
List<Question> list=questionService.query(keywords);
return MsgResponse.success("success", list);
}catch(Exception e){
e.printStackTrace();
return MsgResponse.error(e.getMessage());
}
}
}
3.多对多
问卷模块
1. 查询所有问卷/通过关键字查询问卷(单表)
2. 预览问卷、课调
通过问卷id查询问卷下所有信息(问卷,问题,选项)
2.1 数据库级别表示多对多
问卷 questionnaire
id name
1 主讲问卷
2 辅讲问卷
问题 question
id name
1 授课质量
2 技术水平
3 亲和力
qq
id questionnaire_id question_id
1 1 1
2 1 2
3 1 3
4 2 1
5 2 3
# 通过问卷id查找问卷信息
select * from poll_questionnaire where id =1;
// id name description
# 通过问卷id查找属于问卷的问题信息
select * from poll_question where id in (
select question_id
from poll_qq
where questionnaire_id = 1;
);
2.2 面向对象级别上多对多
QuestionnaireVM{
private Long id;
private String name;
private List<Question> questions;
}
QuestionVM{
private Long id
private String name;
private List<Questionnaire> questionnaires;
}
bean层:
package com.briup.apps.poll.bean.extend;
import java.util.List;
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层:
package com.briup.apps.poll.dao.extend;
import java.util.List;
import com.briup.apps.poll.bean.extend.QuestionnaireVM;
public interface QuestionnaireVMMapper {
List<QuestionnaireVM> selectById(long id);
}
QuestionnaireVM.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>
<select id="selectByQuestionnaireId" parameterType="long" resultMap="QuestionVMResultMap">
select * from poll_question where id in(
select question_id from poll_qq where questionnaire_id = #{id}
)
<!-- id,name,questionType -->
</select>
根据问卷id查找所有该问卷的问题,在QuestionVMMapper.java中声名了一个根据问卷id查找所有问题的方法
该方法在QuestionVMMapper.xml中对应的代码为:
<select id="selectByQuestionnaireId" parameterType="long" resultMap="QuestionVMResultMap">
select * from poll_question where id in(
select question_id from poll_qq where questionnaire_id = #{id}
)
<!-- id,name,questionType -->
</select>
service层
接口
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 IQuestionnaireVMService {
//查询所有问卷
List<Questionnaire> findAll(); //查询所有问卷及问卷的问题
List<QuestionnaireVM> findQuestionById(long id); }
实现
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.extend.QuestionnaireVM;
import com.briup.apps.poll.dao.QuestionnaireMapper;
import com.briup.apps.poll.dao.extend.QuestionnaireVMMapper;
import com.briup.apps.poll.service.IQuestionnaireVMService;
@Service
public class QuestionnaireVMServiceImpl implements IQuestionnaireVMService{
@Autowired
private QuestionnaireMapper questionnaireMapper; @Autowired
private QuestionnaireVMMapper questionnaireVMMapper; @Override
public List<Questionnaire> findAll() {
QuestionnaireExample example=new QuestionnaireExample();
return questionnaireMapper.selectByExampleWithBLOBs(example); } @Override
public List<QuestionnaireVM> findQuestionById(long id) {
return questionnaireVMMapper.selectById(id);
} }
controll层
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.IQuestionnaireVMService;
import com.briup.apps.poll.util.MsgResponse; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; @Api(description="问卷模块接口")
@RestController
@RequestMapping("/questionnaire")
public class QuestionnaireVMController {
@Autowired
private IQuestionnaireVMService qnvs; @ApiOperation(value="查找所有问卷",notes="仅仅只查找问卷")
@PostMapping("findAllQuestionnaire") public MsgResponse findAllQuestionnaire(){
try{
List<Questionnaire> list=qnvs.findAll();
return MsgResponse.success("success", list);
}catch(Exception e){
e.printStackTrace();
return MsgResponse.error(e.getMessage());
}
} @ApiOperation(value="预览问卷信息",notes="预览包括问卷上的问题")
@GetMapping("findAllQuestionnaireById")
public MsgResponse findAllQuestionnaireById(long id){
try{
List<QuestionnaireVM> list=qnvs.findQuestionById(id);
return MsgResponse.success("success", list);
}catch(Exception e){
e.printStackTrace();
return MsgResponse.error(e.getMessage());
}
}
}
ORM框架三种映射在Springboot上的使用的更多相关文章
- SpringMVC实战(三种映射处理器)
1.前言 上一篇博客,简单的介绍了一下SpringMVC的基础知识,这篇博客来说一下SpringMVC中的几种映射处理器机制. 2.三种映射处理器 2.1 BeanNameUrlHandlerMapp ...
- ORM框架(对象关系映射)
Entity Framework 学习初级篇1--EF基本概况 http://www.cnblogs.com/xray2005/archive/2009/05/07/1452033.html ORM ...
- Orm框架(AntOrm,Ktorm)在mac机器上如何使用代码生成
Orm框架介绍 AntOrm 是我维护的一个开源csharp -netcore 项目 Ktorm 是一个大神开源的kotlin项目 由于我工作上都用到了,为了提高工作效率 我写了一个mac端工具帮助快 ...
- DjangoRestFramework框架三种分页功能的实现 - 在DjangoStarter项目模板中封装
前言 继续Django后端开发系列文章.刚好遇到一个分页的需求,就记录一下. Django作为一个"全家桶"型的框架,本身啥都有,分页组件也是有的,但默认的分页组件没有对API开发 ...
- Spring Boot (三): ORM 框架 JPA 与连接池 Hikari
前面两篇文章我们介绍了如何快速创建一个 Spring Boot 工程<Spring Boot(一):快速开始>和在 Spring Boot 中如何使用模版引擎 Thymeleaf 渲染一个 ...
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
服务器文档下载zip格式 刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...
- PHP.23-ThinkPHP框架的三种模型实例化-(D()方法与M()方法的区别)
三种模型实例化 原则上:每个数据表应对应一个模型类(Home/Model/GoodsModel.class.php --> 表tp_goods) 1.直接实例化 和实例化其他类库一样实例化模型类 ...
- iOS的三种多线程技术NSThread/NSOperation/GCD
1.iOS的三种多线程技术 1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 2.以下两点是苹果专门开发的"并发"技术,使得程序员可以不再去关心 ...
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
1.iOS的三种多线程技术 1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 2.以下两点是苹果专门开发的“并发”技术,使得程序员可以不再去关心线程的具体使用问题 ...
随机推荐
- 使用命令进行Apache Kafka操作
1.目标 我们可以在Kafka集群上执行几个Apache Kafka Operations .因此,在本文中,我们将详细讨论所有Apache Kafka操作.它还包括有助于实现这些Kafka操作的命令 ...
- war包方式部署solo博客
solo,一款小而美的博客系统,GitHub:https://github.com/b3log/solo 环境和文件准备 服务器:用的阿里云服务器,系统是 CentOS 7.3 64 位. JDK:1 ...
- django settings.py 配置文件
目录 settings.py 配置文件 settings.py 配置文件 import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.ab ...
- chmod: changing permissions of 'xxx': Operation not permitted
众所周知,在linux系统中,权限最大的是root账号,但凡修改涉及到系统本身的重大权限的操作,都需要root的权限才能操作.但是有些时候也有root干不了的事情. 比如:chmod: changin ...
- kubernetes第三章--创建harbor私有镜像库
- iOS自动签名网站
node.js作为服务端,调用shell脚本进行iOS包重签名. 需要安装:nodejs ,forever 安装环境: 安装nodejs 安装forever: npm install forever ...
- Redis3.2集群部署安装
Redis集群部署安装 Linux版本:CentOS release 6.9 Redis 版本:redis-3.2.12.tar.gz 1.执行解压命令 tar -xzf redis-3.2.12.t ...
- 六、MySQL系列之数据备份(六)
本篇主要介绍用户授权.以及数据备份等知识: 一.用户授权 首先我们需要知道的是: 所有的用户及权限信息都存储在mysql数据库下的user表中,故我们可以通过查看user表的记录来查看用户权限信息,当 ...
- Linux kernel启动选项(参数)
在Linux中,给kernel传递参数以控制其行为总共有三种方法: 1.build kernel之时的各个configuration选项. 2.当kernel启动之时,可以参数在kernel被GRUB ...
- Linux之redis-cluster
一,为什么要用redis-cluster 1.并发问题 redis官方生成可以达到 10万/每秒,每秒执行10万条命令假如业务需要每秒100万的命令执行呢? 2.数据量太大 一台服务器内存正常是16~ ...