Spring的jdbc模板3:完成CURD操作

测试类代码如下

package zcc.spring_jdbc.demo2;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import domain.Student;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext5.xml")
public class SpringDemo1 {

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Test
    /*
     * 增加
     */
    public void demo1() {
        jdbcTemplate.update("insert into student values(?,?,?)", 8, "周司", "男");

    }

    @Test
    /*
     * 删除
     */
    public void demo2() {
        jdbcTemplate.update("delete from student where id = ?", 2);

    }

    @Test
    /*
     * 修改
     */
    public void demo3() {
        jdbcTemplate.update("update student set name = ? where id = ? ", "李三", 3);

    }

    @Test
    /*
     * 查询字符串
     */
    public void demo4() {
        String name = jdbcTemplate.queryForObject("select name from student where id = ? ", String.class, 1);
        System.out.println(name);
    }

    @Test
    /*
     * 查询总数
     */
    public void demo5() {
        Long total = jdbcTemplate.queryForObject("select count(*) from student ", Long.class);
        System.out.println(total);
    }

    @Test
    /*
     * 查询单个对象
     */
    public void demo6() {
        Student student = jdbcTemplate.queryForObject("select * from student where id = ? ", new myRowMapper(), 1);
        System.out.println(student.toString());
    }

    @Test
    /*
     * 查询多个对象
     */
    public void demo7() {
        List<Student>  list= jdbcTemplate.query("select * from student", new myRowMapper());
        for(Student student:list) {
            System.out.println(student.toString());
        }
    }
    
    /*
     *数据封装 
     */
    class myRowMapper implements RowMapper<Student> {
        public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setSex(rs.getString("sex"));
            return student;
        }
    }
}

applicationContext5.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/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
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- =============配置spring内置的连接池================== -->
    <!-- Springdemo1每一次都要New,交给spring管理之后只需要new一次 -->
    <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        ===属性注入=== <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
        <property name="url" value="jdbc:mysql://localhost:3307/test"></property> 
        <property name="username" value="root"></property> <property name="password" 
        value="123456"></property> </bean> -->

    <!-- ==============配置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://localhost:3307/test"></property> <property 
        name="user" value="root"></property> <property name="password" value="123456"></property> 
        </bean> -->

    <!-- ===============引入属性文件============= -->
    <!-- 第一种方式通过一个bean标签引入的(很少) -->
    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="location" value="classpath:jdbc.properties"/> </bean> -->
    <!-- 第二种方式,通过context标签引入 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    <!-- ==========配置c3p0连接池============ -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- ==============配置jdbc模板================ -->
    <bean id="jdbcTemplate"
        class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- ==属性注入== -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

jdbc.properties的代码如下

 

posted @ 2019-02-19 16:50  曾聪聪  阅读(361)  评论(0编辑  收藏  举报