码迷,mamicode.com
首页 > 数据库 > 详细

Spring学习-6-jdbc支持

时间:2016-08-11 00:59:54      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

Spring对jdbc技术提供了很好的支持

1) spring对c3p0连接池的支持很完善

2) spring对jdbc提供了JdbcTemplate,来简化jdbc操作

       JdbcTemplate模板工具类,类似于Dbutils组件

使用步骤:

1.找到相关jar文件

技术分享

 

Dept

package com.cx.jdbc;

/**
 * Created by cxspace on 16-8-10.
 */
public class Dept {

    private int deptId;

    private String deptName;

    public int getDeptId() {
        return deptId;
    }

    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }


}

  

com.cx.jdbc.UserDao01

 

package com.cx.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


/**
 * Created by cxspace on 16-8-10.
 */
public class UserDao01 {

  //直接注入jdbcTemplate
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void save(){
        String sql = "insert into t_dept(deptName) values(‘test01‘);";
        //使用jdbc模板简化操作
        jdbcTemplate.update(sql);
    }

    public void deleteById(int id){

        String sql = "delete from t_dept where deptId = ?";

        jdbcTemplate.update(sql,id);
    }

    public Dept findById(int id){

        String sql = "select * from t_dept WHERE  deptId = ?";

        List<Dept> list = jdbcTemplate.query(sql, new MyResult(),id);

        return (list!=null&&list.size()>0 ? list.get(0) : null);
    }

    public List<Dept> getAll(){

        String sql = "select * from t_dept";

        List<Dept> list = jdbcTemplate.query(sql,new MyResult());

        return list;
    }


    class MyResult implements RowMapper<Dept>{

        @Override
        public Dept mapRow(ResultSet resultSet, int i) throws SQLException {

            Dept dept = new Dept();

            dept.setDeptId(resultSet.getInt("deptId"));
            dept.setDeptName(resultSet.getString("deptName"));

            return dept;
        }
    }

}

  

com/cx/jdbc/bean.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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">


    <!--数据源对象c3p0连接池-->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///learnHibernate"></property>
        <property name="user" value="root"></property>
        <property name="password" value="33269456.cx"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>

    <!--创建jdbcTemplate对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--dao实例-->
    <bean id="userDao" class="com.cx.jdbc.UserDao01">
       <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

</beans>

  

com.cx.jdbc.App

package com.cx.jdbc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by cxspace on 16-8-10.
 */
public class App {

    //容器对象
    ApplicationContext ac = new ClassPathXmlApplicationContext("com/cx/jdbc/bean.xml");

    @Test
    public void testApp() throws Exception{

        UserDao01 userDao = (UserDao01)ac.getBean("userDao");

      //  System.out.println(userDao.findById(3));

      //  System.out.println(userDao.getAll());

      //   userDao.save();

        userDao.deleteById(9);
    }
}

  

 

Spring学习-6-jdbc支持

标签:

原文地址:http://www.cnblogs.com/cxspace/p/5759213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!