链接:https://pan.baidu.com/s/15luDElW4oeEaP0nvEQ_40w
提取码:i2r1
JdbcTemplate是Spring对JDBC的封装,目的是使JDBC更加易于使用。JdbcTemplate是Spring的一部分。
接下来,使用JdbcTemplate进行增删改查(CRUD)的操作
写一个pojo类(不是重点)
1 package top.bigking.pojo; 2 3 import java.util.Date; 4 5 public class User { 6 private Integer id; 7 private String username; 8 private String password; 9 private Date birthday; 10 private Integer age; 11 12 public User() { 13 } 14 15 public User(Integer id, String username, String password, Date birthday, Integer age) { 16 this.id = id; 17 this.username = username; 18 this.password = password; 19 this.birthday = birthday; 20 this.age = age; 21 } 22 23 public User(String username, String password, Date birthday, Integer age) { 24 this.username = username; 25 this.password = password; 26 this.birthday = birthday; 27 this.age = age; 28 } 29 30 public Integer getId() { 31 return id; 32 } 33 34 public void setId(Integer id) { 35 this.id = id; 36 } 37 38 public String getUsername() { 39 return username; 40 } 41 42 public void setUsername(String username) { 43 this.username = username; 44 } 45 46 public String getPassword() { 47 return password; 48 } 49 50 public void setPassword(String password) { 51 this.password = password; 52 } 53 54 public Date getBirthday() { 55 return birthday; 56 } 57 58 public void setBirthday(Date birthday) { 59 this.birthday = birthday; 60 } 61 62 public Integer getAge() { 63 return age; 64 } 65 66 public void setAge(Integer age) { 67 this.age = age; 68 } 69 70 @Override 71 public String toString() { 72 return "User{" + 73 "id=" + id + 74 ", username='" + username + '\'' + 75 ", password='" + password + '\'' + 76 ", birthday=" + birthday + 77 ", age=" + age + 78 '}'; 79 } 80 }
再写一个接口
1 package top.bigking.dao; 2 3 import top.bigking.pojo.User; 4 5 import java.util.List; 6 7 public interface UserDao { 8 public int insertUser(User user); 9 public int deleteById(Integer id); 10 public int updateUser(User user); 11 public List<User> queryUser(); 12 public User selectUserById(Integer id); 13 }
接下来对接口进行实现:
1 package top.bigking.dao.impl; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.jdbc.core.BeanPropertyRowMapper; 5 import org.springframework.jdbc.core.JdbcTemplate; 6 import org.springframework.stereotype.Repository; 7 import top.bigking.dao.UserDao; 8 import top.bigking.pojo.User; 9 10 import java.util.List; 11 12 13 @Repository("userDao") 14 public class UserDaoImpl implements UserDao { 15 @Autowired 16 private JdbcTemplate jdbcTemplate; 17 18 @Override 19 public int insertUser(User user) { 20 String sql = "insert into t_user(user_name, password, birthday, age) values(?, ?, ?, ?)"; 21 return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getBirthday(), user.getAge()); 22 } 23 24 @Override 25 public int deleteById(Integer id) { 26 String sql = "delete from t_user where id = ?"; 27 return jdbcTemplate.update(sql, id); 28 } 29 30 @Override 31 public int updateUser(User user) { 32 String sql = "update t_user set user_name = ?, password = ?, birthday = ?, age = ?"; 33 return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getBirthday(), user.getAge()); 34 } 35 36 @Override 37 public List<User> queryUser() { 38 String sql = "select * from t_user"; 39 return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)); 40 } 41 42 @Override 43 public User selectUserById(Integer id) { 44 String sql = "select * from t_user where id = ?"; 45 return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id); 46 } 47 }
接下来使用单元测试一个一个测试:
1 package top.bigking.test; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 import top.bigking.dao.UserDao; 8 import top.bigking.pojo.User; 9 10 import java.util.Date; 11 import java.util.List; 12 13 public class TestJdbc { 14 private UserDao userDao; 15 16 @Before 17 public void connect(){ 18 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml"); 19 userDao = (UserDao) applicationContext.getBean("userDao"); 20 } 21 @Test 22 public void testConnect(){ 23 User user = userDao.selectUserById(6); 24 System.out.println(user); 25 } 26 @Test 27 public void testInsertUser(){ 28 User user = new User("ABKing", "123456", new Date(), 20); 29 System.out.println(userDao.insertUser(user)); 30 } 31 32 @Test 33 public void testDeleteUser(){ 34 System.out.println(userDao.deleteById(6)); 35 } 36 @Test 37 public void testUpdateUser(){ 38 User user = new User(36, "ABKing", "666666", new Date(), 20); 39 System.out.println(userDao.updateUser(user)); 40 } 41 @Test 42 public void testQuery(){ 43 List<User> userList = userDao.queryUser(); 44 for(User user : userList){ 45 System.out.println(user); 46 } 47 } 48 }