SpringBoot 开发流程概要
编码环境:springboot +mybatis
1、调整yml文件,主要是数据库连接部分
# Tomcat
server:
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
port: 8082
connection-timeout: 5000ms
servlet:
context-path: /zzzz
# ssl:
# protocol: TLS
# key-store: classpath:zzzz.p12
# key-store-password: zzzzzzzz
# key-store-type: JKS
# key-alias: zzzz
spring:
# 环境 dev|test|prod
profiles:
active: test
# jackson时间格式化
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
enabled: true
mvc:
throw-exception-if-no-handler-found: true
application:
name: zzzz-mission
# resources:
# add-mappings: false
#mybatis
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.ck.modules.*.entity
global-config:
#数据库相关配置
db-config:
#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: AUTO
logic-delete-value: -1
logic-not-delete-value: 0
banner: false
#原生配置
configuration:
# map-underscore-to-camel-case: false
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
jdbc-type-for-null: 'null'
#
#mybatis-plus:
# configuration:
# map-underscore-to-camel-case: false
ck:
redis:
open: false
shiro:
redis: false
# APP模块,是通过jwt认证的,如果要使用APP模块,则需要修改【加密秘钥】
jwt:
# 加密秘钥
secret: ffffe52034348f86b67cde581c0fffff[com.ck]
# token有效时长,7天,单位秒
expire: 604800
header: token
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/zzzz?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
2、编写原始的pojo,主要用于存数据,例如
@Data
public class ProcessInfo {
/**
*
*/
private String process;
private int procRunState;
private BigDecimal procCpuRate;
private BigDecimal procMemRate;
}
3、编写Mapper
3.1 方法一:xml,该种方法mapper文件夹一般放在resource路径下
注意:namespace需要与mapper内相对应,
<?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.ck.dao.SpecialJobMapper">
<select id="getSubstationSpecialJob" resultType="hashmap" >
select *
from (
select *
from special_job as t
where
1=1
and (t.mission_type=1)
) as p
</select>
</mapper>
其后,编写与之对应的mapper类
@Mapper
public interface SpecialJobMapper{
List getSubstationSpecialJob();
}
方法二:通过@select注解,无需配置xml,例如
@Mapper
public interface OperatelogDao {
@Select("SELECT * FROM operatelog WHERE eventid = (select max(eventid) from operatelog ) and isGG=0 ")
public List<HashMap<String, Object>> selectLastNotCheckedDailyOperatelogList();
}
4、编写serviceImpl类,先创建一个空的service接口,之后再把需要对外公开的方法放入该接口中
通过@Autowired,注入mapper,使用mapper的方法,获得数据,注意需要@Service,例
@Service
public class SpecialJobServiceImpl implements SpecialJobService {
@Autowired
private SpecialJobMapper specialJobMapper;
@Override
public List getSubstationSpecialJob() {
return specialJobMapper.getSubstationSpecialJob();
}
}
5、将serviceImpl类需要公开的接口放入service接口中;
public interface SpecialJobService {
List getSubstationSpecialJob();
List getProjectSpecialJob();
public HashMap<String, Integer> getSubstationStatis();
public HashMap<String, Integer> getProjectStatis();
}
6、编写Controller
@RestController
@RequestMapping("/rightHomePage")
public class RightPageController {
@Autowired
private PageServiceRight pageServiceRight;
@ResponseBody
@RequestMapping(value ="/getSubstationSpecialData", method = RequestMethod.GET)
public Map getSubstationSpecialData() {
return pageServiceRight.getSubstationSpecialData();
}
}
7、前端请求测试
this.$http({
url: this.$http.adornUrl('/rightHomePage/getSubstationSpecialData'),
method: 'GET'
}).then(({data}) => {
console.log('getSubstationSpecialData', data)
if (data) {
// 逻辑代码
} else {
console.log('get substationSpecial null')
}
})