如图,file-new- project
然后选择Sping Initializr ,选择如图:,next
填写合适的名称:Group代表你项目的来源,一般是com.公司log;
Arifact:项目名称;package:包路径。然后next
选择web,勾选web选项,next;
最后,还可以更改项目名称,以及选择项目存放位置。直接finish,创建项目完成。
项目创建完成之后,就可以尝试运行,在src-main-java-包路径下有一个application文件,这是项目的启动文件,如:
还有resources文件夹下的application.yml文件,里面书写有关项目的配置,十分方便:
1.spring:
http:
encoding:
charset: UTF-8
enabled: true
thymeleaf:
cache: false
//代表连接数据库
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://yun.elion.com.cn:45435/isldb?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: elion
password: Teamw0rk
//扫面与mapper相关的xml文件
mybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xml #实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.elion.*.entity
//设置项目端口号以及项目名称:
server:
port: 8080
servlet:
context-path: /isl-admin
//有关redis的配置
redisson: address: 'redis://127.0.0.1:6379' subscriptionConnectionMinimumIdleSize: 1 failedAttempts: 2 connectionPoolSize: 64 idleConnectionTimeout: 10000 thread: 4 connectionMinimumIdleSize: 12 timeout: 3000 subscriptionConnectionPoolSize: 50 database: 0 connectTimeout: 10000 pingTimeout: 1000 retryInterval: 1500 subscriptionsPerConnection: 5 reconnectionTimeout: 3000 retryAttempts: 2
//有关dubbo的配置
dubbo:
application:
id: isl-rest
name: isl-rest
protocol:
name: dubbo
port: -1
registry:
protocol: zookeeper
group: isl-dev
address: zookeeper://192.168.200.120:23166?backup=192.168.200.121:27106,192.168.200.122:36421
provider:
timeout: 5000
retries: 0
consumer:
check: false
monitor:
protocol: registry
//输出日志的配置
logging:
level:
root: INFO
com.elion: DEBUG
//发送邮件的配置
mail: host: smtp.163.com userName: [email protected] password: qaz820347720 toAddress: [email protected] subject: 您好 text: hello properties: mail: smtp: auth: true timeout: 2500000
然后想运行一个项目,从controller的地址进入,到service层,service实现层,到mapper层,到mapper.xml的配置层。
controller中:
@SLf4j:输出日志的注解
@RestController:@RestController是@ResponseBody和@Controller的组合注解。用来处理http请求
@RequestMapping:处理请求路径
@Reference:类似@Autowired,都是用来注入,这样可以调用service方法
service层中:
定义好需要的接口
serviceImpl,service实现层中,实现service接口,并调用mapper操作数据库。
@Transactional是声明式事务管理 编程中使用的注解
@Service注解是告诉Spring,当Spring要创建UserServiceImpl的的实例时,bean的名字必须叫做"userService",这样当Action需要使用UserServiceImpl的的实例时,就可以由Spring创建好的"userService",然后注入给Action
mapper层:会继承一个basMapper类,具体如下:
然后是与mapper对应的mapper.xml文件:
namespace:命名空间要写对应的mapper的路径;
id:要与mapper中方法名对应
resultType:要与返回类型相对应
当这些类全部完成之后,在启动类上加一个注解@MapperScan,可以指定要扫描的Mapper类的包的路径,之前是要在每一个mapper文件名上加@Mappe,现在有了@MapperScan就可以省事。
最后在浏览器中写入congtroller中对应的地址,这样一个完整的流程就结束了。