小言_互联网的博客

SpringBoot的基本使用(整合SSM)

348人阅读  评论(0)
 
 

SpringBoot的基本使用

创建一个Maven工程

在pom.xml中引入统一的父工程以及需要的启动器

在resources下添加SpringBoot项目的配置文件application.properties---覆盖默认配置(端口号)

创建引导类--这个类要在父包下,这样才能扫描子包-类

整合SpringMVC

引入启动器

创建Controller类

配置拦截器

首先需要自定义一个拦截器,实现HandlerInterceptor接口

配置拦截器

自定义一个Java配置类(使用@Configuration),实现WebMvcConfigurer接口

测试—浏览器访问

整合数据源

引入jdbc的启动器,mysql的驱动

在application.properties中添加四大配置

整合Mybatis

引入Mybatis的启动器

覆盖默认配置

整合通用Mapper

引入启动器

添加实体类--需要使用注解@Table,name属性对应表名

添加Mapper层的接口

添加Service类

写了一个查询所有的方法,一个删除的方法

整体测试

总结

总体思路

1.搭建springboot的基本应用

2.整合SpringMVC

3.整合数据源

4.整合Mybatis,Mapper

个人总结

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。


创建一个Maven工程

在pom.xml中引入统一的父工程以及需要的启动器


  
  1. <!--引入父工程-->
  2. <parent>
  3. <groupId>org.springframework.boot </groupId>
  4. <artifactId>spring-boot-starter-parent </artifactId>
  5. <version>2.0.7.RELEASE </version>
  6. </parent>

resources下添加SpringBoot项目的配置文件application.properties---覆盖默认配置(端口号)

server.port=8888

创建引导类--这个类要在父包下,这样才能扫描子包-类


  
  1. /**
  2. * @Author: Promsing
  3. * @Date: 2021/5/30 - 22:43
  4. * @Description: 引导类
  5. * @version: 1.0
  6. */
  7. @SpringBootApplication
  8. public class UserApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(UserApplication.class,args);
  11. }
  12. }

整合SpringMVC

引入启动器


  
  1. <!-- springMVc的启动器-->
  2. <dependency>
  3. <groupId>org.springframework.boot </groupId>
  4. <artifactId>spring-boot-starter-web </artifactId>
  5. </dependency>

创建Controller类


  
  1. /**
  2. * @Author: Promsing
  3. * @Date: 2021/5/30 - 22:36
  4. * @Description: User表的Controller类
  5. * @version: 1.0
  6. */
  7. @Controller
  8. @RequestMapping("user")
  9. public class UserController {
  10. @Autowired
  11. private UserService service;
  12. @GetMapping("test")
  13. @ResponseBody
  14. public String test(){
  15. return "hello User";
  16. }
  17. @GetMapping("findAll")
  18. @ResponseBody
  19. public String findAll(){
  20. List<Account> users = service.findAll();
  21. System.out.println(users);
  22. return "findAll";
  23. }
  24. }

配置拦截器

首先需要自定义一个拦截器,实现HandlerInterceptor接口


  
  1. /**
  2. * @Author: Promsing
  3. * @Date: 2021/5/30 - 23:06
  4. * @Description: mvc拦截器
  5. * @version: 1.0
  6. */
  7. @Component
  8. public class MyInterceptor implements HandlerInterceptor {
  9. /**
  10. * 前置方法 --Handler执行之前执行
  11. * @param request
  12. * @param response
  13. * @param handler
  14. * @return
  15. * @throws Exception
  16. */
  17. @Override
  18. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  19. //true 放行
  20. //false 拦截
  21. System.out.println( "前置方法正在执行!");
  22. return true;
  23. }
  24. @Override
  25. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  26. System.out.println( "后置方法正在执行!");
  27. }
  28. @Override
  29. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  30. System.out.println( "完成方法正在执行!");
  31. }
  32. }

配置拦截器

自定义一个Java配置类(使用@Configuration),实现WebMvcConfigurer接口


  
  1. /**
  2. * @Author: Promsing
  3. * @Date: 2021/5/30 - 23:15
  4. * @Description: 配置拦截器
  5. * 1.java配置类
  6. * 2.实现WebMvcConfigurer
  7. * @version: 1.0
  8. */
  9. @Configuration
  10. public class WebMvcConfiguration implements WebMvcConfigurer {
  11. //注入自定义拦截器
  12. @Autowired
  13. private MyInterceptor myInterceptor;
  14. @Override
  15. public void addInterceptors(InterceptorRegistry registry) {
  16. //传入拦截器对象 设置路径--链式编程
  17. registry.addInterceptor(myInterceptor).addPathPatterns( "/**");
  18. }
  19. }

测试—浏览器访问


整合数据源

引入jdbc的启动器,mysql的驱动


  
  1. <!-- jdbc的启动器-->
  2. <dependency>
  3. <groupId>org.springframework.boot </groupId>
  4. <artifactId>spring-boot-starter-jdbc </artifactId>
  5. </dependency>
  6. <!-- mysql的驱动-->
  7. <dependency>
  8. <groupId>mysql </groupId>
  9. <artifactId>mysql-connector-java </artifactId>
  10. </dependency>

application.properties中添加四大配置


  
  1. ## 指定数据库名
  2. spring.datasource.username=root
  3. spring.datasource.password=root
  4. #注释--注意 指定数据源名 spring.datasource.data-password
  5. spring.datasource.url=jdbc:mysql:///ssm
  6. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

整合Mybatis

引入Mybatis的启动器


  
  1. <!--mybatis的启动器-->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot </groupId>
  4. <artifactId>mybatis-spring-boot-starter </artifactId>
  5. <version>1.3.2 </version>
  6. </dependency>

覆盖默认配置

mybatis.type-aliases-package=com.dynamic.user.pojo

整合通用Mapper

引入启动器


  
  1. <!-- 通用Mapper-->
  2. <dependency>
  3. <groupId>tk.mybatis </groupId>
  4. <artifactId>mapper-spring-boot-starter </artifactId>
  5. <version>2.0.4 </version>
  6. </dependency>

添加实体类--需要使用注解@Table,name属性对应表名


  
  1. /**
  2. * @Author: Promsing
  3. * @Date: 2021/5/30 - 23:54
  4. * @Description: Account实体类
  5. * @version: 1.0
  6. */
  7. @Table(name="account")
  8. public class Account {
  9. private Integer id;
  10. private String name;
  11. private Double money;
  12. public Integer getId() {
  13. return id;
  14. }
  15. public void setId(Integer id) {
  16. this.id = id;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public Double getMoney() {
  25. return money;
  26. }
  27. public void setMoney(Double money) {
  28. this.money = money;
  29. }
  30. @Override
  31. public String toString() {
  32. return "User{" +
  33. "id=" + id +
  34. ", name='" + name + '\'' +
  35. ", money=" + money +
  36. '}';
  37. }
  38. }

添加Mapper层的接口


  
  1. /**
  2. * @Author: Promsing
  3. * @Date: 2021/5/31 - 17:28
  4. * @Description: DAO接口--Mapper
  5. * @version: 1.0
  6. */
  7. @Mapper
  8. public interface UserMapper extends tk.mybatis.mapper.common.Mapper<Account> {
  9. //由于使用了Mapper注解,不需要写其他方法
  10. }

添加Service类

写了一个查询所有的方法,一个删除的方法


  
  1. /**
  2. * @Author: Promsing
  3. * @Date: 2021/5/31 - 0:11
  4. * @Description: Service层
  5. * @version: 1.0
  6. */
  7. @Service
  8. public class UserService {
  9. @Autowired
  10. private UserMapper userMapper;
  11. public List<Account> findAll(){
  12. return userMapper.selectAll();
  13. // return dao.findAll();
  14. }
  15. @Transactional //整合事务--事务的注解
  16. public void deleteUserById(Long id ){
  17. this.userMapper.deleteByPrimaryKey(id);
  18. }
  19. }

整体测试


  
  1. http: //localhost:8888/user/findAll
  2. 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配置。

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。

 

 

 


转载:https://blog.csdn.net/promsing/article/details/117423540
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场