JdbcTemplate的CRUD操作

实体类

package com.itheima.domain;

import java.io.Serializable;

/**
* @Author: lijiahao
* @Description: 账户的实体类
* @Data: Create in 1:32 2020/2/9
* @Modified By:
*/
public class Account implements Serializable {

private Integer id;
private String name;
private Float money;

@Override
public String toString() {
return "account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + 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;
}
}

JdbcTemplate测试类
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;

/**
* @Author: lijiahao
* @Description: jdbctemplate的最基本用法:CRUD
* @Data: Create in 1:34 2020/2/9
* @Modified By:
*/
public class JdbcTemplateDemo3 {
public static void main(String[] args) {

//1.获取容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
JdbcTemplate jt = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
//3.执行操作

//保存
jt.update("insert into account (name,money) values (?,?)","eee",3333f);
//更新
jt.update("update account set name=? ,money=? where id=?","test",1523f,7);
//删除
jt.update("delete from account where id=?", 8);

//查询所有
//List<Account> accountList = jt.query("select * from account where money > ?",new AccountRowMapper() , 1000f);
List<Account> accountList = jt.query("select * from account where money > ?",new BeanPropertyRowMapper<Account>(Account.class), 1000f);
for(Account account : accountList){
//System.out.println(account);
}
//查询一个
List<Account> accountList1 = jt.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class), 1);
System.out.println(accountList1.isEmpty()?"没有内容":accountList1.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>{

//把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中
public Account mapRow(ResultSet resultSet, int rowNum) throws SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setMoney(resultSet.getFloat("money"));
account.setName(resultSet.getString("name"));
return account;
}
}

配置文件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/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<!--配置JdbTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8&amp;useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>


猜你喜欢

转载自www.cnblogs.com/lijiahaoAA/p/12286059.html
今日推荐