[email protected]定时任务、整合jdbcTemplate、mybatis区分多数据源

时间:2022-05-21 05:19:10

@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);
}