spring4-4-jdbc-01

1.建立数据属性文件db.properties

jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/spring-1?characterEncoding=UTF-8

jdbc.initialPoolSize=5
jdbc.maxPoolSize=10

 

2.建立配置文件

  添加命名空间context,beans.

 2.1 读入数据属性文件

    <!-- 1.导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

 

 2.2 配置C3P0数据源:

    <!-- 2.配置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.initialPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

 

 2.3 配置JdbcTemplate bean:

    <!-- 3.配置 jdbc template -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

 

3.应用

3.1加载Sping IOC容器

    private ApplicationContext ctx = null;
    private JdbcTemplate jdbcTemplate;
    @Before
    public void init(){
        ctx = new ClassPathXmlApplicationContext("applicationContext-jdbc.xml");
        jdbcTemplate = ctx.getBean("jdbcTemplate",JdbcTemplate.class);
    }
    @Test
    public void testJdbc() throws SQLException {
        DataSource dataSource = ctx.getBean("dataSource",DataSource.class);
        System.out.println(dataSource.getConnection());
    }

 

 3.2测试连接:

        DataSource dataSource = ctx.getBean("dataSource",DataSource.class);
        System.out.println(dataSource.getConnection());

 

 3.3 测试插入,更新,删除

        //insert
        String insertSql = "insert employees(name,email,dept_id) values(?,?,?)";
        jdbcTemplate.update(insertSql, "张三","zhangsan@si.com","3");
        
        //update
        String updateSql = "update employees set name= ?  where id = ?";
        jdbcTemplate.update(updateSql, "张四","1");
        
        //delete
        String deleteSql = "delete from  employees where id=?";
        jdbcTemplate.update(deleteSql, "1");

 

3.4 批处理 插入,修改,删除

    //insert
        String insertSql = "insert employees(name,email,dept_id) values(?,?,?)";
        List<Object[]> batchArgs = new ArrayList<Object[]>();
        batchArgs.add(new Object[]{"张三","zhang@si.com","1"});
        batchArgs.add(new Object[]{"李四","li@si.com","2"});
        batchArgs.add(new Object[]{"王五","wang@si.com","3"});
        batchArgs.add(new Object[]{"钱六","qian@si.com","4"});
        jdbcTemplate.batchUpdate(insertSql, batchArgs);
        
        // update
        String updateSql = "update employees set email=? where id=?";
        batchArgs.clear();
        batchArgs.add(new Object[]{"zhangsan@si.com","1"});
        batchArgs.add(new Object[]{"lisi@si.com","2"});
        batchArgs.add(new Object[]{"wangwu@si.com","3"});
        batchArgs.add(new Object[]{"qianliu@si.com","4"});
        jdbcTemplate.batchUpdate(updateSql, batchArgs);
        
        // delete
        String deleteSql = "delete from employees  where id = ?";
        batchArgs.clear();
        batchArgs.add(new Object[]{"1"});
        batchArgs.add(new Object[]{"2"});
        batchArgs.add(new Object[]{"3"});
        batchArgs.add(new Object[]{"4"});
        jdbcTemplate.batchUpdate(deleteSql, batchArgs);

 4. 查询一行,返回对象

/**
     * 从数据库中获取一条记录, 实际得到对应的一个对象
     * 注意不是调用 queryForObject(String sql, Class<Employee> requiredType, Object... args) 方法!
     * 而需要调用 queryForObject(String sql, RowMapper<Employee> rowMapper, Object... args)
     * 1. 其中的 RowMapper 指定如何去映射结果集的行, 常用的实现类为 BeanPropertyRowMapper
     * 2. 使用 SQL 中列的别名完成列名和类的属性名的映射. 例如 last_name lastName
     * 3. 不支持级联属性. JdbcTemplate 到底是一个 JDBC 的小工具, 而不是 ORM 框架
     */
    @Test
    public void testQueryForObject(){
        String sql = "SELECT id, last_name lastName, email, dept_id as \"department.id\" FROM employees WHERE id = ?";
        RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);
        Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, 1);
        
        System.out.println(employee);
    }

 

5.查询类的集合

/**
     * 查到实体类的集合
     * 注意调用的不是 queryForList 方法
     */
    @Test
    public void testQueryForList(){
        String sql = "SELECT id, last_name lastName, email FROM employees WHERE id > ?";
        RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);
        List<Employee> employees = jdbcTemplate.query(sql, rowMapper,5);
        
        System.out.println(employees);
    }

 

6.查询单列值

/**
     * 获取单个列的值, 或做统计查询
     * 使用 queryForObject(String sql, Class<Long> requiredType) 
     */
    @Test
    public void testQueryForObject2(){
        String sql = "SELECT count(id) FROM employees";
        long count = jdbcTemplate.queryForObject(sql, Long.class);
        
        System.out.println(count);
    }

 

posted @ 2015-07-01 17:30  勿妄  阅读(256)  评论(0编辑  收藏  举报