Quarz 链接JDBC数据库(Spring Tool Suite 4 & H2数据库)

  • 第一层(启动类的配置方法)
  • 第二层(配置文件 app.properties)
  • 第三层(H2 数据库 数据)
  • 第四层(配置文件 pom.xml)
  • 第五层 (最终执行结果)

第一层(启动类的配置方法)①②③④

所在包 com.softcits.autotasknew - ①②③
① AutoTaskApplication.java :带有mian方法的执行文件

//所在包autoTaskNew - src/mai/java - com.softcits.autotasknew
//AutoTaskApplication.java
//注意添加 AutoTaskConfig.class , DBConfig.class

package com.softcits.autotasknew;

import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.softcits.autotasknew.service.AutoTaskNewService;

public class AutoTaskApplication {
     

	public static void main(String[] args) {
     
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AutoTaskConfig.class,
				DBConfig.class);

		AutoTaskNewService atns = ctx.getBean(AutoTaskNewService.class);
		
		atns.insertData("mayun");
		atns.getDesiredData();

		ctx.close();
	}

}

② AutoTaskConfig.java

//工程刚建立的时候@Configuration是无法添加的,会显示报错
//必须要先配置号pom.xml文件,才可以添加@Configuration,报错消失

package com.softcits.autotasknew;

import org.springframework.context.annotation.Configuration;

@Configuration
public class AutoTaskConfig {
     

}

③ DBConfig.java

package com.softcits.autotasknew;

import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@ComponentScan("com.softcits.autotasknew")
@PropertySource("classpath:/app.properties")
//@EnableAspectJAutoProxy
//@EnableTransactionManagement
public class DBConfig {
     

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;

    @Value("${spring.datasource.initialSize}")
    private Integer initialSize;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Value("{spring.datasource.connectionProperties}")
    private String connectionProperties;

    @Primary
    @Bean(name = "dataSource", destroyMethod = "close", initMethod = "init")
    public DataSource dataSource() {
     
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
     
            datasource.setFilters(filters);
        } catch (SQLException e) {
     
            e.printStackTrace();
        }
        datasource.setConnectionProperties(this.connectionProperties);
        return datasource;
    }
       
    @Bean("jdbcTemplate")
    public JdbcTemplate jdbcTemplate(
            @Qualifier("dataSource") DataSource dataSource) {
     
        JdbcTemplate temp = new JdbcTemplate(dataSource);
        return temp;
    }
}

所在包 com.softcits.autotasknew.service - ④

//AutoTaskNewService.java

package com.softcits.autotasknew.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class AutoTaskNewService {
     
	
	@Autowired
	private JdbcTemplate jdbctemplate;

	
	//查询数据
	public void getDesiredData() {
     

		List<Map<String, Object>> list = jdbctemplate.queryForList("select * from PERSONS");
		for (Map<String, Object> record : list) {
     
			System.out.println(record.get("ID_P") + " : " + record.get("LASTNAME"));
		}
		
	}
	
	//插入数据
	public void insertData( String name) {
     
		jdbctemplate.update("insert into PERSONS (LASTNAME) values (?);" , name);
		System.out.println("insert done.");
	}
		
	
}

第二层(配置文件 app.properties)

所在包autoTask - src/mai/resources
//所在包autoTask - src/mai/resources
//app.properties

user=lisi
password=123

###Email Configuration
mail.ServerHost=smtp.qq.com
mail.SenderAddress=账号@qq.com//必须是邮箱名
mail.SenderNick=\u5014\u5F3A\u5C0F\u62F3\u62F3 //可以是中文
mail.SenderUsername=账号@qq.com //必须是邮箱名
mail.SenderPassword=lylnpehbdjoub***//手机短信获得第三方授权码

#//数据连接池(缓冲池)
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=config,stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

第三层 (H2 数据库 数据)

数据库启动
地址:C:\DataBase\h2-2019-03-13\h2\bin
执行文件
在这里插入图片描述
启动项:
在这里插入图片描述
登录H2数据库
Quarz 链接JDBC数据库(Spring Tool Suite 4 & H2数据库)_第1张图片
Quarz 链接JDBC数据库(Spring Tool Suite 4 & H2数据库)_第2张图片

第四层(配置文件 pom.xml)

//所在包autoTask - target
//pom.xml

项目刚初始化的pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>autoTaskNew</groupId>
  <artifactId>autoTaskNew</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>autoTaskNew</name>
  <description>autoTaskNew</description>
</project>

需要特殊注意的加载项

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>5.1.6.RELEASE</version>
    </dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
	<dependency>
    	<groupId>com.h2database</groupId>
    	<artifactId>h2</artifactId>
    	<version>1.4.199</version>
    	</dependency>

完整的加载项

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>autoTask</groupId>
  <artifactId>autoTask</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>autoTask</name>
  <description>autoTask</description>
  
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.6.RELEASE</version>
        <scope>test</scope>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-email</artifactId>
        <version>1.5</version>
    </dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.26</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.26</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.199</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.3.0</version>
</dependency>

  </dependencies>
</project>

第五层 (最终执行结果)

Quarz 链接JDBC数据库(Spring Tool Suite 4 & H2数据库)_第3张图片

你可能感兴趣的:(Quarz,Quarz,JDBC)