# spring 整合 jdbc template(xml配置方式) 1、创建标准web maven工程,导入依赖 ```xml 4.0.0 com.heibaiying spring-jdbc 1.0-SNAPSHOT 5.1.3.RELEASE org.springframework spring-context ${spring-base-version} org.springframework spring-beans ${spring-base-version} org.springframework spring-core ${spring-base-version} org.springframework spring-web ${spring-base-version} org.springframework spring-webmvc ${spring-base-version} javax.servlet javax.servlet-api 4.0.1 provided org.projectlombok lombok 1.18.4 provided org.springframework spring-jdbc ${spring-base-version} mysql mysql-connector-java 8.0.13 junit junit 4.12 test org.springframework spring-test ${spring-base-version} test com.oracle ojdbc6 11.2.0.3.0 ``` 2、在web.xml 进行如下配置 ```xml springMvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springApplication.xml 1 springMvc / ``` 3、在resources文件夹下新建数据库配置文件jdbc.properties ```properties # 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=密码 ``` 4、在resources文件夹下创建springApplication.xml 配置文件 ```xml ``` 5、新建查询接口及其实现类,这里我查询的表是mysql和oracle中的字典表 ```java public interface MysqlDao { List get(); } ``` ```java 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 JdbcTemplate */ public List get() { List relations = jdbcTemplate.query("select * from help_keyword where help_keyword_id = ? ", new Object[]{691}, new RowMapper() { 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; } } ``` ```mysql package com.heibaiying.dao.impl; import com.heibaiying.bean.Flow; import java.util.List; /** * @author : heibaiying * @description : */ public interface OracleDao { List get(); } ``` ```java 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 JdbcTemplate */ public List get() { List flows = jdbcTemplate.query("select * from APEX_030200.WWV_FLOW_CALS where ID = ? ", new Object[]{217584603977429772L}, new RowMapper() { 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; } } ``` 6.新建测试类进行测试 ```java 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 relations = mysqlDao.get(); if (relations != null) { for (Relation relation : relations) { System.out.println(relation.getId() + " " + relation.getName()); } } } } ``` ```java 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 flows = oracleDao.get(); if (flows != null) { for (Flow flow : flows) { System.out.println(flow.getId() + " " + flow.getPlugId()); } } } } ```