SpringBoot构建项目02

1. Thymeleaf模板引擎

Thymeleaf模板引擎用来渲染视图,其原理是模板+数据=文件

使用方法:

①:导入依赖

        
            org.springframework.boot
            spring-boot-starter-thymeleaf 
        

②:在templates下创建模板文件,其默认的模板文件格式是html,注意添加命名空间!



    
    Title


成功!

这是显示欢迎信息

thymeleaf的表达式是作为标签中的一个属性!在渲染数据时会被替换成对应的值!

③:控制层绑定数据

    @RequestMapping("/thy")
    public String hello(Model model){
        model.addAttribute("msg", "hello world");
        return "hello";
    }

Thymeleaf的视图解析器:

自动配置类:ThymeleafAutoConfiguration

默认的前缀是classpath:/templates/,默认的后缀是.html

可以在application.yml中自定义前缀、后缀等!

spring:
  thymeleaf:
    prefix: classpath:/templates/html/
    suffix: .jsp

2. 拦截器

①:自定义拦截器继承HandlerInterceptorAdapter重写preHandle方法并交给spring管理

@Component
public class MyInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("登录拦截..." +"路径为:"+request.getRequestURL());
        return super.preHandle(request, response, handler);
    }
}

②:在配置类中实现WebMvcConfigurer接口重写addInterceptors方法,注册拦截器

@SpringBootApplication
public class HelloConfig implements WebMvcConfigurer{
    @Autowired
    private MyInterceptor myInterceptor;
    public static void main(String[] args) {
        SpringApplication.run(HelloConfig.class,args);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
            .addPathPatterns("/**").excludePathPatterns("/login");
    }
}

表示拦截所有请求,对/login放行!

3. DataSource集成

手动配置:

①:导入依赖

        
            com.alibaba
            druid
            1.1.20
        
        
        
            mysql
            mysql-connector-java
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

②:yml中配置相关属性

jdbc:
  username: root
  password: 112334
  url: jdbc:mysql:///test
  driverClassName: com.mysql.jdbc.Driver

③:配置类中注入值

@SpringBootApplication
public class ApplicationConfig {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class);
    }
    @ConfigurationProperties(prefix = "jdbc")
    @Bean
    public DataSource dataSource(){
        return new DruidDataSource();
    }
}

自动配置:

只需要在yml中配置

spring:
  datasource:
    url: jdbc:mysql:///test
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 112334
    

4. Mybatis集成

①:导包

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

②:搭建项目结构

domain、mappper、service、controller

③:yml中的配置

配置数据源

spring:
  datasource:
    url: jdbc:mysql:///test
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 112334
    type: com.alibaba.druid.pool.DruidDataSource

配置mybatis扫描的配置文件

mybatis:
  mapper-locations: classpath:com/hanfengyi/mapper/*Mapper.xml

④:配置类中加上@MapperScan注解扫描mapper接口交给spring管理

@SpringBootApplication
@MapperScan("com.hanfengyi.mapper")
public class ApplicationConfig {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class);
    }
}

5. 事务继承

  1. 导入mybatis依赖
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
  1. 在配置类中使用@EnableTransactionManagement开启事务管理器
@SpringBootApplication
@EnableTransactionManagement
public class ApplicationConfig{
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class);
    }
}
  1. 在业务层开启事务
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Transactional(readOnly = true)
    @Override
    public List getAll() {
        return userMapper.selectAll();
    }
}

你可能感兴趣的:(SpringBoot构建项目02)