Spring——JdbcTemplate
简介
它是一个Spring框架提供的一个对象。是对原始JDBC API对象的简单封装。
Idea快捷键:
双击shift,快速查询类的源代码
shift+shift:输入RowMapper
ctrl+shift+B:选中一个类名,看它的实现
导包
<dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>5.0.5.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>5.0.5.RELEASEversion> dependency>
配置
class="com.mchange.v2.c3p0.ComboPooledDataSource">
class="org.springframework.jdbc.core.JdbcTemplate" scope="singleton">
其他类
BeanProperRowMapper:实体属性行映射
作用:作为update的参数,用于直接封装多个bean对象为:List
实例化:
new BeanProperRowMapper
方法
void setDataSource(DataSource datasource)
参数:
dataSource:数据源
返回值:void
返回值意义:无
作用:通过dataSource设置JdbcTemplate
...args :代表有多少占位符。就多少参数
int update(String str,...args)
参数:
str:sql语句
...args:占位符赋值
返回值:int
返回值意义:影响行数
作用:更新操作
T queryForObject(String sql , T.class )
参数:
sql:查询语句
T.class:基本类型的封装类型
返回值:基本类型T
返回值意义:获取查询结果的基本类型T
作用:获取查询结果的基本类T
T queryForObject(String sql , BeanProperRowMap
参数:
sql:sql查询语句
BeanProperRowMapper:封装多个Bean为List
...args:为占位符赋值
返回值:Object
返回值意义:获取查询结果的封装对象
作用:获取查询结果的封装对象
List
参数:
sql:sql查询语句
BeanProperRowMapper:封装多个Bean为List
...args:为占位符赋值
返回值:List
返回值意义:获取查询结果集的封装对象集合
作用:获取查询结果集的封装对象集合
使用
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JdbcTemplateTest { @Autowired private JdbcTemplate jdbcTemplate; @Test // 测试JDBC模板开发步骤 public void test6() throws PropertyVetoException, SQLException { //查询操作,查询总数 Integer integer = jdbcTemplate.queryForObject("select count(money) from account ", Integer.class); System.out.println(integer); } @Test // 测试JDBC模板开发步骤 public void test5() throws PropertyVetoException, SQLException { //查询操作,查询单个对象 Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new BeanPropertyRowMapper(Account.class), 4); System.out.println(account); } @Test // 测试JDBC模板开发步骤 public void test4() throws PropertyVetoException, SQLException { //查询操作,查询多个对象 List query = jdbcTemplate.query("select * from account ", new BeanPropertyRowMapper (Account.class)); ArrayList accountArray = (ArrayList ) query; System.out.println(accountArray.toString()); } @Test // 测试JDBC模板开发步骤 public void test3() throws PropertyVetoException, SQLException { //更新操作 int row = jdbcTemplate.update("insert into account (name,money) values (?,?)","lisi","7000"); } @Test // 测试JDBC模板开发步骤 public void test2() throws PropertyVetoException, SQLException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); //更新操作 int row = jdbcTemplate.update("insert into account (name,money) values (?,?)","zhangsan","4000"); } @Test // 测试JDBC模板开发步骤 public void test1() throws PropertyVetoException, SQLException { // 创建数据源对象 ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/user"); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("844597608a"); System.out.println(comboPooledDataSource.getConnection()); JdbcTemplate jdbcTemplate = new JdbcTemplate(); // 设置数据源,获取Connect对象 jdbcTemplate.setDataSource(comboPooledDataSource); //更新操作 int row = jdbcTemplate.update("insert into account (name,money) values (?,?)","tom","5000"); } }