父项目搭建
- 新建普通maven项目,修改pom.xml文件
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jiu</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 打包方式-->
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.2.5</druid.version>
<junit.version>4.12</junit.version>
<lombok.version>1.18.20</lombok.version>
<log4j.version>1.2.17</log4j.version>
<logback.version>1.2.4</logback.version>
<mybatis.version>2.2.0</mybatis.version>
<springboot.version>2.3.3.RELEASE</springboot.version>
<springcloud.version>Hoxton.SR8</springcloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- SpringCloud的依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${springcloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- SpringBoot启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 此时自动创建的src文件可以删除
API模块
-
在父项目下新建一个module,同样为maven项目。
-
在api模块的pom文件下添加本模块所需的依赖
- 此时依赖不需要写版本号,父项目已经做了版本控制。
<!-- 当前Module需要的依赖-->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
-
创建数据库springcloud1
-
创建数据表dept,并插入数据。
5. 新建实体类Dept
package com.jiu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors //链式
public class Dept implements Serializable {
private Long deptno;
private String dname;
//数据库来源
private String db_source;
public Dept(String dname) {
this.dname = dname;
}
}
- api模块编写完成
服务提供者模块
-
在父项目新建子模块spring-cloud-provider-dept-8001
-
同样在pom文件下添加相关依赖。
<dependencies>
<!-- 我们需要拿到实体类dept,所以需要配置api module-->
<dependency>
<groupId>com.jiu</groupId>
<artifactId>spring-cloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- springboot-test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<!-- web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!-- 热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
- 创建主启动类
package com.jiu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringCloudProviderDept8001 {
public static void main(String[] args) {
SpringApplication.run(SpringCloudProviderDept8001.class, args);
}
}
- 在resources目录下编写application.yml配置文件
server:
port: 8001
spring:
application:
name: spring-cloud-provider-dept
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/springcloud1?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
type-aliases-package: com.jiu.pojo
- 在resources/mybatis目录下编写mybatis配置文件mybatis-config.xml,并开启二级缓存。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
- 在resources/mybatis下新建mapper文件夹,用于存放mapper.xml文件。
- 在com.jiu.mapper下编写DeptMapper接口,并编写查询所有Dept的方法。
package com.jiu.mapper;
import com.jiu.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface DeptMapper {
public List<Dept> queryAllDept();
}
- 编写对应的xml文件DeptMapper.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.jiu.mapper.DeptMapper">
<select id="queryAllDept" resultType="dept">
select *
from dept;
</select>
</mapper>
- 编写业务接口DeptService以及对应的实现类DeptServiceImpl。
- 接口
package com.jiu.service;
import com.jiu.pojo.Dept;
import java.util.List;
public interface DeptService {
public List<Dept> queryAllDept();
}
- 实现类
package com.jiu.service;
import com.jiu.mapper.DeptMapper;
import com.jiu.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
private DeptMapper deptMapper;
@Autowired
public void setDeptMapper(DeptMapper deptMapper) {
this.deptMapper = deptMapper;
}
@Override
public List<Dept> queryAllDept() {
return deptMapper.queryAllDept();
}
}
- 编写DeptController显示查询结果。
package com.jiu.controller;
import com.jiu.service.DeptService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/dept")
public class DeptController {
private DeptService deptService;
@Autowired
public void setDeptService(DeptService deptService) {
this.deptService = deptService;
}
@GetMapping("/queryAllDept")
public List queryAllDept(){
return deptService.queryAllDept();
}
}
- 启动主程序,在浏览器输入localhost:8001/dept/queryAllDept测试结果。
- 结果如下: