SpringBoot简化SpringMVC项目

1.springboot的参数设置

1.application.properties语法

server.port=80  
server.session-timeout=30  
server.tomcat.uri-encoding=UTF-8  
  
spring.datasource.url = jdbc:mysql://localhost:3306/crm
spring.datasource.username = root  
spring.datasource.password = mymysql  
spring.datasource.driverClassName = com.mysql.jdbc.Driver  

2.application.yml语法

server:  
  port: 80
  session-timeout: 30  
  tomcat.uri-encoding: UTF-8  
  
spring:  
  datasource:  
    url : jdbc:mysql://localhost:3306/crm  
    username : root  
    password : mymysql  
    driverClassName : com.mysql.jdbc.Driver

3.application.properties优先级

  1. 项目/config/application.properties
  2. 项目/application.properties
  3. classpath:config/application.properties
  4. classpath:application.properties

2.数据库连接池

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///crm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=admin

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
使用的是阿里的连接池

3.事务管理

直接在业务层实现类上或者其方法上直接贴@Transactional注解即可

#SpringBoot默认优先选择CGLIB代理,如果需要改为优先使用JDK代理,需要做以下配置
spring.aop.proxy-target-class=false #优先使用JDK代理

4.继承WEB

默认的静态资源放在resources/static下
动态资源放在templates 例如jsp/ftl

路径映射配置

#在匹配模式时是否使用后缀模式匹配
spring.mvc.pathmatch.use-suffix-pattern=true

集成freemarker

#一般我们会做1个配置,其余默认即可
#暴露session对象的属性
spring.freemarker.expose-session-attributes=true

5.统一异常处理

springboot出现错误的时候,会把他交给classpath:static/error/404.html(其他错误)

@ControllerAdvice //控制器增强器
public class ExceptionControllerAdvice {
    
    @ExceptionHandler(Exception.class) //处理什么类型的异常
    public String handlException(Exception e, Model model) {
        model.addAttribute("msg", e.getMessage());
        return "errorView"; //逻辑视图名称
    }
}

6.添加拦截器

之前我们需要其写标签去注册我们自定义的拦截器,在springboot中只需要让我们的配置对象实现webmvcconfiguer接口,实现接口中add…方法即可

@Component
public class LoginInterceptor implements HandlerInterceptor { }
@Component
public class PermissionInterceptor implements HandlerInterceptor { }
@Configuration
public class MvcJavaConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private PermissionInterceptor permissionInterceptor;

    public void addInterceptors(InterceptorRegistry registry) {
        // 注册登录拦截器
        registry.addInterceptor(loginInterceptor)
            // 对哪些资源起过滤作用
            .addPathPatterns("/**")
            // 对哪些资源起排除作用
            .excludePathPatterns("/logout.do","/loginUser.do","/login.html","/css/**","/js/**");
        // 注册登录拦截器
        registry.addInterceptor(permissionInterceptor)
            // 对哪些资源起过滤作用
            .addPathPatterns("/**")
            // 对哪些资源起排除作用
            .excludePathPatterns("/logout.do","/loginUser.do","/login.html","/css/**","/js/**");
    }
}

7.类中使用日志输出

使用lombook提供的注解来简化代码

@Slf4j
@Service
public class PermissionServiceImpl implements IPermissionService {}
log.info(...);
log.error(...);
...
//输出日志中有变量可以使用{}作为占位符
log.info("删除id为{}的数据", id);
``

```bash
#把日志级别修改为debug,不过我们一般不会更改,除非要调试找bug,不然控制台显示的内容太多也容易乱
logging.level.root=debug
Spring Boot是一个用于创建独立的、基于Spring的生产级别的应用程序的框架。它简化了Spring应用程序的配置和部署过程,并提供了一种快速开发的方式。在整合Spring MVC项目时,你可以使用Spring Boot的自动配置功能来简化配置。 首先,你需要在pom.xml文件中添加Spring Boot和Spring MVC的依赖。然后,你可以创建一个MVCController类,使用各种注解来接收不同类型的请求。例如,你可以使用@RestController注解来标记该类,并使用@RequestMapping注解来指定请求的路径和请求方法。你还可以使用@GetMapping、@PostMapping、@PutMapping和@DeleteMapping注解来指定特定的请求方法。 在处理请求时,你可以返回不同类型的数据。例如,你可以返回字符串,可以使用@RequestMapping注解的value属性来指定返回的字符串。你还可以使用Model对象来传递数据到视图中。 总结起来,整合Spring Boot和Spring MVC项目的步骤包括添加依赖、创建MVCController类并使用注解接收不同类型的请求,以及返回不同类型的数据。这样可以简化配置和开发过程,并提高开发效率。 #### 引用[.reference_title] - *1* [springboot整合springmvc项目](https://blog.csdn.net/bluewelkin/article/details/112470271)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [11.4 SpringBoot 整合 SpringMVC](https://blog.csdn.net/Bug_marker/article/details/121418551)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [springboot 整合springmvc](https://blog.csdn.net/zxc472504515/article/details/124626824)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值