08.@Scheduled瀹氭椂浠诲姟銆佹暣鍚坖dbcTemplate銆乵ybatis鍖哄垎澶氭暟鎹簮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了08.@Scheduled瀹氭椂浠诲姟銆佹暣鍚坖dbcTemplate銆乵ybatis鍖哄垎澶氭暟鎹簮相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/vax' title='vax'>vax
ali group ace param find system 瀹氭椂 ica@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瀹氭椂浠诲姟銆佹暣鍚坖dbcTemplate銆乵ybatis鍖哄垎澶氭暟鎹簮的主要内容,如果未能解决你的问题,请参考以下文章
iOS 14浜偣涓€瑙堬細灏忕粍浠躲€佺揣鍑戝瀷璁捐銆佹櫤鑳芥暣鐞嗐€佸浠诲姟鍗忓悓
Hyperf 鍙戝竷 杞婚噺绾ф湁鍚戞棤鐜浘浠诲姟缂栨帓搴?鍜?閫嗘尝鍏拌〃绀烘硶 瀛靛寲缁勪欢浠ュ強 v2.1.4 鐗堟湰