JdbcTemplate的CRUD操作
生活随笔
收集整理的這篇文章主要介紹了
JdbcTemplate的CRUD操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Account.java
package com.itheima.domain;import java.io.Serializable;/*** 賬戶的實體類*/ public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';} }bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置JdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置數(shù)據(jù)源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/eesy"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean> </beans>JdbcTemplateDemo3.java
package com.itheima.jdbctemplate;import com.itheima.domain.Account; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper;import java.sql.ResultSet; import java.sql.SQLException; import java.util.List;/*** 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.執(zhí)行操作//保存 // 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<Account> accounts = jt.query("select * from account where money > ?",new AccountRowMapper(),1000f);/* List<Account> accounts = jt.query("select * from account where money > ?",new BeanPropertyRowMapper<Account>(Account.class),1000f);for(Account account : accounts){System.out.println(account);}*///查詢一個/* List<Account> accounts = jt.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),12);System.out.println(accounts.isEmpty()?"沒有內(nèi)容":accounts.get(0));*///查詢返回一行一列(使用聚合函數(shù),但不加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>{/*** 把結(jié)果集中的數(shù)據(jù)封裝到Account中,然后由spring把每個Account加到集合中* @param rs* @param rowNum* @return* @throws SQLException*/@Overridepublic 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;} }?
總結(jié)
以上是生活随笔為你收集整理的JdbcTemplate的CRUD操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JdbcTemplate在Spring的
- 下一篇: Spring Boot内容概要