Spring JdbcTemplate
一根头发学一年 人气:0说明:基于atguigu笔记。
JdbcTemplate 是 Spring 对 JDBC 的封装,目的是使JDBC更加易于使用,JdbcTemplate是Spring的一部分。JdbcTemplate 处理了资源的建立和释放,它帮助我们避免一些常见的错误,比如忘了总要关闭连接。他运行核心的JDBC工作流,如Statement的建立和执行,而我们只需要提供SQL语句和提取结果即可。
如何使用
准备工作:
1.引入相关 jar 包
druid.java、mysql-connector-java.jar、spring-jdbc.java、spring-orm.java、spring-tx.java。
2.在 spring 配置文件配置数据库连接池
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql:///user_db" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> </bean>
3.配置 JdbcTemplate 对象,注入 DataSource
<!-- JdbcTemplate 对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--注入 dataSource--> <property name="dataSource" ref="dataSource"></property> </bean>
5.配置文件里开启组件扫描
<!-- 组件扫描 --> <context:component-scan base-package="com.atguigu"></context:component-scan>
6.创建 service 类,创建 dao 类,在 dao 注入 jdbcTemplate 对象
service
@Service public class BookService { //注入 dao @Autowired private BookDao bookDao; }
dao
@Repository public class BookDaoImpl implements BookDao { //注入 JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; }
数据库表对应的实体类
public class UserT { private String userId; private String username; private String ustatus; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUstatus() { return ustatus; } public void setUstatus(String ustatus) { this.ustatus = ustatus; } }
数据库操作
添加
dao:
@Repository public class BookDaoImpl implements BookDao { //注入 JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; //添加的方法 @Override public void add(Book book) { //1 创建 sql 语句 String sql = "insert into t_book values(?,?,?)"; //2 调用方法实现 Object[] args = {book.getUserId(), book.getUsername(), book.getUstatus()}; int update = jdbcTemplate.update(sql,args); System.out.println(update); } }
测试类:
@Test public void testJdbcTemplate() { ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); BookService bookService = context.getBean("bookService", BookService.class); Book book = new Book(); book.setUserId("1"); book.setUsername("java"); book.setUstatus("a"); bookService.addBook(book); }
修改
dao增加
@Override public void updateBook(Book book) { String sql = "update t_book set username=?,ustatus=? where user_id=?"; Object[] args = {book.getUsername(), book.getUstatus(),book.getUserId()}; int update = jdbcTemplate.update(sql, args); System.out.println(update); }
删除
dao增加
@Override public void delete(String id) { String sql = "delete from t_book where user_id=?"; int update = jdbcTemplate.update(sql, id); System.out.println(update); }
查询
1.返回某个值
dao增加
@Overridepublic int selectCount() {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->String sql = "select count(*) from t_book";Integer count = jdbcTemplate.queryForObject(sql, Integer.class);return count;}@Override public int selectCount() { String sql = "select count(*) from t_book"; Integer count = jdbcTemplate.queryForObject(sql, Integer.class); return count; }
2.返回对象
dao增加
@Override public Book findBookInfo(String id) { String sql = "select * from t_book where user_id=?"; //调用方法 Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id); return book; }
RowMapper 是接口,针对返回不同类型数据,使用这个接口里面实现类完成
数据封装。
3.返回集合
dao增加
@Override public List<Book> findAllBook() { String sql = "select * from t_book"; //调用方法 List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class)); return bookList; }
批量操作
1.批量修改
dao增加
@Override public void batchAddBook(List<Object[]> batchArgs) { String sql = "insert into t_book values(?,?,?)"; int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs); System.out.println(Arrays.toString(ints)); }
测试:
List<Object[]> batchArgs = new ArrayList<>(); Object[] o1 = {"3","java","a"}; Object[] o2 = {"4","c++","b"}; Object[] o3 = {"5","MySQL","c"}; batchArgs.add(o1); batchArgs.add(o2); batchArgs.add(o3); //调用批量添加 bookService.batchAdd(batchArgs);
2.批量
dao增加
@Override public void batchUpdateBook(List<Object[]> batchArgs) { String sql = "update t_book set username=?,ustatus=? where user_id=?"; int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs); System.out.println(Arrays.toString(ints)); }
3.批量删除
@Override public void batchDeleteBook(List<Object[]> batchArgs) { String sql = "delete from t_book where user_id=?"; int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs); System.out.println(Arrays.toString(ints)); }
加载全部内容