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

通过配置文件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);
    }

}

浏览器中结果:

数据库中结果:

 

转载于:https://www.cnblogs.com/qingyundian/p/8127240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值