JdbcTemplate常⽤⽅法
JdbcTemplate简介
  JdbcTemplate是Spring JDBC的核⼼类,借助该类提供的⽅法可以很⽅便的实现数据的增删改查。
  Spring对数据库的操作在jdbc上⾯做了深层次的封装,使⽤spring的注⼊功能,可以把DataSource注册到JdbcTemplate之中。
  JdbcTemplate位于中。其全限定命名为org.JdbcTemplate。要使⽤
JdbcTemlate还需⼀个这个包包含了事务和异常控制
JdbcTemplate主要提供以下五类⽅法:
execute⽅法:可以⽤于执⾏任何SQL语句,⼀般⽤于执⾏DDL语句;
update⽅法及batchUpdate⽅法:update⽅法⽤于执⾏新增、修改、删除等语句;batchUpdate⽅法⽤于执⾏批处理相关语句;
query⽅法及queryForXXX⽅法:⽤于执⾏查询相关语句;
call⽅法:⽤于执⾏存储过程、函数相关语句。
xml中的配置:
<!-- 扫描 -->
<context:component-scan base-package="j.*"></context:component-scan>
<!-- 不属于⾃⼰⼯程的对象⽤bean来配置 -->
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close" p:username="root" p:password="qw13579wq">
<property name="driverClassName" value="sql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>
</bean>
<!-- 配置jdbcTemplate -->
<bean class="org.JdbcTemplate" p:dataSource-ref="dataSource"></bean>
常⽤⽅法:
数据库user_info表:
我们先创建⼀个实体对象,对应数据库表中的信息,⽅便之后的查询操作
j.vo;
public class UserInfo {
private int id;
private String userName;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo [id=" + id + ", userName=" + userName + ", password=" + password + "]";
}
}
修改(包含增、删、改):update()⽅法,另有批量插⼊⽅法batchUpdate()
update()⽅法增删改:
UserInfoDao.java代码:
@Repository
public class UserInfoDao {
@Autowired
//从容器中⾃动扫描获取jdbcTemplate
private JdbcTemplate jdbcTemplate;
//update()实现增加数据
public boolean insert(int id,String userName,String password){
String sql = "insert into user_info values (?,?,?)";
return jdbcTemplate.update(sql,id,userName,password)>0;
}
//update()实现修改
public boolean update(int id,String userName,String password){
String sql = "update user_info set user_name=?,password=? where id=?";
return jdbcTemplate.update(sql,userName,password,id)>0;
}
/
/update()实现删除
public boolean delete(int id){
String sql = "delete from user_info where id=?";
return jdbcTemplate.update(sql,id)>0;
}
}
测试类代码:
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("l");        UserInfoDao userDao = Bean(UserInfoDao.class);
boolean insert = userDao.insert(1,"Jim", "123");
boolean update = userDao.update(1,"Tom","123456");
boolean delete = userDao.delete(1);
     System.out.println("插⼊:"+insert+",修改:"+update+",删除:"+delete);
}
}
测试结果:
查询:查询单个值、查询⼀个对象、查询多个对象
查询单个值
//查询单个值
public boolean login(String userName,String password){
try {
String sql = "select id from user_info where user_name=? and password=?";
jdbcTemplate.queryForObject(sql,String.class,userName,password);
return true;
} catch (DataAccessException e) {
return false;
}
}
查询⼀个对象:RowMapper⽅法和ResultSetExtractor⽅法
//查询单个对象
public UserInfo getById(int id){
String sql = "select id,user_name,password from user_info where id=?";
//RowMapper⽅法
class UserInfoRowMapper implements RowMapper<UserInfo>{
@Override
public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
UserInfo userInfo = new UserInfo();
userInfo.Int("id"));
userInfo.String("user_name"));
userInfo.String("password"));
return userInfo;
}
}
return jdbcTemplate.queryForObject(sql,new UserInfoRowMapper(),id);
//RowMapper⽅法的Lambda表达式
return jdbcTemplate.queryForObject(sql,(ResultSet rs,int rowNum)->{
UserInfo userInfo = new UserInfo();
userInfo.Int("id"));
userInfo.String("user_name"));
userInfo.String("password"));
return userInfo;
},id);
/
jdbctemplate查询一条数据
/ResultSetExtractor⽅法
class UserInfoResultSet implements ResultSetExtractor<UserInfo>{
@Override
public UserInfo extractData(ResultSet rs) throws SQLException, DataAccessException {            UserInfo userInfo = null;
()){
userInfo = new UserInfo();
userInfo.Int("id"));
userInfo.String("user_name"));
userInfo.String("password"));
}
return userInfo;
}
}
return jdbcTemplate.query(sql,new UserInfoResultSet(),id);
//ResultSetExtractor⽅法的lambda表达式
return jdbcTemplate.query(sql,(ResultSet rs)->{
UserInfo userInfo = null;
()){
userInfo = new UserInfo();
userInfo.Int("id"));
userInfo.String("user_name"));
userInfo.String("password"));
}
return userInfo;
},id);
}
查询多个对象:RowMapper⽅法和ResultSetExtractor⽅法
//查询多个对象
public List<UserInfo> selectAll(){
String sql = "select id,user_name,password from user_info";
//RowMapper⽅法
class UserInfoRowMapper implements RowMapper<UserInfo>{
@Override
public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
UserInfo userInfo = new UserInfo();
userInfo.Int("id"));
userInfo.String("user_name"));
userInfo.String("password"));
return userInfo;
}
}
return jdbcTemplate.query(sql,new UserInfoRowMapper());
//RowMapper⽅法的Lambda表达式
return jdbcTemplate.query(sql,(ResultSet rs,int rowNum)->{
UserInfo userInfo = new UserInfo();
userInfo.Int("id"));
userInfo.String("user_name"));
userInfo.String("password"));
return userInfo;
});
//ResultSetExtractor⽅法
class UserInfoResultSet implements ResultSetExtractor<List<UserInfo>>{
@Override
public List<UserInfo> extractData(ResultSet rs) throws SQLException, DataAccessException {            List<UserInfo> list = new ArrayList<>();
()){
UserInfo userInfo = new UserInfo();
userInfo.Int("id"));
userInfo.String("user_name"));
userInfo.String("password"));
list.add(userInfo);
}
return list;
}
}
return jdbcTemplate.query(sql, new UserInfoResultSet());
//ResultSetExtractor⽅法的lambda表达式
return jdbcTemplate.query(sql,(ResultSet rs)->{
List<UserInfo> list = new ArrayList<>();
()){
UserInfo userInfo = new UserInfo();
userInfo.Int("id"));
userInfo.String("user_name"));
userInfo.String("password"));
list.add(userInfo);
}
return list;
});
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。