@Scheduled注解执行定时任务

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;
import java.util.Date; @Component
public class MyJob {
@Scheduled(fixedRate = 1000) //1秒执行一次
public void run(){
System.out.println(new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date()));
}
}
@SpringBootApplication(scanBasePackages = {"com.fly"})
@EnableScheduling
public class SpringDemoApp{

整合jdbcTemplate

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.1-atlassian-hosted</version>
<scope>runtime</scope>
</dependency>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.username=
spring.datasource.password=
spring.datasource.url=
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class UseDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public AdminUser findOne(Long id){
String sql = "select * from ADMIN_USER where ID = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<AdminUser>(AdminUser.class));
} public List<AdminUser> findAll(){
String sql = "select * from ADMIN_USER";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper(AdminUser.class));
}
}
@SpringBootTest(classes = SpringDemoApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class UseDaoTest {
@Autowired
private UseDao useDao; @Test
public void test(){
List<AdminUser> list = useDao.findAll();
for (AdminUser adminUser : list) {
System.out.println(adminUser);
}
} @Test
public void findOne(){
AdminUser user = useDao.findOne(1L);
System.out.println(user);
}
}

区分多数据源

 <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
spring.datasource.db1.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.db1.username=
spring.datasource.db1.password=
spring.datasource.db1.url= spring.datasource.db2.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.db2.username=
spring.datasource.db2.password=
spring.datasource.db2.url=

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration
@MapperScan(basePackages = {"com.fly.db1"},sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSource1 {
/**
* 配置db1数据库
* @return
*/
@Bean(name = "db1Datasource")
@ConfigurationProperties(prefix = "spring.datasource.db1")
@Primary
public DataSource db1Datasource(){
return DataSourceBuilder.create().build();
} /**
* //数据库的会话工厂
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "db1SqlSessionFactory")
@Primary
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1Datasource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean.getObject();
} /**
* 配置事务管理
* @param dataSource
* @return
*/
@Bean(name = "db1DataSourceTransactionManager")
@Primary
public DataSourceTransactionManager db1DataSourceTransactionManager(@Qualifier("db1Datasource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
} /**
* 数据库的会话模版
* @param sqlSessionFactory
* @return
*/
@Bean(name = "db1SqlSessionTemplate")
@Primary
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration
@MapperScan(basePackages = {"com.fly.db2"},sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSource2 {
/**
* 配置db1数据库
* @return
*/
@Bean(name = "db2Datasource")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db1Datasource(){
return DataSourceBuilder.create().build();
} /**
* //数据库的会话工厂
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "db2SqlSessionFactory")
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2Datasource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean.getObject();
} /**
* 配置事务管理
* @param dataSource
* @return
*/
@Bean(name = "db2DataSourceTransactionManager")
public DataSourceTransactionManager db2DataSourceTransactionManager(@Qualifier("db2Datasource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
} /**
* 数据库的会话模版
* @param sqlSessionFactory
* @return
*/
@Bean(name = "db2SqlSessionTemplate")
public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}

src/main/java/com/fly/db1/UserMapper.java

import com.fly.AdminUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; @Mapper
public interface UserMapper {
@Select("select * from ADMIN_USER where ID = #{id}")
AdminUser findOne(@Param("id") Long id);
}

08.@Scheduled定时任务、整合jdbcTemplate、mybatis区分多数据源的更多相关文章

  1. Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源

    多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...

  2. SpringBoot第四集:整合JdbcTemplate和JPA(2020最新最易懂)

    SpringBoot第四集:整合JdbcTemplate和JPA(2020最新最易懂) 当前环境说明: Windows10_64 Maven3.x JDK1.8 MySQL5.6 SpringTool ...

  3. paip.环境配置整合 ibatis mybatis proxool

    paip.环境配置整合 ibatis mybatis proxool  索引: ///////////1.调用 ///////////////2. ibatis 主设置文件  com/mijie/ho ...

  4. 7.4mybatis整合ehcache(mybatis无法实现分布式缓存必须和其他缓存框架整合)

    <\mybatis\day02\14查询缓存-二级缓存-整合ehcache.av> mybatis的缓存机制(一级缓存二级缓存和刷新缓存)和mybatis整合ehcache-- 这里有做本 ...

  5. SpringBoot学习18:springboot使用Scheduled 定时任务器

    Scheduled 定时任务器:是 Spring3.0 以后自带的一个定时任务器. 1.在pom.xml文件中添加Scheduled依赖 <!-- 添加spring定时任务 Scheduled ...

  6. springboot 整合jdbcTemplate

    springboot 整合jdbcTemplate 〇.搭建springboot环境(包括数据库的依赖) 一.添加依赖 如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖: ...

  7. spring boot 2.x 系列 —— spring boot 整合 druid+mybatis

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构 项目查询用的表对应的建表语句放置在resour ...

  8. SpringBoot第四篇:整合JDBCTemplate

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10868954.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前面几篇文 ...

  9. scheduled定时任务+实例请求数据库

    1.scheduled定时任务类:ScheduledDemo.java package com.nantian.scheduled; import java.util.Date; import org ...

随机推荐

  1. POJ 2253 Frogger ( 最短路变形 || 最小生成树 )

    题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...

  2. 576 C

    C. MP3 爆ll == #include<bits/stdc++.h> using namespace std; typedef long long ll; #define P pai ...

  3. 第二章(1.5)Python基础知识(数据类型)

    一.list(列表) list是一种有序的集合,可以随时添加和删除其中的元素 用len()函数可以获得list元素的个数 列表操作包含以下函数: cmp(list1, list2):比较两个列表的元素 ...

  4. MySQL innodb的组合索引各个列中的长度不能超过767,

    MySQL索引的索引长度问题   MySQL的每个单表中所创建的索引长度是有限制的,且对不同存储引擎下的表有不同的限制. 在MyISAM表中,创建组合索引时,创建的索引长度不能超过1000,注意这里索 ...

  5. leetcode 230. 二叉搜索树中第K小的元素(C++)

    给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [ ...

  6. CEF3 命令行 CefCommandLine 所有选项 与 开发中使用的测试网址

    转自: https://blog.csdn.net/xiezhongyuan07/article/details/86640413 1.cef3 commandLine设置 在cef3开发过程中,在O ...

  7. tomcat启动控制台报Exception in thread ''main".......“Could not find the main class:.....Bootstrap”问题

    startup.bat文件打开最后end下一行加pause调试,重新启动tomcat,发现配置没问题,但是依然报错,发现是jdk版本问题,jdk1.6无法与tomcat8适配,重新装个1.7版本的jd ...

  8. 【ABAP系列】SAP ABAP中关于commit的一点解释

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP中关于commi ...

  9. PTA 1155 Heap Paths (DFS)

    题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...

  10. maven基础--IDEA集成

    创建项目 构建项目 查找依赖 依赖范围 provided:已提供依赖范围.编译和测试有效,运行无效.如servlet-api,在项目运行时,tomcat等容器已经提供