新增 spring-jdbc 样例
This commit is contained in:
parent
af975f5eb1
commit
964df531a8
84
spring/spring-jdbc-annotation/pom.xml
Normal file
84
spring/spring-jdbc-annotation/pom.xml
Normal file
@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>com.heibaiying</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class Flow {
|
||||
|
||||
private long id;
|
||||
|
||||
private long flowId;
|
||||
|
||||
private long plugId;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
@Data
|
||||
public class Relation {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@PropertySource(value = "classpath:mysql.properties")
|
||||
@Data
|
||||
public class DataSourceConfig {
|
||||
|
||||
/**
|
||||
* 感觉这种注入的方式并不够好
|
||||
* 没有spring-boot中使用@ConfigurationProperties(prefix = "config")指定前缀注入的方式优雅
|
||||
*/
|
||||
@Value("${mysql.driverClassName}")
|
||||
private String driverClassName;
|
||||
@Value("${mysql.url}")
|
||||
private String url;
|
||||
@Value("${mysql.username}")
|
||||
private String username;
|
||||
@Value("${mysql.password}")
|
||||
private String password;
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
|
||||
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[0];
|
||||
}
|
||||
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class[]{ServletConfig.class};
|
||||
}
|
||||
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/"};
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTransactionManagement // 开启声明式事务处理 等价于xml中<tx:annotation-driven/>
|
||||
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
||||
public class ServletConfig implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private DataSourceConfig sourceConfig;
|
||||
|
||||
/**
|
||||
* 配置数据源
|
||||
*/
|
||||
@Bean
|
||||
public DriverManagerDataSource dataSource() {
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName(sourceConfig.getDriverClassName());
|
||||
dataSource.setUrl(sourceConfig.getUrl());
|
||||
dataSource.setUsername(sourceConfig.getUsername());
|
||||
dataSource.setPassword(sourceConfig.getPassword());
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置jdbcTemplate
|
||||
*
|
||||
* @param dataSource 这个参数的名称需要保持和上面方法名一致 才能自动注入,因为
|
||||
* 采用@Bean注解生成的bean 默认采用方法名为名称,当然也可以在使用@Bean时指定name属性
|
||||
*/
|
||||
@Bean
|
||||
public JdbcTemplate jdbcTemplate(DriverManagerDataSource dataSource) {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
||||
jdbcTemplate.setDataSource(dataSource);
|
||||
return jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义事务管理器
|
||||
*/
|
||||
@Bean
|
||||
public DataSourceTransactionManager transactionManager() {
|
||||
DataSourceTransactionManager manager = new DataSourceTransactionManager();
|
||||
manager.setDataSource(dataSource());
|
||||
return manager;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class MysqlDaoImpl implements MysqlDao {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
/**
|
||||
* 更多JDBC 的使用可以参考官方文档
|
||||
* @see <a href="https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/data-access.html#jdbc-JdbcTemplate">JdbcTemplate</a>
|
||||
*/
|
||||
public List<Relation> get() {
|
||||
List<Relation> relations = jdbcTemplate.query("select * from help_keyword where help_keyword_id = ? ", new Object[]{691},
|
||||
new RowMapper<Relation>() {
|
||||
public Relation mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Relation relation = new Relation();
|
||||
relation.setId(rs.getString("help_keyword_id"));
|
||||
relation.setName(rs.getString("name"));
|
||||
return relation;
|
||||
}
|
||||
|
||||
});
|
||||
return relations;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class OracleDaoImpl implements OracleDao {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
/**
|
||||
* 更多JDBC 的使用可以参考官方文档
|
||||
* @see <a href="https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/data-access.html#jdbc-JdbcTemplate">JdbcTemplate</a>
|
||||
*/
|
||||
public List<Flow> get() {
|
||||
List<Flow> flows = jdbcTemplate.query("select * from APEX_030200.WWV_FLOW_CALS where ID = ? ", new Object[]{217584603977429772L},
|
||||
new RowMapper<Flow>() {
|
||||
public Flow mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Flow flow = new Flow();
|
||||
flow.setId(rs.getLong("ID"));
|
||||
flow.setFlowId(rs.getLong("FLOW_ID"));
|
||||
flow.setPlugId(rs.getLong("PLUG_ID"));
|
||||
return flow;
|
||||
}
|
||||
|
||||
});
|
||||
return flows;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.dao.impl;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
public interface MysqlDao {
|
||||
|
||||
List<Relation> get();
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.heibaiying.dao.impl;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
public interface OracleDao {
|
||||
|
||||
List<Flow> get();
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
# mysql 数据库配置
|
||||
mysql.driverClassName=com.mysql.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.username=root
|
||||
mysql.password=root
|
@ -0,0 +1,5 @@
|
||||
# oracle 数据库配置
|
||||
oracle.driverClassName=oracle.jdbc.driver.OracleDriver
|
||||
oracle.url=jdbc:oracle:thin:@//IP地址:端口号/数据库实例名
|
||||
oracle.username=用户名
|
||||
oracle.password=密码
|
@ -0,0 +1,36 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
public class MysqlDaoTest {
|
||||
|
||||
@Autowired
|
||||
private MysqlDao mysqlDao;
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
List<Relation> relations = mysqlDao.get();
|
||||
if (relations != null) {
|
||||
for (Relation relation : relations) {
|
||||
System.out.println(relation.getId() + " " + relation.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
public class OracleDaoTest {
|
||||
|
||||
/*注入接口时: 如果接口有多个实现类 可以用这个指定具体的实现类*/
|
||||
@Qualifier("oracleDaoImpl")
|
||||
@Autowired
|
||||
private OracleDao oracleDao;
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
List<Flow> flows = oracleDao.get();
|
||||
if (flows != null) {
|
||||
for (Flow flow : flows) {
|
||||
System.out.println(flow.getId() + " " + flow.getPlugId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
84
spring/spring-jdbc/pom.xml
Normal file
84
spring/spring-jdbc/pom.xml
Normal file
@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>com.heibaiying</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class Flow {
|
||||
|
||||
private long id;
|
||||
|
||||
private long flowId;
|
||||
|
||||
private long plugId;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
@Data
|
||||
public class Relation {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class MysqlDaoImpl implements MysqlDao {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
/**
|
||||
* 更多JDBC 的使用可以参考官方文档
|
||||
* @see <a href="https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/data-access.html#jdbc-JdbcTemplate">JdbcTemplate</a>
|
||||
*/
|
||||
public List<Relation> get() {
|
||||
List<Relation> relations = jdbcTemplate.query("select * from help_keyword where help_keyword_id = ? ", new Object[]{691},
|
||||
new RowMapper<Relation>() {
|
||||
public Relation mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Relation relation = new Relation();
|
||||
relation.setId(rs.getString("help_keyword_id"));
|
||||
relation.setName(rs.getString("name"));
|
||||
return relation;
|
||||
}
|
||||
|
||||
});
|
||||
return relations;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class OracleDaoImpl implements OracleDao {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
/**
|
||||
* 更多JDBC 的使用可以参考官方文档
|
||||
* @see <a href="https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/data-access.html#jdbc-JdbcTemplate">JdbcTemplate</a>
|
||||
*/
|
||||
public List<Flow> get() {
|
||||
List<Flow> flows = jdbcTemplate.query("select * from APEX_030200.WWV_FLOW_CALS where ID = ? ", new Object[]{217584603977429772L},
|
||||
new RowMapper<Flow>() {
|
||||
public Flow mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Flow flow = new Flow();
|
||||
flow.setId(rs.getLong("ID"));
|
||||
flow.setFlowId(rs.getLong("FLOW_ID"));
|
||||
flow.setPlugId(rs.getLong("PLUG_ID"));
|
||||
return flow;
|
||||
}
|
||||
|
||||
});
|
||||
return flows;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.dao.impl;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
public interface MysqlDao {
|
||||
|
||||
List<Relation> get();
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.heibaiying.dao.impl;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
public interface OracleDao {
|
||||
|
||||
List<Flow> get();
|
||||
}
|
11
spring/spring-jdbc/src/main/resources/jdbc.properties
Normal file
11
spring/spring-jdbc/src/main/resources/jdbc.properties
Normal file
@ -0,0 +1,11 @@
|
||||
# mysql 数据库配置
|
||||
mysql.driverClassName=com.mysql.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
||||
# oracle 数据库配置
|
||||
oracle.driverClassName=oracle.jdbc.driver.OracleDriver
|
||||
oracle.url=jdbc:oracle:thin:@//IP地址:端口号/数据库实例名
|
||||
oracle.username=用户名
|
||||
oracle.password=密码
|
45
spring/spring-jdbc/src/main/resources/springApplication.xml
Normal file
45
spring/spring-jdbc/src/main/resources/springApplication.xml
Normal file
@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
|
||||
|
||||
<!-- 开启注解包扫描-->
|
||||
<context:component-scan base-package="com.heibaiying.*"/>
|
||||
|
||||
<!--指定配置文件的位置-->
|
||||
<context:property-placeholder location="classpath:jdbc.properties"/>
|
||||
|
||||
<!--配置数据源-->
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<!--Mysql 配置-->
|
||||
<!--<property name="driverClassName" value="${mysql.driverClassName}"/>
|
||||
<property name="url" value="${mysql.url}"/>
|
||||
<property name="username" value="${mysql.username}"/>
|
||||
<property name="password" value="${mysql.password}"/>-->
|
||||
<!--Oracle 配置-->
|
||||
<property name="driverClassName" value="${oracle.driverClassName}"/>
|
||||
<property name="url" value="${oracle.url}"/>
|
||||
<property name="username" value="${oracle.username}"/>
|
||||
<property name="password" value="${oracle.password}"/>
|
||||
</bean>
|
||||
|
||||
<!--配置 jdbcTemplate -->
|
||||
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
|
||||
<!--引用上面的数据源-->
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
|
||||
<!--定义事务管理器-->
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
<!-- 开启事务注解@Transactional支持 -->
|
||||
<tx:annotation-driven/>
|
||||
|
||||
|
||||
</beans>
|
24
spring/spring-jdbc/src/main/webapp/WEB-INF/web.xml
Normal file
24
spring/spring-jdbc/src/main/webapp/WEB-INF/web.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
<!--配置spring前端控制器-->
|
||||
<servlet>
|
||||
<servlet-name>springMvc</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:springApplication.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>springMvc</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
</web-app>
|
9
spring/spring-jdbc/src/main/webapp/index.jsp
Normal file
9
spring/spring-jdbc/src/main/webapp/index.jsp
Normal file
@ -0,0 +1,9 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>index</title>
|
||||
</head>
|
||||
<body>
|
||||
hello Spring !
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,34 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration({"classpath:springApplication.xml"})
|
||||
public class MysqlDaoTest {
|
||||
|
||||
@Autowired
|
||||
private MysqlDao mysqlDao;
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
List<Relation> relations = mysqlDao.get();
|
||||
if (relations != null) {
|
||||
for (Relation relation : relations) {
|
||||
System.out.println(relation.getId() + " " + relation.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration({"classpath:springApplication.xml"})
|
||||
public class OracleDaoTest {
|
||||
|
||||
/*注入接口时: 如果接口有多个实现类 可以用这个指定具体的实现类*/
|
||||
@Qualifier("oracleDaoImpl")
|
||||
@Autowired
|
||||
private OracleDao oracleDao;
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
List<Flow> flows = oracleDao.get();
|
||||
if (flows != null) {
|
||||
for (Flow flow : flows) {
|
||||
System.out.println(flow.getId() + " " + flow.getPlugId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -402,7 +402,7 @@ public class Programmer {
|
||||
|
||||
```
|
||||
|
||||
注:@Data 是lombok包下的注解,用来生成相应的set、get方法和全参、无参构造器,使得类的书写更为简洁。
|
||||
注:@Data 是lombok包下的注解,用来生成相应的set、get方法,使得类的书写更为简洁。
|
||||
|
||||
2.新建ParamBindController.java 文件
|
||||
|
||||
|
@ -394,7 +394,7 @@ public class Programmer {
|
||||
|
||||
```
|
||||
|
||||
注:@Data 是lombok包下的注解,用来生成相应的set、get方法和全参、无参构造器,使得类的书写更为简洁。
|
||||
注:@Data 是lombok包下的注解,用来生成相应的set、get方法,使得类的书写更为简洁。
|
||||
|
||||
2.新建ParamBindController.java 文件
|
||||
|
||||
|
@ -4,6 +4,6 @@
|
||||
<title>index</title>
|
||||
</head>
|
||||
<body>
|
||||
INDEX!
|
||||
hello Spring !
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user