SpringBoot入门(3)

1. springboot中拦截器的配置

1.1 定义拦截器

自定义一个类去继承 HandlerInterceptorAdapter 类 覆写 preHandle 方法

@Component
//自定义拦截器
public class MyHandlerInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("你到底是不是拦截器");
        return super.preHandle(request, response, handler);
    }
}

1.2 注册拦截器

在主配置类实现 WebMvcConfigurer 接口 ,覆写注册拦截器的方法

@SpringBootApplication
@MapperScan("cn.itsource.springboot_allin.mapper")
@EnableTransactionManagement
//@EnableTransactionManagement
public class SpringbootAllinApplication implements WebMvcConfigurer {
    //配置自定义拦截器
    @Autowired
    private MyHandlerInterceptor myHandlerInterceptor;

    //覆写添加拦截器的方法
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
		/* addPathPatterns() 拦截路径
		*  excludePathPatterns("/employee")  放行路径
		*/
        registry.addInterceptor(myHandlerInterceptor).addPathPatterns("/**").excludePathPatterns("/employee");
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringbootAllinApplication.class, args);
    }

}

添加视图控制器 (添加后可以直接访问Templates下面的页面)

 @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/xx").setViewName("hello2.html");
    }
    //访问/xx路径就会定位到  templates/hello2.html

2. 集成DataSource

2.1 添加依赖的jar包

<!-- https://mvnrepository.com/artifact/com.alibaba/druid 
	这里我们使用阿里的 连接池,也可以不配置这个依赖,
	springboot 内置了连接池给我们使用
-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>
        <!-- mysql 数据库驱动. -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

2.2 手动配置连接池的配置文件


---------------自动配置,推荐使用 ----------------
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql:///crm
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource   #如果使用默认的连接池,可以不配置type

---------------手动配置bean 不推荐使用---------------
 @Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource ;
    }

3. 集成MyBatis

  1. 导入依赖文件
 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
 </dependency>
  1. 配置文件(在 application.yml中配置 )
mybatis: 
  mapper-locations: cn/itsource/springboot_allin/mapper/*Mapper.xml  
  //上面是mybatis 对xml的扫描路径
  1. 配置mapper接口的扫描包(在主配置类上)
@SpringBootApplication
@MapperScan("cn.itsource.springboot_allin.mapper")
public class SpringbootAllinApplication {
  
}

4. 集成事务

  1. 使用注解 @EnableTransactionManagement 开启注解管理
@SpringBootApplication
@MapperScan("cn.itsource.web.controller.mapper")
@EnableTransactionManagement
public class SpringbootAllinApplication {

}
  1. service打事务标签 @Transactional
@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }
}
  1. xml 方式的配置

导入依赖的jar包

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>

配置事务xml

<!-- 配置事物管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <aop:config>
        <aop:pointcut expression="execution(* cn.itsource.web.controller.service..*.*(..))" id="coreServicePointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="coreServicePointcut"/>
    </aop:config>

    <!-- aop应用事务管理 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

在主配置类导入配置的xml

@ImportResource("classpath:applicationContext-service.xml")
public class ApplicationConfig

5. PageHelper的使用

  1. 导入依赖的jar包
<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
</dependency>
  1. 编写查询语句,这里的分页就不需要limit 了,也不需要查询总条数
<select id="selectPage" resultType="cn.itsource.web.controller.domain.Employee">
	select id,username,realName from t_employee
</select>
  1. 具体分页的service层方法
 @Override
    public Page<Employee> selectPage() {
        PageHelper.startPage(1,5 );
        Page<Employee> page = (Page<Employee>) employeeMapper.selectPage();
        return  page;
    }
  1. 获取结果
 Page<Employee> page = employeeService.selectPage();
 System.out.println("总条数:"+page.getTotal());
 for (Employee employee : page.getResult()) {
 	System.out.println(employee);
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值