宽屏
1.新建Maven项目
2.建好一个普通的Maven项目之后,作为springcloud的总项目,删除掉src目录。
3.编写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.allen</groupId> <artifactId>springcloud</artifactId> <version>1.0-SNAPSHOT</version> <!-- 打包方式 pom --> <packaging>pom</packaging> <properties> <project.build.soueceEncoding>UTF-8</project.build.soueceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> </properties> <dependencyManagement> <dependencies> <!-- springCloud的依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <scope>import</scope> </dependency> <!-- SpringBoot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- SpringBoot 启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- 日志测试 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
4.新建一个module,springcloud-api
5.编写springcloud-api的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"> <parent> <artifactId>springcloud</artifactId> <groupId>com.allen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-api</artifactId> <!-- 当前Module自己需要的依赖,如果父依赖中已经配置了版本.这里就不用写了 --> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
6.新建数据库db01
7.新建部门表dept
8.往dept表里面插入数据
insert into dept (dname, db_source) values ('开发部', database()); insert into dept (dname, db_source) values ('人事部', database()); insert into dept (dname, db_source) values ('财务部', database()); insert into dept (dname, db_source) values ('市场部', database()); insert into dept (dname, db_source) values ('运维部', database());
9.在api模块中新建pojo包
10.新建Dept实体类
package com.allen.springcloud.pojo; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; @Data @NoArgsConstructor @Accessors(chain = true) public class Dept implements Serializable { private long deptno; private String dname; private String db_source; public Dept(String dname) { this.dname = dname; } }
11.新建服务提供者
12.编写springcloud-provider-dept-8001的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"> <parent> <artifactId>springcloud</artifactId> <groupId>com.allen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-provider-dept-8001</artifactId> <dependencies> <!-- 我们需要拿到实体类,所以需要配置api module --> <dependency> <groupId>com.allen</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- junit --> <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>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <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> </project>
13.创建配置文件application.yml和mybatis的mapper映射文件配置。
14.编写application.yml配置文件
server: port: 8001 #mybatis配置 mybatis: type-aliases-package: com.allen.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml #spring配置 spring: application: name: springcloud-provider-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://ost:3306/db01?useUnicode=true&serverTimezone=GMT%2B8&useSSL=false username: root password: 123456
15.编写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>
16.编写接口文件,在java下面创建com.allen.springcloud.dao.DeptDao接口文件。
package com.allen.springcloud.dao; import com.allen.springcloud.pojo.Dept; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface DeptDao { public boolean addDept(Dept dept); public Dept queryById(Long id); public List<Dept> queryAll(); }
17.在resources/mybatis/mapper下创建编写DeptMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.allen.springcloud.dao.DeptDao"> <insert id = "addDept" parameterType="Dept"> insert into dept (dname, db_source) values(#{dname}, DATABASE()); </insert> <select id = "queryById" resultType = "Dept" parameterType = "Long"> select * from dept where deptno = #{deptno}; </select> <select id = "queryAll" resultType = "Dept"> select * from dept; </select> </mapper>
18.创建service
package com.allen.springcloud.service; import com.allen.springcloud.pojo.Dept; import java.util.List; public interface DeptService { public boolean addDept(Dept dept); public Dept queryById(Long id); public List<Dept> queryAll(); }
19.编写DeptService的实现类DeptServiceImpl。
package com.allen.springcloud.service; import com.allen.springcloud.dao.DeptDao; import com.allen.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DeptServiceImpl implements DeptService{ @Autowired private DeptDao deptDao; @Override public boolean addDept(Dept dept) { return deptDao.addDept(dept); } @Override public Dept queryById(Long id) { return deptDao.queryById(id); } @Override public List<Dept> queryAll() { return deptDao.queryAll(); } }
20.创建Controller包,创建编写DeptController。
package com.allen.springcloud.controller; import com.allen.springcloud.pojo.Dept; import com.allen.springcloud.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; //提供Restful服务 @RestController public class DeptController { @Autowired private DeptService deptService; @PostMapping("/dept/add") public boolean addDept(Dept dept){ return deptService.addDept(dept); } @GetMapping("/dept/get/{id}") public Dept get(@PathVariable("id") Long id){ return deptService.queryById(id); } @GetMapping("/dept/list") public List<Dept> queryAll(){ return deptService.queryAll(); } }
21.编写启动类DeptProvider_8001
package com.allen.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //启动类 @SpringBootApplication public class DeptProvider_8001 { public static void main(String[] args){ SpringApplication.run(DeptProvider_8001.class, args); } }
22.启动
23.测试接口