Spring配置数据库的连接
2016-06-16 15:02
393 查看
在项目的src下有一个applicationContext.xml文件,在其中可以配置数据库的连接!
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 扫描类包, 将标注Spring注解的类自动转化为Bean, 同时完成Bean的注入 -->
<context:component-scan base-package="com.baobaotao.dao"/>
<!-- 定义一个使用DBCP实现的数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://123.206.74.66:3306/sampledb"
p:username="tao"
p:password="password"/>
<!-- 定义jdbc模板Bean -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.jdbcTemplate"
p:dataSource-ref="dataSource"/>
</beans>
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 扫描类包, 将标注Spring注解的类自动转化为Bean, 同时完成Bean的注入 -->
<context:component-scan base-package="com.baobaotao.dao"/>
<!-- 定义一个使用DBCP实现的数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://123.206.74.66:3306/sampledb"
p:username="tao"
p:password="password"/>
<!-- 定义jdbc模板Bean -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.jdbcTemplate"
p:dataSource-ref="dataSource"/>
</beans>
相关文章推荐
- 使用SpringBoot快速构建应用程序
- Java中,List菜单树的生成,最后转成json传给前台
- Java集合---HashSet的源码分析
- SPRING MVC 的请求参数获取的几种方法
- [Java] 通过域名获取IP地址的方法与作用
- Java NIO
- struts2环境配置
- Spring实战—复杂集合类型的注入
- Java Socket编程学习博客汇总
- Java连接各种数据库的实例
- java 时间操作
- Spring MVC controller的方法返回值
- spring mvc的搭建实例,自己写的小例子
- Java伪异步IO
- Java并发编程:volatile关键字解析
- springSecurity双模认证
- JAVA取得使用者输入的字符串例子
- JAVA多线程实现的三种方式及内部原理
- java注解
- 使用JDB-debug调试