<?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 ">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.lzj"></context:component-scan>
<!-- 配置c3p0数据库连接池 -->
<!-- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///hibernate_crm"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="qqqq"></property>
</bean> -->
<!-- 采用配置文件形式配置数据库连接池 -->
<!-- 先打开扫描配置文件 -->
<context:property-placeholder location="classpath:c3p0.properties"/>
<!-- 再property中配置 -->
<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>
<!-- 配置JdbcTemplate -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置Dao 在注解中配置
<bean name="userDao" class="com.lzj.dao.impl.UserDaoImpl">
<property name="jt" ref="jdbcTemplate"></property>
</bean> -->
</beans>
Demo.java
package com.lzj.demo;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
//注入UserDaoImpl 在spring容器中
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun(){
User user = new User();
user.setName("郑梦婷2");
user.setMoney(2000d);
ud.save(user);
}
}