项目目录结构
1. 导入依赖
创建 maven 工程,除了 Spring 的基本依赖外,还需要导入 Mybatis 和 Druid 的相关依赖:
org.springframeworkgroupId>
spring-jdbcartifactId>
${spring-base-version}version>
dependency>
mysqlgroupId>
mysql-connector-javaartifactId>
8.0.13version>
dependency>
com.oraclegroupId>
ojdbc6artifactId>
11.2.0.3.0version>
dependency>
org.mybatisgroupId>
mybatis-springartifactId>
1.3.2version>
dependency>
org.mybatisgroupId>
mybatisartifactId>
3.4.6version>
dependency>
com.alibabagroupId>
druidartifactId>
1.1.12version>
dependency>
2. web.xml 配置
在 web.xml 中配置 Spring 的前端控制器以及 Druid 的 Web 监控台,用于获取数据库的相关监控信息:
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">
springMvcservlet-name>
org.springframework.web.servlet.DispatcherServletservlet-class>
contextConfigLocationparam-name>
classpath:springApplication.xmlparam-value>
init-param>
1load-on-startup>
servlet>
springMvcservlet-name>
/url-pattern>
servlet-mapping>
DruidStatViewservlet-name>
com.alibaba.druid.support.http.StatViewServletservlet-class>
resetEnableparam-name>
trueparam-value>
init-param>
loginUsernameparam-name>
druidparam-value>
init-param>
loginPasswordparam-name>
druidparam-value>
init-param>
servlet>
DruidStatViewservlet-name>
/druid/*url-pattern>
servlet-mapping>
DruidWebStatFilterfilter-name>
com.alibaba.druid.support.http.WebStatFilterfilter-class>
exclusionsparam-name>
*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*param-value>
init-param>
filter>
DruidWebStatFilterfilter-name>
/*url-pattern>
filter-mapping>
web-app>
3. 数据库配置
在 resources 文件夹下新建数据库配置文件 jdbc.properties:
# mysql 数据库配置
mysql.url=jdbc:mysql://localhost:3306/mysql
mysql.username=root
mysql.password=root
# oracle 数据库配置
oracle.url=jdbc:oracle:thin:@//IP 地址:端口号/数据库实例名
oracle.username=用户名
oracle.password=密码
4. Druid 连接池配置
在 resources 文件夹下创建 springApplication.xml 配置文件和 druid.xml 配置文件:
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
bean>
bean>
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
bean>
beans>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
bean>
beans>
5. MyBatis 配置
新建 mybtais 配置文件,按照需求配置额外参数, 更多 settings 配置项可以参考 官方文档
settings>
configuration>
6. 数据查询
新建查询接口及其实现类,以下示例分别查询的是 MySQL 和 Oracle 中的字典表:
public interface MysqlDao {
List get();
}
SELECT help_keyword_id AS id,name
FROM HELP_KEYWORD
WHERE HELP_KEYWORD_ID = #{id}
select>
mapper>
public interface OracleDao {
ListqueryById(long id);
}
select * from APEX_030200.WWV_FLOW_CALS where ID = #{id}
select>
mapper>
7. 测试查询
新建测试类进行测试:
@RestController
public class MysqlController {
@Autowired
private MysqlDao mysqlDao;
@GetMapping("relation/{id}")
public String get(@PathVariable(name = "id") String id) {
return mysqlDao.queryById(id).get(0).toString();
}
}
@RestController
public class OracleController {
@Autowired
private OracleDao oracleDao;
@GetMapping("flow/{id}")
public String get(@PathVariable(name = "id") Long id) {
return oracleDao.queryById(id).get(0).toString();
}
}
8. Druid 监控台
Druid Web 页面访问地址为:http://localhost:8080/druid/index.html ,可以登录后查看数据库相关监控数据:
源码GitHub地址:https://github.com/heibaiying/spring-samples-for-all