欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在applicationContext.xml中设置读取jdbc.properties文件&配置jdbcTemplate对象

程序员文章站 2022-06-23 11:26:26
linkrefer1111一、applicationContext.xml

在applicationContext.xml中设置读取jdbc.properties文件
refer1111
在applicationContext.xml中设置读取jdbc.properties文件&配置jdbcTemplate对象

一、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: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.xsd">
        <!--扫描包-->
        <context:component-scan base-package="cn.kgc.dao"></context:component-scan>
        <!--引入外部配置文件-->
        <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
        <!--创建数据源-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
</beans>

二、jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/group_project
jdbc.user=root
jdbc.password=ROOT

在applicationContext.xml中设置读取jdbc.properties文件&配置jdbcTemplate对象配置jdbcTemplate对象

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property>
		<property name="password" value="123"></property>
		<property name="username" value="pro"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<bean id="baseDao" class="org.dao.impl.EmpDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
 
</beans>

1、db.properties:jdbc.xxx(加jdbc.)是为了和其他的名字区分

jdbc.jdbcUrl=jdbc:mysql:///hibernate_32
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=1234

2、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">

<!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"  />

<!-- 1.将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
    <property name="driverClass" value="${jdbc.driverClass}" ></property>
    <property name="user" value="${jdbc.user}" ></property>
    <property name="password" value="${jdbc.password}" ></property>
</bean>

<!-- 2.将JDBCTemplate放入spring容器 (JDBCTemplate 和 DBUtils中的QueryRunner几乎一模一样)    JdbcTemplate使用:      1、JdbcTemplate是spring整合jdbc的类,直接在xml中配置,但是需要在jdbcTemplate中配置连接池,然后在dao层操作数据库      2、也可以让dao层中的类继承JdbcDaoSupport,这样applicationContext.xml中就不用再配置JdbcTemplate了,        只需要在每个dao层中的对象中配置连接池就行了,如3. -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource" ref="dataSource" ></property>
</bean>

<!-- 3.将UserDao放入spring容器 -->
<bean name="userDao" class="cn.itcast.a_jdbctemplate.UserDaoImpl" >
    <!-- <property name="jt" ref="jdbcTemplate" ></property> -->
    <property name="dataSource" ref="dataSource" ></property>
</bean>
</beans>

本文地址:https://blog.csdn.net/weixin_44769957/article/details/110948650

相关标签: Spring