通过配置文件XML方法的配置

可以使用非常简练的Service类

UserService类代码如下:

  1. package com.swift;
  2.  
  3. public class UserService {
  4. private UserDao userDao;
  5.  
  6. public void setUserDao(UserDao userDao) {
  7. this.userDao = userDao;
  8. }
  9. public boolean add() {
  10. return userDao.add();
  11. }
  12. }

UserService要调用UserDao中连接数据库的方法add(),需要一个userDao对象,这个对象通过配置文件XML来注入对象,需要setUserDao()来配合

同样UserDao类也非常简练

  1. package com.swift;
  2.  
  3. import org.springframework.jdbc.core.JdbcTemplate;
  4.  
  5. public class UserDao {
  6. private JdbcTemplate jdbcTemplate;
  7.  
  8. public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  9. this.jdbcTemplate = jdbcTemplate;
  10. }
  11.  
  12. public boolean add() {
  13. // ComboPooledDataSource dataSource=new ComboPooledDataSource();
  14. // dataSource.setDriverClass("com.mysql.jdbc.Driver");
  15. // dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sw_database");
  16. // dataSource.setUser("root");
  17. // dataSource.setPassword("root");
  18.  
  19. String sql="insert into sw_user(username,password) values(?,?)";
  20. int count=jdbcTemplate.update(sql,"弹痕","战侠歌");
  21. if(count==1) {
  22. return true;
  23. }
  24. return false;
  25. }
  26. }

UserDao中要使用JdbcTemplate的对象,这个对象也通过配置文件XML来注入,需要setJdbcTemplate()来配合

而jdbcTemplate对象又需要通过XML在配置中注入dataSource对象

dataSource对象是通过c3p0连接池类得到,需要导入前一篇随笔提供的jar包支持

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" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  9.  
  10. <!-- c3p0连接池得到dataSource -->
  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/sw_database"></property>
  14. <property name="user" value="root"></property>
  15. <property name="password" value="root"></property>
  16. </bean>
  17.  
  18. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  19. <property name="dataSource" ref="dataSource"></property>
  20. </bean>
  21.  
  22. <bean id="userDao" class="com.swift.UserDao">
  23. <property name="jdbcTemplate" ref="jdbcTemplate"></property>
  24. </bean>
  25.  
  26. <bean id="userService" class="com.swift.UserService">
  27. <property name="userDao" ref="userDao"></property>
  28. </bean>
  29.  
  30. </beans>

测试类中只要得到XML配置文件对象就可以搞定所有了

代码如下:

  1. package com.swift;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.context.support.ClassPathXmlApplicationContext;
  12.  
  13. @WebServlet("/test")
  14. public class ServletTest extends HttpServlet {
  15. private static final long serialVersionUID = 1L;
  16.  
  17. public ServletTest() {
  18. super();
  19. }
  20.  
  21. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  22. response.setCharacterEncoding("utf-8");
  23. response.setContentType("text/html;charset=utf-8");
  24. response.getWriter().append("Served at: ").append(request.getContextPath());
  25.  
  26. //使用JdbcTemplat的queryForObject方法
  27. ApplicationContext context=new ClassPathXmlApplicationContext("c3p0.xml");
  28. UserService userService= (UserService) context.getBean("userService");
  29. if(userService.add()) {
  30. response.getWriter().append("添加成功!");
  31. }else {
  32. response.getWriter().append("添加失败!");
  33. }
  34. }
  35.  
  36. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37. doGet(request, response);
  38. }
  39.  
  40. }

浏览器中结果:

数据库中结果:

Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法的更多相关文章

  1. Spring中c3p0连接池 jar包下载 c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包

    c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包 下载地址: https://pan.baidu.com/s/1jHDiR7g 密码 tyek

  2. Spring_day03--Spring配置c3p0连接池和dao使用jdbcTemplate

    Spring配置c3p0连接池和dao使用jdbcTemplate 1 spring配置c3p0连接池 第一步 导入jar包 第二步 创建spring配置文件,配置连接池 原始方式 (1)把代码在配置 ...

  3. C3P0连接池详细配置

    C3P0连接池详细配置 转自http://msq.javaeye.com/blog/60387 <c3p0-config> <default-config> <!--当连 ...

  4. Spring之c3p0连接池配置和使用

    1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...

  5. Hibernate的配置中,c3p0连接池相关配置

    一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...

  6. Java框架spring 学习笔记(十六):c3p0连接池的配置以及dao使用jdbcTemplate

    连接池可以大大提高数据库的性能和连接速度,将那些已连接的数据库连接存放在一个连接池里,以后别人要连接数据库的时候,将不会重新建立数据库连接,直接从连接池中取出可用的连接,用户使用完毕后,会释放连接重新 ...

  7. Spring之c3p0连接池xml配置和使用举例

    1.导入jar包 c3p0-0.9.5.2.jar mchange-commons-java-0.2.11.jar 2.源码: beans.xml <beans xmlns="http ...

  8. Spring中Druid链接池的配置

    本文记录了使用Druid的方法, 包括Spring和Spring boot中使用Druid的配置方法. Spring中配置Druid连接池 以链接mysql为例 1 添加druid依赖 <dep ...

  9. spring hibernate4 c3p0连接池配置

    c3p0-0.9.1.2.jar,c3p0-oracle-thin-extras-0.9.1.2.jar,点此下载 <bean id="dataSource" class=& ...

随机推荐

  1. 洛谷 P1434 [SHOI2002]滑雪 解题报告

    这题方法有很多, 这里介绍2种: 方法1 很容易想到搜索, bfs或dfs应该都可以, 就不放代码了: 方法2 这题还可以用 dp 来做. 做法:先将每个点按照高度从小到大排序,因为大的点只能向小的点 ...

  2. 2016 CCPC-Final

    A.The Third Cup is Free #include <bits/stdc++.h> using namespace std; typedef long long ll; in ...

  3. 字典排序permutation

    理论 C++ 中的next_permutation 一般作为正序全排列的使用规则,其实这个就是正序字典排序的实现. 比如我们要对 列表 [1,2,3]  做full permutation 一般使用递 ...

  4. 湖南大学新生赛C,G,J题解

    C: 思路:做几组数据就基本能发现规律,奇数为-1,偶数为1 代码: #include<cstdio> #include<iostream> #include<cstri ...

  5. Ubuntu同时忘记用户密码和root密码

    在设置密码的时候,用到了小键盘,重启后再次用小键盘输入密码时,发现输入的并不是数字,而是其他符号.所以在设置关键信息的时候,小键盘还是得慎用啊. 解决方案: 在引导界面也就是开机倒计时的时候,按下 e ...

  6. 038 Count and Say 数数并说

    数数并说序列是一个整数序列,第二项起每一项的值为对前一项的计数,其前五项如下:1.     12.     113.     214.     12115.     1112211 被读作 " ...

  7. 记录一个修改application.properties时遇到的坑

    有一个需求是会频繁修改配置文件中的常量,为了方便就会用unzip解压war包,修改propertites中的值后重新打war 包,部署,但是发现修改的值没有起作用,,一直在纠结...后来发现其实在编译 ...

  8. 1137 - Sin your life sin公式 + 枚举

    http://www.ifrog.cc/acm/problem/1137 和差化积公式, 变成2 * sin((x + y) / 2) * cos((x - y) / 2) + sin(n - (x ...

  9. nodejs 实践:express 最佳实践(六) express 自省获得所有的路由

    nodejs 实践:express 最佳实践(六) express 自省获得所有的路由 某些情况下,你需要知道你的应用有多少路由,这在 express 中没有方法可以.因此我这边曲线了一下,做成了一个 ...

  10. IO(Properties、序列化流、打印流、CommonsIO)

    第1章 Properties类 1.1 Properties类介绍 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字 ...