这篇文章将为大家详细讲解有关如何在本地搭建一个springboot+springcloud+mybatis项目,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
开发环境准备:
IDE:IntelliJ Idea 2019.3
数据库:mysql 8.0.2.2
SpringBoot版本:2.2.0.RELEASE
SpringCloud版本:Hoxton.RELEASE
打开Idea,单击New->File->Project,选择Maven,直接下一步
输入我们的项目名,我这里起名为pam,单击Finish完成创建
->New->Module,创建一个注册中心服务端的Module,输入一下名称,我这里以pam-server为例,直接选finish;
同理再创建一个客户端的Module,输入一下名称,我这里以pam-service为例,直接选finish,结构如图所示:
->Settings->Maven,配置一下本地的maven,单击OK
引入一下springboot、springcloud及mybatis的依赖。这里引入的springboot版本为2.2.0-RELEASE,与之相对应的springcloud版本为Hoxton.RELEASE。注意二者版本需要相适配,否则会启动报错。这里额外引入了几个常用依赖,直接在maven单击一下刷新,仓库即可在线将所需依赖下载进去。
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--注册中心相关依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.10.0</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-contract-stub-runner</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-contract-verifier</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-test-support</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </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配置文件:
<?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="mapUnderscoreToCamelCase" value="true"/> <!--增加日志输出--> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> </configuration>
编写mapper文件,创建一个User类的mybatis映射文件:
<?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.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的项目基本架构搭建成功!
关于如何在本地搭建一个springboot+springcloud+mybatis项目就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。