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

可以使用非常简练的Service类

UserService类代码如下:

package com.swift;

public class UserService {
private UserDao userDao; public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean add() {
return userDao.add();
}
}

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

同样UserDao类也非常简练

package com.swift;

import org.springframework.jdbc.core.JdbcTemplate;

public class UserDao {
private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} public boolean add() {
// ComboPooledDataSource dataSource=new ComboPooledDataSource();
// dataSource.setDriverClass("com.mysql.jdbc.Driver");
// dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sw_database");
// dataSource.setUser("root");
// dataSource.setPassword("root"); String sql="insert into sw_user(username,password) values(?,?)";
int count=jdbcTemplate.update(sql,"弹痕","战侠歌");
if(count==1) {
return true;
}
return false;
}
}

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

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

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

XML配置文件代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- c3p0连接池得到dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="userDao" class="com.swift.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <bean id="userService" class="com.swift.UserService">
<property name="userDao" ref="userDao"></property>
</bean> </beans>

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

代码如下:

package com.swift;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; @WebServlet("/test")
public class ServletTest extends HttpServlet {
private static final long serialVersionUID = 1L; public ServletTest() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().append("Served at: ").append(request.getContextPath()); //使用JdbcTemplat的queryForObject方法
ApplicationContext context=new ClassPathXmlApplicationContext("c3p0.xml");
UserService userService= (UserService) context.getBean("userService");
if(userService.add()) {
response.getWriter().append("添加成功!");
}else {
response.getWriter().append("添加失败!");
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

浏览器中结果:

数据库中结果:

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. Python基础:模块化来搭项目

    简单模块化 import 最好在最顶端 sys.path.append("..")表示把当前程序所在位置向上提了一级 在python3规范中,__init__.py并不是必须的. ...

  2. spring框架_AOP和注解

    1.什么是AOP :全称是Aspect Oriented Programming即:面向切面编程. 简单来说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础 ...

  3. B-Tree深入理解

    定义: 根节点至少包括两个孩子 树中每个节点最多含有m个孩子(m>=2) 除根节点和叶子节点外,其他每个节点字少有(ceil(m/2):去上线),个孩子. 所有叶子节点都位于同一高度 假设每个非 ...

  4. visdom可视化pytorch训练过程

    一.前言 在深度学习模型训练的过程中,常常需要实时监听并可视化一些数据,如损失值loss,正确率acc等.在Tensorflow中,最常使用的工具非Tensorboard莫属:在Pytorch中,也有 ...

  5. blur和focus的运用

    这两个事件不仅仅只能运用与input.span之类的元素.还可以运用于window. 可以切换title. 当切换当前页面时,改变title的文字为‘离开了’. <!DOCTYPE html&g ...

  6. MySQL无法启动Couldn't find MySQL server (/usr/bin/mysqld_safe)解决办法(来源网络)

    MySQL无法启动Couldn't find MySQL server (/usr/bin/mysqld_safe) 启动的时候,报上述错误,从这个报错来看,多半是因为读取到了另外的my.cnf导致的 ...

  7. standby checking script 3则 V1 shell 脚本

    ---1 #!/bin/sh export ORACLE_SID=hdbexport ORACLE_BASE=/db/hdbdg/app/product/databaseexport ORACLE_H ...

  8. 在linux上怎么查看tomcat日志

    进入到tomcat的logs文件夹 tail -f catalina.out

  9. ORACLE行转列通用过程(转)

    1.使用视图 SQL code? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 create or r ...

  10. A(光圈)S(快门) P(程序) M(手动)曝光模式

    摄影笔记:http://mp.weixin.qq.com/s/SCzXybbCCE8VzfAQKTqlDw 曝光模式,指的就是M档(手动曝光).A档(Av档,光圈优先).S档(Tv档,快门优先),Au ...