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);
}
}
}
上一篇:iOS中实现多线程的技术方案


下一篇:java:Oracle(Jdbc的封装)和HTML(登录,注册,个人信息界面)