> 文章列表 > JdbcTemplate总结

JdbcTemplate总结

JdbcTemplate总结

JdbcTemplate总结

JdbcTemplate技术Spring技术里面提供的一种数据库访问技术。之前学习的数据库技术是 JdbcUtils类完成的,现在用JdbcTemplate新技术了。

使用JdbcTemplate技术的本质就是:通过 IOC容器配置一个 JdbcTemplate对象,使用它来完成对数据库表的各种操作。

创建新数据库,创建表 Monster过程省略。

1、引入相关的jar包

在这里插入图片描述

2、创建配置文件 src/jdbc.properties

这个文件是填写数据库驱动相关的信息。

jdbc.userName=root
jdbc.password=hsp//自己改密码
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring

3、创建配置文件 src/JdbcTemplate_ioc.xml

改文件的作用:

  • 加载引入 jdbc.properties,引入驱动信息
  • 配置数据源,数据源来自 "com.mchange.v2.c3p0.ComboPooledDataSource
<?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"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"><!--    加载引入 jdbc.properties,引入驱动信息--><context:property-placeholder location="classpath:jdbc.properties"/>
<!--    配置数据源,数据源来自 `"com.mchange.v2.c3p0.ComboPooledDataSource`--><bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"><property name="user" value="${jdbc.userName}"></property><property name="password" value="${jdbc.password}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.url}"></property></bean><bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"><property name="dataSource" ref="dataSource"/></bean>
</beans>

4、JdbcTemplateTest.java测试文件

public class JdbcTemplateTest {//查询 id=100 的 monster 并封装到 Monster 实体对象@Testpublic void selectDataByJdbcTemplate() {ApplicationContext ioc =new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);String sql="select * from monster where id =100";RowMapper<Monster> rowMapper = new BeanPropertyRowMapper<Monster>(Monster.class);Monster monster = bean.queryForObject(sql, rowMapper);System.out.println("monster="+monster);}
///* 查询多条记录* 查询 id>=200 的 monster 并封装到 Monster 实体对象*/@Testpublic void selectMulDataByJdbcTemplate(){ApplicationContext ioc =new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);String sql="select * from monster where id >=?";RowMapper<Monster> rowMapper = new BeanPropertyRowMapper<Monster>(Monster.class);List<Monster> monsterList = bean.query(sql, rowMapper, 100);for (Monster monster :monsterList) {System.out.println(monster);}}/* 查询返回结果只有一行一列的值* 12. 查询返回结果只有一行一列的值,比如查询 id=100 的怪物名*/@Testpublic void selectScalarByJdbcTemplate(){ApplicationContext ioc =new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);String sql="select name from monster where id = 100";String name = bean.queryForObject(sql, String.class);System.out.println("name="+name);}//测试连接是否成功@Testpublic void testDataSource() throws SQLException {ApplicationContext ioc =new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");DataSource dataSource = ioc.getBean(DataSource.class);Connection conn = dataSource.getConnection();System.out.println("conn="+conn);conn.close();}//添加数据@Testpublic void addDataSource() throws SQLException {ApplicationContext ioc =new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);String sql="INSERT INTO monster VALUE(?,?,?)";int affected=bean.update(sql,700,"红孩儿","枪法厉害2");System.out.println("成功行数:"+affected);}//修改数据@Testpublic void updateDataSource() throws SQLException {ApplicationContext ioc =new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);String sql="update monster set skill=? where id=?";int affect = bean.update(sql, "美女计", 300);System.out.println("affected="+affect);System.out.println("update 成功!");}//批量添加数据@Testpublic void updateBatchDataSource() throws SQLException {ApplicationContext ioc =new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);String sql="insert into monster values(?,?,?);";List<Object[]> param_list = new ArrayList<>();param_list.add(new Object[]{500,"白骨精","吃人"});param_list.add(new Object[]{600,"青蛇精","吃小孩"});bean.batchUpdate(sql,param_list);System.out.println("批量添加成功!");}
}