構建服務提供者 springcloud-study-provider-dept-8001
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-study</artifactId>
<groupId>com.sc.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sc.springcloud</groupId>
<artifactId>springcloud-study-provider-dept-8001</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--引入自己定義的api通用包-->
<dependency>
<groupId>com.sc.springcloud</groupId>
<artifactId>springcloud-study-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<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>
</dependency>
<!--熱部署 修改後立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
yml文件配置
#配置服務器信息
server:
port: 8001
#context-path: /study 配置根目錄
#數據庫信息配置
spring:
application:
name: study-springcloud-dept #應用名稱
datasource:
url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&verifyServerCertificate=false&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#druid連接池配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置監控統計攔截的filters,去掉監控界面sql無法統計,‘wall’用於防火牆
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
userGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#Mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.dept.entity
configuration:
map-underscore-to-camel-case: true #開啓駝峯命名
cache-enabled: true #開啓二級緩存
創建一個controller、service、dao、mapper.xml
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
public boolean addDept(@RequestBody DeptEntity deptEntity) {
return deptService.addDept(deptEntity);
}
@RequestMapping(value = "/dept/findById/{deptNo}",method = RequestMethod.GET)
public DeptEntity findById(Long deptNo) {
return deptService.findById(deptNo);
}
@RequestMapping(value = "/dept/findAll",method = RequestMethod.GET)
public List<DeptEntity> findAll() {
return deptService.findAll();
}
}
public interface DeptService {
/**
* 插入
* @param deptEntity
* @return
*/
boolean addDept(DeptEntity deptEntity);
/**
* 根據id查找
* @param deptNo
* @return
*/
DeptEntity findById(Long deptNo);
/**
* 查詢全部
* @return
*/
List<DeptEntity> findAll();
}
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
DeptDao deptDao;
@Override
public boolean addDept(DeptEntity deptEntity) {
return deptDao.addDept(deptEntity);
}
@Override
public DeptEntity findById(Long deptNo) {
return deptDao.findById(deptNo);
}
@Override
public List<DeptEntity> findAll() {
return deptDao.findAll();
}
}
@Mapper
public interface DeptDao {
/**
* 插入
* @param deptEntity
*
* @return
*/
boolean addDept(DeptEntity deptEntity);
/**
* 根據id查找
* @param deptNo
* @return
*/
DeptEntity findById(Long deptNo);
/**
* 查詢全部
* @return
*/
List<DeptEntity> findAll();
}
<?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.springcloud.dao.DeptDao">
<select id="findById" resultType="DeptEntity" parameterType="Long">
SELECT * FROM dept WHERE dept_no = #{deptNo}
</select>
<select id="findAll" resultType="DeptEntity">
SELECT * FROM dept;
</select>
<insert id="addDept">
INSERT INTO dept(dept_name,db_source) VALUE (#{deptName},DATABASE());
</insert>
</mapper>
創建入口類
@SpringBootApplication
public class DeptProvider8001App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001App.class,args);
}
}
這樣,我們啓動它,就能提供部門的業務功能