这里写目录标题
- 背景
- 步骤
- 明确标准
- 实操过程
- 创建好数据库,命名好(这里会考察一个命名规范),表的命名,中间使用下划线隔离开。
- 使用idea创建Springboot项目(注意版本问题)
- 使用插件生成代码
- 常用代码
- pom文件
- 总结
背景
现在面试越来越灵活了,可以限时写代码,将整个流程串起来,这个过程中可以考察面试者的东西有很多,如高压环境下实现的效果,还有代码的规范性等。
步骤
明确标准
实操过程
创建好数据库,命名好(这里会考察一个命名规范),表的命名,中间使用下划线隔离开。
考点:
1、库名,表名应该如何设置:库名、表名小写,中间用下划线隔开aliyun_admin。
2、字段如何设置:有必备字段,小写,中间用下划线隔开。(id、create_time、update_time)
3、表中字段之间的关系(主键外键等)
使用idea创建Springboot项目(注意版本问题)
创建web项目
引入pom文件中相关坐标(注意版本问题)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version>
</dependency>
<!--lombok用来简化实体类-->
</dependencies>
使用插件生成代码
首先连接数据库
然后自动生成代码
这样生成的代码controller 里边没有真正的方法代码,可以参考如下:
常用代码
@Autowired
private IEricUserService ericUserService;
/**
* 添加
*
* @param model DictionaryModel
* @return 添加的结果
* @author
* @since ${version} 2019-01-02 19:15:16
*/
@ApiOperation(value = "添加")
@PostMapping(value = {"/create"})
public FrontResult create(@RequestBody EricUser model) {
EricUser dictionaryEntity = new EricUser();
BeanUtils.copyProperties(model, dictionaryEntity);
ericUserService.save(dictionaryEntity);
return FrontResult.build(FrontResult.SUCCESS, "添加成功","");
}
/**
* 删除
*
* @param id 主键id
* @return ItooResult 是否删除成功
* @author 刘雅娟
* @since ${version} 2019-01-02 19:15:16
*/
@ApiOperation(value = "根据id删除(单个)")
@DeleteMapping(value = {"/delete/{id}"})
public FrontResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
ericUserService.removeById(id);
return FrontResult.build(FrontResult.SUCCESS, "删除成功","");
}
/**
* 批量删除
*
* @param ids ids
* @return ItooResult 批量删除是否成功结果
* @author 刘雅娟
* @since ${version} 2019-01-02 19:15:16
*/
@ApiOperation(value = "根据id批量删除")
@DeleteMapping(value = {"/deleteByIds"})
@ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
public FrontResult deleteByIds(@RequestBody List<String> ids) {
ericUserService.removeByIds(ids);
return FrontResult.build(FrontResult.SUCCESS, "批量删除成功","");
}
/**
* 修改
*
* @param model DictionaryModel
* @return 修改后的结果
* @author 刘雅娟
* @since ${version} 2019-01-02 19:15:16
*/
@ApiOperation(value = "根据id修改dictionary")
@PutMapping(value = {"/modify"})
public FrontResult modify(@RequestBody EricUser model) {
EricUser dictionaryEntity = new EricUser();
BeanUtils.copyProperties(model, dictionaryEntity);
boolean b = ericUserService.updateById(dictionaryEntity);
return FrontResult.build(FrontResult.SUCCESS, "修改成功",b);
}
/**
* 根据id查找Dictionary
*
* @param id 主键id
* @return 根据id查找的结果
* @author 刘雅娟
* @since ${version} 2019-01-02 19:15:16
*/
@ApiOperation(value = "根据id查询")
@GetMapping(value = {"/findById/{id}"})
public FrontResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
EricUser dictionaryEntity = ericUserService.getById(id);
return FrontResult.build(FrontResult.SUCCESS, "查询成功", dictionaryEntity);
}
@ApiOperation(value = "分页查询当下课程班级学生学情")
@PostMapping(value = {"/getAllStuStudyingSituationByPage"})
public FrontResult getAllStuStudyingSituationByPage(@RequestBody EricUser ericUser) throws Exception {
PageInfo studentStudyingDataForTeacher = ericUserService.getAllBypage(ericUser);
//判断是否成功返回结果
if (studentStudyingDataForTeacher != null) {
return FrontResult.build(FrontResult.SUCCESS,"查询成功", studentStudyingDataForTeacher);
}
return FrontResult.build(FrontResult.FAIL,"查询失败","");
}
分页代码
@Service
public class EricUserServiceImpl extends ServiceImpl<EricUserMapper, EricUser> implements IEricUserService {
@Autowired
EricUserMapper ericUserMapper;
private static final Logger log = LogManager.getLogger();
@Override
public PageInfo getAllBypage(EricUser studentStudyingDataForTeacherModel) throws Exception {
try {
int pageNumber = 1;
int pageSize = 2;
PageHelper.startPage(pageNumber, pageSize);
EricUser ericUser = new EricUser();
QueryWrapper<EricUser> userQueryWrapper = Wrappers.query();
List<EricUser> studentStudyingDataForTeacherLists = ericUserMapper.selectList(userQueryWrapper);
PageInfo pageInfo = new PageInfo(studentStudyingDataForTeacherLists);
return pageInfo;
} catch (Exception e) {
log.error("获取当前课程当前班级所有学生学情失败", e);
return null;
}
}
}
记得给主函数上边加上
@MapperScan(“com.example.demo.mapper”)
package com.example.demo.config;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @introduce: 返回前端的结果实体
* @author: liuBing
* @DATE: 2020/5/25
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FrontResult {
public static final String SUCCESS = "0000";
public static final String FAIL = "1111";
/**
* 结果状态码
*/
private String code;
/**
* 响应结果描述
*/
private String message;
/**
* 返回数据
*/
private Object data;
/**
* 静态方法,返回前端实体结果
*
* @param code 状态码
* @param message 消息
* @param data 数据
* @return 前端实体结果
* @author liuBing
*/
public static FrontResult build(String code, String message, Object data) {
return new FrontResult(code, message, data);
}
/**
* 返回成功的结果实体
*
* @param message 消息
* @param data 数据
* @return 实体
* @author: liuBing
* @DATE: 2020/5/25
*/
public static FrontResult getSuccessResult(String message, Object data) {
FrontResult result = new FrontResult();
result.code = ResultCodeEnum.SUCCESS.getCode();
result.message = message;
result.data = data;
return result;
}
/**
* 返回无需data的成功结果实体
*
* @param message 消息内容
* @return 返回结果
* @author liuBing
*/
public static FrontResult getSuccessResultOnlyMessage(String message) {
FrontResult result = new FrontResult();
result.code = ResultCodeEnum.SUCCESS.getCode();
result.message = message;
result.data = null;
return result;
}
/**
* 获取一个异常结果
*
* @param code 错误码
* @param message 自定义异常信息
* @return FrontResult
* @author 刘兵
*/
public static FrontResult getExceptionResult(String code, String message) {
FrontResult result = new FrontResult();
result.code = code.isEmpty() ? ResultCodeEnum.CODE_EXCEPTION.getCode() : code;
result.message = message.isEmpty() ? ResultCodeEnum.CODE_EXCEPTION.getMsg() : message;
return result;
}
}
/**
* @ClassName ResultCodeEnum
* @Description 返回前端的状态码枚举类
* @Author ZWB
* @Date 2021/12/12 17:03
* @Version 1.0
*/
package com.example.demo.config;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public enum ResultCodeEnum {
// 请求成功
SUCCESS("0000"),
// 请求失败
FAIL("1111"),
//用户不存在
USER_NOT_EXIST("2001"),
//用户已经存在
USER_HAS_EXISTED("2006"),
//验证码过期
Verification_Code_Out_Time("2002"),
//验证码错误
VERIFICATION_ERROR("2003"),
//短信请求过快,请稍后重试
FRQUENT_REQUESTS("4001"),
// 参数为空
PARAM_ERROR("400","参数为空"),
// 代码内部异常
CODE_EXCEPTION("500", "代码内部异常");
/**
* 状态码
*/
private String code;
public String getCode() {
return code;
}
ResultCodeEnum(String code) {
this.code = code;
}
private String msg;
public String getMsg() {
return msg;
}
}
/**
* @ClassName ResultMsgEnum
* @Description 返回前端消息
* @Author ZWB
* @Date 2021/12/12 17:04
* @Version 1.0
*/
package com.example.demo.config;
public enum ResultMsgEnum {
FIND_SUCCESS("查询成功!"),
FIND_FAIL("查询失败!"),
UPDATE_SUCCESS("更新成功"),
UPDATE_FAIL("更新失败"),
SEND_SUCCESS("发送成功"),
SEND_FAIL("发送失败"),
EXECUTE_SUCCESS("执行成功!"),
EXECUTE_FAIL("执行失败!"),
EXECUTE_FAI("执行失败!"),
A("1");
private String msg;
ResultMsgEnum(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
pom文件
pom文件如下:
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/iiii?serverTimezone=GMT%2B8
username: root
password: 123456
#开启日志
mybatis-plus:
global-config:
# 逻辑删除配置
db-config:
# 删除前
logic-not-delete-value: 1
# 删除后
logic-delete-value: 0
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:demo/mapper/*.xml
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
总结
实现效果并不难,但是既然难度不大,那我们如何在短时间内迅速搭建好能够使用呢,这就好像公务员考试,题都会做,但是如何能在短时间内提高答题的正答率呢,这需要我们进行刻意训练,注重规范等,能够达到面试者的要求。
建表规范,命名规范
版本注意,配置文件格式
引入分页插件,并进行配置
常用返回格式的类累积