Spring Boot是一款由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。Spring Boot框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。Spring Boot 项目旨在简化创建产品级的 Spring 应用和服务。你可通过它来选择不同的 Spring 平台。可创建独立的 Java 应用和 Web 应用,同时提供了命令行工具来允许 'spring scripts'。
1.1 新建maven空项目,并在pom中引入依赖
12org.springFramework.boot3spring-boot-starter-parent42.1.12.RELEASE56
78UTF-89UTF-8101.81112
1314
1516org.springframework.boot17spring-boot-starter-web1819
2021org.springframework.boot22spring-boot-starter-data-jdbc232425mysql26mysql-connector-java275.1.482829
3031org.mybatis.spring.boot32mybatis-spring-boot-starter331.3.2343536tk.mybatis37mapper-spring-boot-starter382.1.53940
4142org.springframework.boot43spring-boot-starter-thymeleaf444546
47484950org.springframework.boot51spring-boot-maven-plugin525354
1.2 准备mysql数据表
1 CREATE TABLE `user` (
2 `id` int(11) NOT NULL AUTO_INCREMENT,
3 `username` varchar(255) NOT NULL,
4 `password` varchar(255) NOT NULL,
5 `birthday` date DEFAULT NULL,
6 `address` varchar(255) DEFAULT NULL,
7 PRIMARY KEY (`id`)
8 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
9 INSERT INTO `user` VALUES ('1', '张三', '12345', '2020-01-16', '许昌市');
10 INSERT INTO `user` VALUES ('2', '李四', '54321', '2020-01-01', '河南省许昌市');
2.1 新建启动入口并测试
在java文件夹中新建 MyApplication.java 文件,作为程序的启动入口。
1 @SpringBootApplication
2 public class MyApplication {
3 public static void main(String[] args) {
4 SpringApplication.run(MyApplication.class, args);
5 }
6 }
在resources文件夹中新建application.yml文件,配置端口和数据库连接
1 server:
2 port: 7001
3 spring:
4 datasource:
5 driver-class-name: com.mysql.jdbc.Driver
6 url: jdbc:mysql://localhost:3306/mydb
7 username: root
8 password: fanbao0713
新建controller --> UserController.java 文件,并启动测试:http://localhost:7001/user/hello
1 @RestController
2 @RequestMapping("user")
3 public class UserController {
4 @GetMapping("hello")
5 public String hello(){
6 return "Hello World!";
7 }
8 }
2.2 数据库连接配置
新建 pojo --> User.java 文件:
1 @Table(name = "user")
2 public class User {
3 @Id
4 @GeneratedValue(strategy = GenerationType.IDENTITY)
5 private Integer id;
6 private String username;
7 private String password;
8 private Date birthday;
9 private String address;
10
11 public Integer getId() {
12 return id;
13 }
14
15 public void setId(Integer id) {
16 this.id = id;
17 }
18
19 public String getUsername() {
20 return username;
21 }
22
23 public void setUsername(String username) {
24 this.username = username;
25 }
26
27 public String getPassword() {
28 return password;
29 }
30
31 public void setPassword(String password) {
32 this.password = password;
33 }
34
35 public Date getBirthday() {
36 return birthday;
37 }
38
39 public void setBirthday(Date birthday) {
40 this.birthday = birthday;
41 }
42
43 public String getAddress() {
44 return address;
45 }
46
47 public void setAddress(String address) {
48 this.address = address;
49 }
50 }
在 application.yml 文件中,新增mybatis关联
1 mybatis:
2 type-aliases-package: com.springbootdemo.pojo
在 mapper --> UserMapper.java 文件:
1 @Mapper
2 public interface UserMapper extends tk.mybatis.mapper.common.Mapper{
3 }
在 service --> UserService.java 文件,由于tk.mybati不是spring官方插件,“userMapper”会报错,可以忽略:
1 @Service
2 public class UserService {
3 @Autowired
4 private UserMapper userMapper;
5
6 public User selectById(Long id){
7 return this.userMapper.selectByPrimaryKey(id);
8 }
9
10 public ListselectAll(){
11 return userMapper.selectAll();
12 }
13 }
在 controller --> UserController.java 文件中,展示接口,并进行测试:http://localhost:7001/user/findAll
1 @RestController
2 @RequestMapping("user")
3 public class UserController {
4 @Autowired
5 private UserService userService;
6
7 @GetMapping("{id}")
8 public User selectById(@PathVariable("id") Long id){
9 return this.userService.selectById(id);
10 }
11
12 @GetMapping("findAll")
13 public MapfindAll(){
14 Mapmap = new HashMap<>();
15 map.put("用户列表", userService.selectAll());
16 return map;
17 }
18 }
3.1 访问静态资源
在 resources 文件夹中新增static中,并加入目录中,static中的各类资源可以直接访问:http://localhost:7001/js/common.js
3.2 拦截器设置
在 interceptors --> UserInterceptor中,首先我们定义一个拦截器:
1 @Component
2 public class UserInterceptor implements HandlerInterceptor {
3 @Override
4 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
5 System.out.println("preHandle/前置拦截器 method is running!");
6 return true;
7 }
8
9 @Override
10 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
11 System.out.println("postHandle/运行拦截器 method is running!");
12 }
13
14 @Override
15 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
16 System.out.println("postHandle/后置拦截器 method is running!");
17 }
18 }
新建 config --> MvcConfig.java 中,定义配置类,注册拦截器:
1 @Configuration
2 public class MvcConfig implements WebMvcConfigurer {
3 @Autowired
4 private HandlerInterceptor handlerInterceptor;
5
6 @Override
7 public void addInterceptors(InterceptorRegistry registry) {
8 registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
9 }
10 }
wireshark安装教程_weblogic12.2.1.3下载