mybatis 配置 mysql连接池_spring 5.x 系列第5篇 —— 整合 mybatis + druid 连接池 (xml配置方式)...
項目目錄結構
1. 導入依賴
創(chuàng)建 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 監(jiān)控臺,用于獲取數(shù)據(jù)庫的相關監(jiān)控信息:
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. 數(shù)據(jù)庫配置
在 resources 文件夾下新建數(shù)據(jù)庫配置文件 jdbc.properties:
# mysql 數(shù)據(jù)庫配置
mysql.url=jdbc:mysql://localhost:3306/mysql
mysql.username=root
mysql.password=root
# oracle 數(shù)據(jù)庫配置
oracle.url=jdbc:oracle:thin:@//IP 地址:端口號/數(shù)據(jù)庫實例名
oracle.username=用戶名
oracle.password=密碼
4. Druid 連接池配置
在 resources 文件夾下創(chuàng)建 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 配置文件,按照需求配置額外參數(shù), 更多 settings 配置項可以參考 官方文檔
settings>
configuration>
6. 數(shù)據(jù)查詢
新建查詢接口及其實現(xiàn)類,以下示例分別查詢的是 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 監(jiān)控臺
Druid Web 頁面訪問地址為:http://localhost:8080/druid/index.html ,可以登錄后查看數(shù)據(jù)庫相關監(jiān)控數(shù)據(jù):
源碼GitHub地址:https://github.com/heibaiying/spring-samples-for-all
總結
以上是生活随笔為你收集整理的mybatis 配置 mysql连接池_spring 5.x 系列第5篇 —— 整合 mybatis + druid 连接池 (xml配置方式)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue node --- 前后端联系
- 下一篇: qq群管理助手(基于web接口)