WEB开发|第八堂课

定时任务

  1. 启动类添加注解@EnableScheduling,自动扫描
  2. 定时任务业务类 添加@Component注解进行扫描
  3. 类方法添加@Schedule注解设置时间
//三种方法最终耗时的间隔比较
@Component
public class MailTask {
    /**
    *@Author CaesarDing
    *@Description 邮件数统计
    *@Date 2020/11/26
    *@Time 13:57
    *@Param []
    *@Return void
    *@MethodName: sumMail
    */
    @Autowired
    private MailService mailService;
    //@Scheduled(fixedDelay = 4000) //间隔8s
    //@Scheduled(cron = "*/4 * * * * *") //间隔8s
    //@Scheduled(fixedRate = 4000) //间隔4s
    //https://cron.qqe2.com/
    public void sumMail() throws InterruptedException {
        List<Mail> mailList = mailService.findAll();
        System.out.println(LocalDateTime.now().withNano(0) + "  当前注册邮箱总数为:" + mailList.size());
        Thread.sleep(4000L);
    }
}

异步任务

  1. 启动类添加@EnableAsync注解开启功能 自动扫描
  2. 定义异步任务类
    添加@Component标记,被容器扫描,添加@Async异步方法
  3. 通过Future类获取异步结果

IDEA 热部署

  1. 添加依赖(spring-boot独有)
        <!--spring-boot热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.4.0</version>
            <!-- 启用 -->
            <optional>true</optional>
        </dependency>
  1. 自动构建项目
    在这里插入图片描述
    3.启动自动加载
    ctrl + alt +shift + /
    在这里插入图片描述
    在这里插入图片描述
  2. 启动自动更新类
    4.
  3. 验证
    当IDEA 失去鼠标焦点的时候,就会自动加载

在这里插入图片描述

@Mapper的使用

@Mapper注解的的作用
1:为了把mapper这个DAO交給Spring管理 http://412887952-qq-com.iteye.com/blog/2392672
2:为了不再写mapper映射文件 https://blog.youkuaiyun.com/weixin_39666581/article/details/103899495
3:为了给mapper接口 自动根据一个添加@Mapper注解的接口生成一个实现类 http://www.tianshouzhi.com/api/tutorials/mapstruct/292

  1. pom.xml
<!--    配置mybatis    -->
        <!--缺少***starter,在启用MapperScan的时候会报错-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
  1. 启动类添加注解@MapperScan (也可以使用@Mapper)
    扫描单个包@MapperScan("com.example.web_class.dao")
    扫描多个包@MapperScan({"com.example.web_class.dao","com.example.web_class.service"})

  2. 编写mapper接口进行测试

package com.example.web_class.dao;

import com.example.web_class.domain.User;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 添加了@Mapper注解之后这个接口在编译时会生成相应的实现类
 *
 * 需要注意的是:这个接口中不可以定义同名的方法,因为会生成相同的id
 * 也就是说这个接口是不支持重载的
 */
 //@Mapper
public interface UserMapper {
//    @Select("select * from users where username=#{username} and password=#{password}")
    User selectByUsernameAndPassword(User user);
    User selectByName(User user);
    User selectAll(User user);
    @Select("select * from mybatis_db.users") //用来测试
    List<User> findAll();
}

package com.example.web_class.service;

import com.example.web_class.domain.User;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface UserService {
    String login(User user);
    List<User> findAll();
}

package com.example.web_class.service.impl;

import com.example.web_class.dao.UserMapper;
import com.example.web_class.domain.User;
import com.example.web_class.service.UserService;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{
    public static Map<String, User> sessionMap=new HashMap<>();
            //获取sqlSession的工具类
    String resource= "config/mybatis-config.xml";
    //    JAVA中getResourceAsStream这个方法是用来获取配置文件
    InputStream inputStream= Resources.getResourceAsStream((resource));
    SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession=sqlSessionFactory.openSession(true);
    UserMapper userMapper =sqlSession.getMapper(UserMapper.class);

    public UserServiceImpl() throws IOException {
    }

    @Override
    public String login(User user){
        System.out.println("====正在查询数据库====");
        // String result = "";
        User result = userMapper.selectByUsernameAndPassword(user);
        System.out.println("查询结果:"+result);
        if (result==null){
            return null;
        }else {
            String token = UUID.randomUUID().toString();
            System.out.println(token);
            sessionMap.put(token,user);
            return token;
        }
    }

    @Override
    public List<User> findAll() {
        List<User> list = userMapper.findAll();
        return list;
    }


}

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: UserTask
 * @Description: 用户定时业务类
 * @Author CaesarDing
 * @Date 2020/11/26
 * @Version 1.0.0
 */
@Component
public class UserTask {
/*    @Autowired
    private UserMapper userMapper;
    因为dao里面的接口没有implement去实现,所以不能直接调用 11/26
    这里不能直接调用userMapper接口,no beans*/
    @Autowired
    private UserService userService;

    @Scheduled(cron = "0/10 * * * * *")
    //@Scheduled(cron = "0 0/1 * * * *")
    public void sumUser(){
        List<User> list = userService.findAll();
        System.out.println("test");
        System.out.println(LocalDateTime.now().withNano(0) + "  当前注册的用户数为:"+ list.size());
    }
}
  1. 测试结果——成功
    在这里插入图片描述
    理解实现过程
调用
实现
调用
帮助生成实现类
常见错误:直接调用no beans
Schedule类
MailService接口
MailServiceImp
MailMapper接口
Mapper注解
MailMapper
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caesarding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值