在公司的项目中用到了springboot,用到了swagger2这个api框架,持久层实现是jpa。小编结合项目中的框架,整合一下springboot+mybatisplus+swagger2.
一、在idea里安装lombok和mybatisplus插件。
二、添加pom.xml文件依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.6.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>3.0.5version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.6.1version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.29version>
dependency>
<dependency>
<groupId>javaxgroupId>
<artifactId>javaee-apiartifactId>
<version>8.0version>
dependency>
<dependency>
<groupId>com.dmsdbj.itoogroupId>
<artifactId>itoo-toolartifactId>
<version>2.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.4version>
dependency>
dependencies>
三、swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerClass {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.yye.springboot.controller"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("springboot结合swagger构建api文档")
.description("简单优雅的restful风格,https://blog.csdn.net/yye894817571")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
四、在application.properties文件中添加数据库链接和Mybatis配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/itoo_exam?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=12345678
mybatis.config-location=classpath:mybatis/config/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
五、添加mybatis-config.xml文件
<configuration>
configuration>
entity类
package com.yye.springboot.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.dmsdbj.itoo.tool.base.entity.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import javax.persistence.Column;
import java.io.Serializable;
@ApiModel(value = "PaperRecordEntity:答题记录表")
@Data
@NoArgsConstructor
@Accessors(chain = true)
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@TableName("te_paper_record")
public class PaperRecordEntity extends BaseEntity implements Serializable {
/**
* 考生_id
*/
@ApiModelProperty(value = "考生_id",required = true)
@Column(name = "examinee_id")
private String examineeId;
/**
* 课程_id
*/
@ApiModelProperty(value = "课程_id",required = true)
@Column(name = "course_id")
private String courseId;
/**
* 考试_id
*/
@ApiModelProperty(value = "考试_id",required = true)
@Column(name = "examination_id")
private String examinationId;
/**
* 模板_id
*/
@ApiModelProperty(value = "模板_id",required = true)
@Column(name = "template_id")
private String templateId;
/**
* 父题目id
*/
@ApiModelProperty(value = "父题目id")
@Column(name = "parent_id")
private String parentId;
/**
* 题型_id
*/
@ApiModelProperty(value = "题型_id",required = true)
@Column(name = "question_type_id")
private String questionTypeId;
/**
* 题干_id
*/
@ApiModelProperty(value = "题干_id",required = true)
@Column(name = "question_id")
private String questionId;
/**
* 考试分类id
*/
@ApiModelProperty(value = "考试分类id",required = true)
@Column(name = "exam_classify_id")
private String examClassifyId;
/**
* 标准分数
*/
@ApiModelProperty(value = "标准分数",required = true)
@Column(name = "standard_score")
private Double standardScore;
/**
* 判分状态(0未判 ,1 已判 ,2 已汇总)
*/
@ApiModelProperty(value = "判分状态(0未判 ,1 已判 ,2 已汇总)",required = true)
@Column(name = "mark_status")
private Integer markStatus;
/**
* 正确答案
*/
@ApiModelProperty(value = "正确答案")
@Column(name = "correct_answer")
private String correctAnswer;
@ApiModelProperty(value = "正确答案HTML格式")
@Column(name = "correct_answer_html")
private String correctAnswerHtml;
/**
* 学生答案
*/
@ApiModelProperty(value = "学生答案")
@Column(name = "student_answer")
private String studentAnswer;
/**
* 学生答案HTML格式
*/
@ApiModelProperty(value = "学生答案HTML格式")
@Column(name = "student_answer_html")
private byte[] studentAnswerHtml;
/**
* 学生答案原格式
*/
@ApiModelProperty(value = "学生答案原格式")
@Column(name = "student_answer_array_str")
private String studentAnswerArrayStr;
/**
* 学生得分
*/
@ApiModelProperty(value = "学生得分")
@Column(name = "score")
private Double score;
/**
* 判分老师
*/
@ApiModelProperty(value = "判分老师")
@Column(name = "mark_user")
private String markUser;
/**
* 试题顺序
*/
@ApiModelProperty(value = "试题顺序")
@Column(name = "question_order")
private Integer questionOrder;
/**
* 是否乱序(0 否,1 是)
*/
@ApiModelProperty(value = "是否乱序(0 否,1 是)")
@Column(name = "is_out_of_order")
private Integer isOutOfOrder;
/**
* 是否别名(0 否,1 是)
*/
@ApiModelProperty(value = "是否别名(0 否,1 是)")
@Column(name = "is_alias")
private Integer isAlias;
/**
* 更新答题记录的来源(0默认值,1从在线考试跟新,2来自json导入)
*/
@ApiModelProperty(value = "更新答题记录的来源(0默认值,1从在线考试跟新,2来自json导入)")
@Column(name = "origin")
private Integer origin;
/**
* 历史分id
*/
@ApiModelProperty(value = "历史分id")
@Column(name = "history_id")
private String historyId;
/**
* 版本(默认为1,以后是2,3,4....)
*/
@ApiModelProperty(value = "版本(默认为1,以后是2,3,4....)",required = true)
@Column(name = "version")
private Integer version;
/**
* 时间戳
*/
@ApiModelProperty(value = "时间戳",required = true)
@Column(name = "timespan")
private String timespan;
}
添加mapper文件
<mapper namespace="com.yye.springboot.dao.PaperRecordDao">
<select id="queryLikeExamineeId" resultType="com.yye.springboot.entity.PaperRecordEntity">
SELECT * FROM te_paper_record f
WHERE
f.is_delete=0
<if test="examineeId !=''">
<bind name="pattern" value="'%' + examineeId + '%'"/>
AND
f.id LIKE #{pattern}
if>
order by
f.create_time desc
select>
mapper>
Dao类
@Repository("paperRecordDao")
@Mapper
public interface PaperRecordDao extends BaseMapper<PaperRecordEntity> {
//region 模板生成:模糊查询
/**
* 根据考生_id模糊查询paperRecord
*
* @param examineeId 考生_id
* @return 模糊查询的paperRecord集合
* @author 杨月娥
* @since 2.0.0 2019-6-17 19:00:49
*/
List<PaperRecordEntity> queryLikeExamineeId(@Param("examineeId") String examineeId);
}
service接口
public interface PaperRecordService extends BaseServicePlus {
PageInfo queryLikeExamineeId(String examineeId,Integer pageNo,Integer pageSize);
}
service实现类
@Service("paperRecordService")
public class PaperRecordSericeImpl extends BaseServicePlusImpl<PaperRecordDao,PaperRecordEntity> implements PaperRecordService {
@Autowired
private PaperRecordDao paperRecordDao;
@Override
public PageInfo<PaperRecordEntity> queryLikeExamineeId(String examineeId, Integer pageNo, Integer pageSize) {
PageHelper.startPage(pageNo,pageSize);
return new PageInfo<>(paperRecordDao.queryLikeExamineeId(examineeId));
}
}
controller类
@RequestMapping(value = "/papersRecord")
@RestController
public class PaperRecordController {
@Autowired
private PaperRecordService paperRecordService;
@ApiOperation(value = "根据id模糊查询paperRecord")
@GetMapping(value = {"/queryLikeExamineeId/{id}/{pageNo}/{pageSize}"})
public ItooResult queryLikeExamineeId(@ApiParam(value = "主键id",required = true)@PathVariable String id,
@ApiParam(name="pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
@ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize){
PageInfo<PaperRecordEntity> pageInfo = paperRecordService.queryLikeExamineeId(id,pageNo,pageSize);
return ItooResult.build(ItooResult.SUCCESS,"查询成功",pageInfo);
}
}
至此,springboot+mybatisplus+swagger2整合完毕,小编用了lombok插件,省去了实体类里的getter和setter方法,是代码更为简洁。mybatisplus较mybatis用起来更舒心,只需要在service和dao层分别继承了BaseServicePlus和BaseMapper,省去了之前很多具体实体类的mapper.xml和example文件,只需要根据业务在相应的mapper文件里写一些复杂SQL即可。