• SpringJDBC


    ①新建db.properties

    jdbc.user=root
    jdbc.password=langsin
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql:///test
    
    jdbc.initPoolSize=5
    jdbc.maxPoolSize=10

    public class Customer {
        
        private String customid;
        private String name;
        private String phone;
        public String getCustomid() {
            return customid;
        }
        public void setCustomid(String customid) {
            this.customid = customid;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
        @Override
        public String toString() {
            return "Customer [customid=" + customid + ", name=" + name + ", phone="
                    + phone + "]";
        }
    }

    import static org.junit.Assert.*;
    
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.sql.DataSource;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
    
    public class JDBCTest {
        
        private ApplicationContext ctx = null;
        private JdbcTemplate jdbcTemplate;
        
        
        {
            ctx = new ClassPathXmlApplicationContext("./applicationContext.xml");
            jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
        }
        @Test
        public void testQueryForObject2(){
            String sql = "select count(customid) from customers";
            long count = jdbcTemplate.queryForObject(sql,Long.class);
            System.out.println(count);
        }
        
        public void testQueryForList(){
            String sql = "select customid,name,phone from customers where customid > ?";
            RowMapper<Customer> rowMapper = new BeanPropertyRowMapper<Customer>(Customer.class);
            List<Customer> list = jdbcTemplate.query(sql,rowMapper,1);
            System.out.println(list);
        }
        
        public void testQueryForObject(){
            String sql = "select customid,name,phone from customers where customid = ?";
            RowMapper<Customer> rowMapper = new BeanPropertyRowMapper<Customer>(Customer.class);
            Customer customer = jdbcTemplate.queryForObject(sql,rowMapper,1);
            System.out.println(customer);
        }
        
        
        public void testUpdate(){
            String sql = "update customers set name=? where customid=?";
            jdbcTemplate.update(sql,"liu",2);
        }
        
        public void testBatchUpdate(){
            String sql = "insert into customers values(?,?,?)";
            List<Object[]> list = new ArrayList<Object[]>();
            list.add(new Object[]{"1","zhang1","1101"});
            list.add(new Object[]{"2","zhang2","1102"});
            list.add(new Object[]{"3","zhang3","1103"});
            list.add(new Object[]{"4","zhang4","1104"});
            list.add(new Object[]{"5","zhang5","1105"});
            
            jdbcTemplate.batchUpdate(sql, list);
        }
        
        
        public void testDataSource() throws SQLException {
            DataSource dataSource = (DataSource) ctx.getBean("dataSource");
            System.out.println(dataSource.getConnection());
        }
    
    }

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:c="http://www.springframework.org/schema/c"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
        
        <!-- 导入资源文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        
        
        <!-- 配置C3P0数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            
            <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
            
        </bean>
        
        <!-- 配置 spring 的 jdbcTemplate -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    </beans>
  • 相关阅读:
    OCP-1Z0-053-V12.02-40题
    OCP-1Z0-053-V12.02-255题
    OCP-1Z0-053-V12.02-407题
    OCP-1Z0-053-V12.02-298题
    OCP-1Z0-053-V12.02-38题
    OCP-1Z0-053-V12.02-274题
    OCP-1Z0-053-V12.02-159题
    OCP-1Z0-053-V12.02-406题
    OCP-1Z0-053-V12.02-369题
    OCP-1Z0-053-V12.02-610题
  • 原文地址:https://www.cnblogs.com/sdnu-zhang/p/8528186.html
Copyright © 2020-2023  润新知