SpringBoot的基本使用
在resources下添加SpringBoot项目的配置文件application.properties---覆盖默认配置(端口号)
首先需要自定义一个拦截器,实现HandlerInterceptor接口
自定义一个Java配置类(使用@Configuration),实现WebMvcConfigurer接口
在application.properties中添加四大配置
添加实体类--需要使用注解@Table,name属性对应表名
创建一个Maven工程
在pom.xml中引入统一的父工程以及需要的启动器
<!--引入父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
在resources下添加SpringBoot项目的配置文件application.properties---覆盖默认配置(端口号)
server.port=8888
创建引导类--这个类要在父包下,这样才能扫描子包-类
/**
* @Author: Promsing
* @Date: 2021/5/30 - 22:43
* @Description: 引导类
* @version: 1.0
*/
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
整合SpringMVC
引入启动器
<!-- springMVc的启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
创建Controller类
/**
* @Author: Promsing
* @Date: 2021/5/30 - 22:36
* @Description: User表的Controller类
* @version: 1.0
*/
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService service;
@GetMapping("test")
@ResponseBody
public String test(){
return "hello User";
}
@GetMapping("findAll")
@ResponseBody
public String findAll(){
List<Account> users = service.findAll();
System.out.println(users);
return "findAll";
}
}
配置拦截器
首先需要自定义一个拦截器,实现HandlerInterceptor接口
/**
* @Author: Promsing
* @Date: 2021/5/30 - 23:06
* @Description: mvc拦截器
* @version: 1.0
*/
@Component
public class MyInterceptor implements HandlerInterceptor {
/**
* 前置方法 --Handler执行之前执行
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//true 放行
//false 拦截
System.out.println("前置方法正在执行!");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("后置方法正在执行!");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("完成方法正在执行!");
}
}
配置拦截器
自定义一个Java配置类(使用@Configuration),实现WebMvcConfigurer接口
/**
* @Author: Promsing
* @Date: 2021/5/30 - 23:15
* @Description: 配置拦截器
* 1.java配置类
* 2.实现WebMvcConfigurer
* @version: 1.0
*/
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
//注入自定义拦截器
@Autowired
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//传入拦截器对象 设置路径--链式编程
registry.addInterceptor(myInterceptor).addPathPatterns("/**");
}
}
测试—浏览器访问
整合数据源
引入jdbc的启动器,mysql的驱动
<!-- jdbc的启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在application.properties中添加四大配置
## 指定数据库名
spring.datasource.username=root
spring.datasource.password=root
#注释--注意 指定数据源名 spring.datasource.data-password
spring.datasource.url=jdbc:mysql:///ssm
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
整合Mybatis
引入Mybatis的启动器
<!--mybatis的启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
覆盖默认配置
mybatis.type-aliases-package=com.dynamic.user.pojo
整合通用Mapper
引入启动器
<!-- 通用Mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
添加实体类--需要使用注解@Table,name属性对应表名
/**
* @Author: Promsing
* @Date: 2021/5/30 - 23:54
* @Description: Account实体类
* @version: 1.0
*/
@Table(name="account")
public class Account {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
添加Mapper层的接口
/**
* @Author: Promsing
* @Date: 2021/5/31 - 17:28
* @Description: DAO接口--Mapper
* @version: 1.0
*/
@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<Account> {
//由于使用了Mapper注解,不需要写其他方法
}
添加Service类
写了一个查询所有的方法,一个删除的方法
/**
* @Author: Promsing
* @Date: 2021/5/31 - 0:11
* @Description: Service层
* @version: 1.0
*/
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<Account> findAll(){
return userMapper.selectAll();
// return dao.findAll();
}
@Transactional //整合事务--事务的注解
public void deleteUserById(Long id ){
this.userMapper.deleteByPrimaryKey(id);
}
}
整体测试
http://localhost:8888/user/findAll
http://localhost:8888/user/test
总结
总体思路
1.搭建springboot的基本应用
引入统一的父工程,以及需要的启动器
添加引导类使用@SpringBootApplication(这是一个组合注解包含@EnableAutoConfiguration+@ComponentScan+@SpringBootConfiguration)
2.整合SpringMVC
修改端口(server.port=8888)
添加自定义拦截器:实现HandlerInterceptor接口
配置拦截器:自定义一个配置类(@Configuration),实现WebMvcConfigurer接口
3.整合数据源
引入jdbc启动器,mysql驱动
配置四大属性
4.整合Mybatis,Mapper
引入启动器
接口添加@Mapper注解
mapper继承Mapper<User>
个人总结
Spring Boot是一个简化Spring开发的框架。用来监护spring应用开发,约定大于配置,去繁就简,just run 就能创建一个独立的,产品级的应用。
跟整合SSM相比,SpringBoot减少了很多配置文件,自动配置。无需引入太多jar包,只需要一个启动器就行。
内置了Tomcat,直接启动引导类就行
我们在使用Spring Boot时只需要配置相应的Spring Boot就可以用所有的Spring组件,简单的说,spring boot就是整合了很多优秀的框架,不用我们自己手动的去写一堆xml配置然
后进行配置。从本质上来说,Spring Boot就是Spring,它做了那些没有它你也会去做的Spring Bean配置。
如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。