Spring中的JdbcTemplate的使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring中的JdbcTemplate的使用

阿尔伯特-李古拉-逸凡   2020-03-17 我要评论
# 一、jdbcTemplate的作用 - 它就是用于和数据库交互的,实现对表的crud。与dbutils相似 # 二、JdbcTemplate的使用 ```xml org.springframework spring-context 5.0.2.RELEASE org.springframework spring-jdbc 5.0.2.RELEASE org.springframework spring-tx 5.0.2.RELEASE mysql mysql-connector-java 5.1.6 ``` - 1.JdbcTemplate的最基本用法 ```java package club.leyvan.jdbctemplate; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; /** * JdbcTemplate的最基本用法 */ public class JdbcTemplateDemo1 { public static void main(String[] args) { //准备数据源:spring的内置数据源 DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3306/test"); ds.setUsername("root"); ds.setPassword("123"); //1.创建JdbcTemplate对象 JdbcTemplate jt = new JdbcTemplate(); //给jt设置数据源 jt.setDataSource(ds); //2.执行操作 jt.execute("insert into account(name,money)values('abc',1000)"); } } ``` - 2.JdbcTemplate基于Ioc的注入用法 xml的配置 ```xml <?xml version="1.0" encoding="UTF-8"?> ``` 配置后的java代码 ```java package club.leyvan.jdbctemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; /** * JdbcTemplate的最基本用法 */ public class JdbcTemplateDemo2 { public static void main(String[] args) { //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.获取对象 JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class); //3.执行操作 jt.execute("insert into account(name,money)values('ddd',2222)"); } } ``` - JdbcTemplate的CRUD操作 ```java package club.leyvan.jdbctemplate; import com.itheima.domain.Account; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; /** * JdbcTemplate的CRUD操作 */ public class JdbcTemplateDemo3 { public static void main(String[] args) { //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.获取对象 JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class); //3.执行操作 //保存 // jt.update("insert into account(name,money)values(?,?)","eee",3333f); //更新 // jt.update("update account set name=?,money=? where id=?","test",4567,7); //删除 // jt.update("delete from account where id=?",8); //查询所有 // List accounts = jt.query("select * from account where money > ?",new AccountRowMapper(),1000f); //使用spring提供的封装策略 // List accounts = jt.query("select * from account where money > ?",new BeanPropertyRowMapper(Account.class),1000f); // for(Account account : accounts){ // System.out.println(account); // } //查询一个 // List accounts = jt.query("select * from account where id = ?",new BeanPropertyRowMapper(Account.class),1); // System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0)); //查询返回一行一列(使用聚合函数,但不加group by子句) Long count = jt.queryForObject("select count(*) from account where money > ?",Long.class,1000f); System.out.println(count); } } /** * 定义Account的封装策略 */ class AccountRowMapper implements RowMapper{ /** * 把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中 * @param rs * @param rowNum * @return * @throws SQLException */ @Override public Account mapRow(ResultSet rs, int rowNum) throws SQLException { Account account = new Account(); account.setId(rs.getInt("id")); account.setName(rs.getString("name")); account.setMoney(rs.getFloat("money")); return account; } } ```

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们