Spring对Jdbc的封装——JdbcTemplate的使用
链接:https://pan.baidu.com/s/15luDElW4oeEaP0nvEQ_40w
提取码:i2r1
JdbcTemplate是Spring对JDBC的封装,目的是使JDBC更加易于使用。JdbcTemplate是Spring的一部分。
接下来,使用JdbcTemplate进行增删改查(CRUD)的操作
写一个pojo类(不是重点)
package top.bigking.pojo;
import java.util.Date;
public class User {
private Integer id;
private String username;
private String password;
private Date birthday;
private Integer age;
public User() {
}
public User(Integer id, String username, String password, Date birthday, Integer age) {
this.id = id;
this.username = username;
this.password = password;
this.birthday = birthday;
this.age = age;
}
public User(String username, String password, Date birthday, Integer age) {
this.username = username;
this.password = password;
this.birthday = birthday;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday=" + birthday +
", age=" + age +
'}';
}
}
再写一个接口
package top.bigking.dao;
import top.bigking.pojo.User;
import java.util.List;
public interface UserDao {
public int insertUser(User user);
public int deleteById(Integer id);
public int updateUser(User user);
public List<User> queryUser();
public User selectUserById(Integer id);
}
接下来对接口进行实现:
package top.bigking.dao.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import top.bigking.dao.UserDao;
import top.bigking.pojo.User; import java.util.List; @Repository("userDao")
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate; @Override
public int insertUser(User user) {
String sql = "insert into t_user(user_name, password, birthday, age) values(?, ?, ?, ?)";
return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getBirthday(), user.getAge());
} @Override
public int deleteById(Integer id) {
String sql = "delete from t_user where id = ?";
return jdbcTemplate.update(sql, id);
} @Override
public int updateUser(User user) {
String sql = "update t_user set user_name = ?, password = ?, birthday = ?, age = ?";
return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getBirthday(), user.getAge());
} @Override
public List<User> queryUser() {
String sql = "select * from t_user";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
} @Override
public User selectUserById(Integer id) {
String sql = "select * from t_user where id = ?";
return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
}
}
接下来使用单元测试一个一个测试:
package top.bigking.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.bigking.dao.UserDao;
import top.bigking.pojo.User; import java.util.Date;
import java.util.List; public class TestJdbc {
private UserDao userDao; @Before
public void connect(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
userDao = (UserDao) applicationContext.getBean("userDao");
}
@Test
public void testConnect(){
User user = userDao.selectUserById(6);
System.out.println(user);
}
@Test
public void testInsertUser(){
User user = new User("ABKing", "123456", new Date(), 20);
System.out.println(userDao.insertUser(user));
} @Test
public void testDeleteUser(){
System.out.println(userDao.deleteById(6));
}
@Test
public void testUpdateUser(){
User user = new User(36, "ABKing", "666666", new Date(), 20);
System.out.println(userDao.updateUser(user));
}
@Test
public void testQuery(){
List<User> userList = userDao.queryUser();
for(User user : userList){
System.out.println(user);
}
}
}
Spring对Jdbc的封装——JdbcTemplate的使用的更多相关文章
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...
- Spring抽象JDBC,使用JdbcTemplate
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring对jdbc的支持
Spring对jdbc技术提供了很好的支持. 体现在: 1)Spring对c3p连接池的支持很完善: 2)Spring对jdbc提供了JdbcTemplate,来简化jdbc操作: 1.使用步骤 1) ...
- Spring对jdbc支持
4. Spring对jdbc支持 spring对jdbc提供了很好的支持 体现在: 1.Spring对C3P0连接池的支持很完善 2.Spring对jdbc提供了jdbcTemplate来简化jdbc ...
- [原创]java WEB学习笔记109:Spring学习---spring对JDBC的支持:使用 JdbcTemplate 查询数据库,简化 JDBC 模板查询,在 JDBC 模板中使用具名参数两种实现
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- spring boot + druid + 封装JdbcTemplate
本源码内容如下: spring boot项目 用的druid连接池 druid监控页面配置 数据操作用spring jdbctemplate 进一步封装spring jdbctemplate支持用对象 ...
- Spring的JDBC框架
转自: http://www.cnblogs.com/windlaughing/p/3287750.html Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发. Spring主要 ...
- Spring实战6:利用Spring和JDBC访问数据库
主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...
- spring 整合JDBC
使用Spring提供的三个JDBC模板类(JdbcTemplate.NamedParameterJdbcTemplate.SimpleJdbcTemplate)操作数据库 一.JdbcTemplate ...
随机推荐
- SQL中INEXISTS和IN 的区别和联系
SET NOCOUNT ON , SET NOCOUNT OFF当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数).当 SET NOCOUNT 为 ...
- CodeForces-585B(BFS)
链接: https://vjudge.net/problem/CodeForces-585B 题意: The mobile application store has a new game calle ...
- HTTP协议的请求方法
HTTP概念: HTTP是一个基于TCP/IP通信协议来传递数据,包括html文件.图像.结果等,即是一个客户端和服务器端请求和应答的标准 1.http无连接:限制每次连接只处理一个请求,服务端完成客 ...
- javascript(腾讯)
var a={key:"1",value:2}; war b=a; b.value+=a.key, 打印b.value是多少,a.value呢? 答案:都是21.因为javascr ...
- 基于Ubuntu 搭建 WordPress 个人博客 - 开发者实验室 - 腾讯云
1.准备 LAMP 环境 安装 Apache2 在终端输入该命令 ,使用 apt-get 安装 Apache2: sudo apt-get install apache2 -y 安装好后,您可以通过访 ...
- 019:re_path函数详解
re_path使用: 1.re_path和path的作用是一样的,只不过re_path在写url的时候可以使用正则表达式——功能更加强大: 2.使用正则表达式时,推荐使用原生字符串(即:已 r 开头的 ...
- mysql-5.6.45-linux-glibc2.12-x86_64.tar.gz下载安装
一 ,mysql下载 需要注册,可以通过组合url越过注册下载需要的包. 下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.3 ...
- java+web+大文件上传下载
文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...
- JUnit——单元测试
写了个类,要给别人用,会不会有bug?怎么办?测试一下. JUnit可以测试JDBC.Servelet.Struts.Spring.Hibernate等等. 单元测试是开发人员的工作,测试人员负责测试 ...
- 洛谷 P1505 BZOJ 2157 [国家集训队]旅游
bzoj题面 Time limit 10000 ms Memory limit 265216 kB OS Linux 吐槽 又浪费一个下午--区间乘-1之后,最大值和最小值更新有坑.新的最大值是原来最 ...