技巧:在為把dataSourc連接池注入到jdbcTemplate里面時,學到一招:按住CTRL 不松,點擊相關類名,可以自動跳轉或打開。
說明:主要過程,
1、創建UserDao和UserService類
2、在beans1配置文件中,創建注入連接池
1 <!-- 配置c3p0連接池 --> 2 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 3 <property name="DriverClass" value ="com.mysql.jdbc.Driver"></property> 4 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/user"></property> 5 <property name="user" value="root"></property> 6 <property name="password" value="123456"></property> 7 </bean>
3、在beans1.xml文件中,利用<bean id="" class="> 注入創建UserDao和UserService的類對象。並在UserService中,通過<property name="" ref="" >注入UserDao對象;在UserDao中注入jdbcTemplate對象
<!-- 創建UserService 和UserDao對象,然后在UserService 中注入UserDao對象--> <bean id="userDao" class="spring.c3p0.UserDao"> <!-- 注入jdbcTemplate對象 --> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="userService" class="spring.c3p0.UserService"> <!-- 注入Dao對象 --> <property name="userDao" ref="userDao"></property> </bean>
4、把dataSourc連接池注入到jdbcTemplate里面
1 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" > 2 <!-- 把dataSourc連接池注入到jdbcTemplate里面 --> 3 <property name="dataSource" ref="dataSource"></property> 4 </bean>
TestC3p0.java文件代碼:
1 package spring.c3p0; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class TestC3p0 { 8 @Test 9 public void testDemo() { 10 ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml"); 11 UserService userService = (UserService) context.getBean("userService"); 12 userService.add(); 13 } 14 15 }
UserDao.java代碼
1 package spring.c3p0; 2 3 import org.springframework.jdbc.core.JdbcTemplate; 4 5 public class UserDao { 6 //得到JdbcTemplae對象 7 private JdbcTemplate jdbcTemplate; 8 9 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 10 this.jdbcTemplate = jdbcTemplate; 11 } 12 13 public void add() { 14 // 測試用SQL 15 String sql = "insert into users (name,age) values(?,?)"; 16 int rows = jdbcTemplate.update(sql,"american",299); 17 } 18 19 }
UserService.java代碼
package spring.c3p0; public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { userDao.add(); } }
beans1.xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop.xsd " > 10 <!-- 配置c3p0連接池 --> 11 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 12 <property name="DriverClass" value ="com.mysql.jdbc.Driver"></property> 13 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/user"></property> 14 <property name="user" value="root"></property> 15 <property name="password" value="123456"></property> 16 </bean> 17 <!-- 創建UserService 和UserDao對象,然后在UserService 中注入UserDao對象--> 18 19 <bean id="userDao" class="spring.c3p0.UserDao"> 20 <!-- 注入jdbcTemplate對象 --> 21 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 22 </bean> 23 24 25 <bean id="userService" class="spring.c3p0.UserService"> 26 <!-- 注入Dao對象 --> 27 <property name="userDao" ref="userDao"></property> 28 </bean> 29 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" > 30 <!-- 把dataSourc連接池注入到jdbcTemplate里面 --> 31 <property name="dataSource" ref="dataSource"></property> 32 </bean> 33 34 </beans>
