jdbcTemplate注入过程

1 spring配置c3p0连接池 (1)导入jar包 (2) 创建spring配置文件,配置连接池   2、完整代码如下 UserDao.java
  1. package com.liuyanzhao.c3p0;
  2. import org.springframework.jdbc.core.JdbcTemplate;
  3. public class UserDao {
  4.     //得到JdbcTemplate对象
  5.     private JdbcTemplate jdbcTemplate;
  6.     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  7.         this.jdbcTemplate = jdbcTemplate;
  8.     }
  9.     //添加操作
  10.     public void add() {
  11.         String sql = "insert into user value(?,?,?)";
  12.         jdbcTemplate.update(sql,6,"刘言曌","123456");
  13.     }
  14. }
  UserService.java
  1. package com.liuyanzhao.c3p0;
  2. public class UserService {
  3.     //添加操作
  4.     private UserDao userDao;
  5.     public void setUserDao(UserDao userDao) {
  6.         this.userDao = userDao;
  7.     }
  8.     public void add() {
  9.         userDao.add();
  10.     }
  11. }
  bean1.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" xsi:schemaLocation="
  5.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
  7.     <!--配置c3p0连接池-->
  8.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  9.         <!--注入属性-->
  10.         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  11.         <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring?useUnicode=true&amp;characterEncoding=utf8"></property>
  12.         <property name="user" value="root"></property>
  13.         <property name="password" value=""></property>
  14.     </bean>
  15.     <bean id="userServiceId" class="com.liuyanzhao.c3p0.UserService">
  16.         <!--注入dao对象-->
  17.         <property name="userDao" ref="userDaoId"></property>
  18.     </bean>
  19.     <bean id="userDaoId" class="com.liuyanzhao.c3p0.UserDao">
  20.         <!--注入jdbcTemplate对象-->
  21.         <property name="jdbcTemplate" ref="jdbcTemplateId"></property>
  22.     </bean>
  23.     <!--创建jdbcTemplate对象-->
  24.     <bean name="jdbcTemplateId" class="org.springframework.jdbc.core.JdbcTemplate">
  25.         <!--把dataSource传递到模板里面-->
  26.         <property name="dataSource" ref="dataSource"></property>
  27.     </bean>
  28. </beans>
  ServiceTest.java  测试类
  1. package com.liuyanzhao.c3p0;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class ServiceTest {
  6.     @Test
  7.     public void test() {
  8.         ApplicationContext context =
  9.             new ClassPathXmlApplicationContext("bean1.xml");
  10.         UserService userService = (UserService) context.getBean("userServiceId");
  11.         userService.add();
  12.     }
  13. }
  本文链接:https://liuyanzhao.com/5707.html

发表评论

目前评论:2