• Spring_database_Template


    配置applicationContext.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" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!--自动装箱-->
    <context:component-scan base-package="com.donghua.jdbc"></context:component-scan>

    <!--引入外部配置文件-->
    <context:property-placeholder location="classpath:com/donghua/jdbc/jdbc.properties"/>
    <bean id="userDao" class="com.donghua.jdbc.UserDao"></bean>

    <!-- 一、配置数据库连接池 ComboPooledDataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

    <!-- Connection properties -->
    <property name="driverClass" value="${driverClass}" />
    <property name="jdbcUrl" value="${jdbcUrl}" />
    <property name="user" value="${username}" />
    <property name="password" value="${password}" />
    <!-- Pool properties-->
    <property name="minPoolSize" value="2" />
    <property name="maxPoolSize" value="10" />
    <property name="maxStatements" value="50" />
    <property name="idleConnectionTestPeriod" value="3000" />
    <property name="loginTimeout" value="300" />


    </bean>


    <!-- 二、配置JdbcTemplate,引入模板数据源 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    </beans>

    User.java

    public class User {
    private int age;
    private String name;


    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }

    }

    package com.donghua.jdbc;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

    import javax.annotation.Resource;

    import org.springframework.dao.DataAccessException;
    import org.springframework.jdbc.core.ConnectionCallback;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Component;

    @Component
    public class UserDao {
    //注入模板
    @Resource
    private JdbcTemplate jdbcTemplate;

    public void save(final User u){
    jdbcTemplate.execute(new ConnectionCallback() {
    @Override
    public Object doInConnection(Connection conn) throws SQLException,
    DataAccessException {
    String sql ="insert into t_user(age,userName) values(?,?)";
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.setInt(1, u.getAge());
    ps.setString(2, u.getName());
    ps.execute();
    ps.close();
    return null;
    }
    });
    }

    /* (non-Javadoc)
    * @see com.donghua.jdbc.UserInterface#update()
    */
    public void update(){}
    }

     

     

    测试

    public class UserDaoTest {
    private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml", getClass());

    private UserDao userDao1 = (UserDao) ac.getBean("userDao");

    @Test
    public void testSave() {
    User user = new User();
    user.setName("李四");
    userDao1.save(user);

    }

    @Test
    public void testUpdate() {

    }

    }

  • 相关阅读:
    PHP分页类
    phpexcel 控制导出数据内容和行数
    phpexcel 导入 和 导出
    PHP无限极分类
    php 对查询结果集进行排序
    php 删除文件夹及文件夹内文件函数
    php 字符串截取函数
    php 获取用户登录的ip
    js layer页面层加载新网站
    分享到qq
  • 原文地址:https://www.cnblogs.com/daxiong225/p/4715246.html
Copyright © 2020-2023  润新知