记录一下从零开始搭建一个springboot+springcloud+mybatis本地项目的demo的过程。纯代码小白一枚,若有不足或错误之处,欢迎广大朋友指出!
开发环境准备:
IDE:IntelliJ Idea 2019.3
数据库:mysql 8.0.2.2
SpringBoot版本:2.2.0.RELEASE
SpringCloud版本:Hoxton.RELEASE
一、创建一个新的Idea项目
打开Idea,单击New->File->Project,选择Maven,直接下一步
输入我们的项目名,我这里起名为pam,单击Finish完成创建
二、右键单击project下的父节点->New->Module,创建一个注册中心服务端的Module,输入一下名称,我这里以pam-server为例,直接选finish;
同理再创建一个客户端的Module,输入一下名称,我这里以pam-service为例,直接选finish,结构如图所示:
三、单击File->Settings->Maven,配置一下本地的maven,单击OK
四、在最外层pom文件引入一下springboot、springcloud及mybatis的依赖。这里引入的springboot版本为2.2.0-RELEASE,与之相对应的springcloud版本为Hoxton.RELEASE。注意二者版本需要相适配,否则会启动报错。这里额外引入了几个常用依赖,直接在maven单击一下刷新,仓库即可在线将所需依赖下载进去。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.2.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.0.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.16version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<scope>providedscope>
dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-freemarkerartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-cacheartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-loggingartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
<version>1.3.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-contract-stub-runnerartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-contract-verifierartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-stream-test-supportartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.5version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.0.1version>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>2.6version>
dependency>
dependencies>
五、创建如图所示的目录结构:
①编写注册中心,注册中心目录结构:
编写WebApplication启动类,添加注解@EnableEurekaServer和@SpringBootApplication,我这里想做一个纯注册中心,不与数据库挂钩,所以增加了一个exclude = DataSourceAutoConfiguration.class,启动项目时不自动与数据库连接,若想让注册中心也连接数据库,这个exclude也可以不要。如图所示:
编写application.yml
server:
port: 8081
eureka:
instance:
hostname: 127.0.0.1
server:
enable-self-preservation: false # 关闭自我保护模式(默认为打开)
eviction-interval-timer-in-ms: 600000 # 续期时间,即扫描失效服务的间隔时间 一分钟,本地测试不需这么频繁,调为1小时
client:
register-with-eureka: false #自己就是注册中心,不用注册自己
fetch-registry: false #是否去注册中心获取其他服务的地址
serviceUrl:
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/
spring:
application:
name: xinrong
freemarker:
prefer-file-system-access: false
编写bootstrap.yml:
spring:
cloud:
config:
enabled: false
编写log4j.properties:
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
②编写消费者pam-service,消费者目录结构:
编写消费者启动类PamApplication.java,添加注解@MapperScan(basePackages = {“com.xinrong.pam.*.mapper”})
含义:mybatis中Mapper文件对应的包,启动项目时会自动扫描
编写消费者application.yml:
server:
port: 8088 #端口号8088
eureka:
instance:
hostname: 127.0.0.1
server:
enable-self-preservation: false # 关闭自我保护模式(默认为打开)
eviction-interval-timer-in-ms: 600000 # 续期时间,即扫描失效服务的间隔时间 一分钟
client:
register-with-eureka: true #注册到注册中心
fetch-registry: true #是否去注册中心获取其他服务的地址:是
serviceUrl:
defaultZone: http://127.0.0.1:8081/eureka/ #注册中心地址
mybatis:
mapper-locations: mapper/* #mybatis映射文件路径
config-location: classpath:/mybatis/mybatis-config.xml #mybatis配置文件路径
spring:
application:
name: pam #项目名
datasource:
url: jdbc:mysql://127.0.0.1:3306/pam?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
freemarker:
prefer-file-system-access: false
jdbc:
driverClass: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/pam
username: root
password: root
在数据库中创建一个pam_user表:
create table pam_user(
objectId varchar(50) not null primary key ,
userName varchar(50),
password varchar(255),
phoneNo varchar(20),
sex varchar(5),
birthDay date,
Email varchar(50)
);
编写user文件夹,创建五层架构,在domain中创建一个实体类User:
创建controller类接口及实现类,添加@RestController注解,在接口上添加@RequestMapping注解:
接口:
实现类:
同理,添加service接口及实现类,注意在实现类上添加@Service注解:
创建mapper文件,编写Mapper接口,添加@Repository注解:
在resources中创建mybatis文件夹,编写mybatis-config.xml配置文件:
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="logImpl" value="STDOUT_LOGGING" />
settings>
configuration>
编写mapper文件,创建一个User类的mybatis映射文件:
<mapper namespace="com.xinrong.pam.user.mapper.UserMapper">
<select id="getAll" resultType="com.xinrong.pam.user.domain.User">
select * from pam_user
select>
mapper>
至此,Demo代码编写完毕,首先启动注册中心pam_server,启动成功,通过地址http://127.0.0.1:8081/,成功查看到了注册中心管理界面:
启动服务pam_service:
启动成功,再次查看注册中心管理界面,可以看到pam服务成功注册到了注册中心:
通过浏览器地址栏键入“http://127.0.0.1:8088/controller/pam/user/getAll”,成功查询到了库中录入的测试数据。至此,springBoot的项目基本架构搭建成功!