健康一贴灵,专注医药行业管理信息化

Spring 入门之-dao使用jdbcTemplate(注入过程)

技巧:在为把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>
View Code

 

posted @ 2018-01-31 15:54  一贴灵  阅读(6336)  评论(0编辑  收藏  举报
学以致用,效率第一