准备阶段:
pom.xml
4.0.0
com.henu
springJDBCplus
1.0-SNAPSHOT
org.springframework
spring-context
4.3.20.RELEASE
org.springframework
spring-core
4.3.20.RELEASE
org.springframework
spring-beans
4.3.20.RELEASE
org.springframework
spring-expression
4.3.20.RELEASE
org.springframework
spring-aop
4.3.20.RELEASE
org.springframework
spring-aspects
4.3.20.RELEASE
org.aspectj
aspectjrt
1.9.1
org.aspectj
aspectjweaver
1.9.2
commons-logging
commons-logging
1.2
mysql
mysql-connector-java
5.1.38
com.alibaba
druid
1.1.10
org.springframework
spring-jdbc
5.1.3.RELEASE
org.springframework
spring-tx
5.1.3.RELEASE
org.projectlombok
lombok
1.16.20
provided
junit
junit
4.12
数据库:
项目架构:
一、实体类User
package com.henu.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author George
* @description
**/
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {
private int id;
private String name;
private String pwd;
}
二、dao层
package com.henu.dao;
import com.henu.bean.User;
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 George
* @description
**/
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
public int insertUser(User user) {
String sql = "insert into user values(0,?,?)";
return jdbcTemplate.update(sql,user.getName(),user.getPwd());
}
public int deleteUser(int id) {
String sql = "delete from user where id = ?";
return jdbcTemplate.update(sql,id);
}
public int updateUser(User user) {
String sql = "update user set name=?,pwd=? where id=?";
return jdbcTemplate.update(sql,user.getName(),user.getPwd(),user.getId());
}
public User queryUserById(int id) {
String sql = "select * from user where id = ?";
//使用内部类
class UserMapper implements RowMapper{
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPwd(resultSet.getString("pwd"));
return user;
}
}
//这里需要注意的是使用的是queryForObject()方法。如果使用query会报出ClassCastException
return (User) jdbcTemplate.queryForObject(sql,new UserMapper(),id);
}
public List queryAll() {
String sql = "select * from user";
//使用匿名内部类
return jdbcTemplate.query(sql, new RowMapper() {
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPwd(resultSet.getString("pwd"));
return user;
}
});
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
三、service层
package com.henu.service;
import com.henu.bean.User;
import com.henu.dao.UserDao;
import java.util.List;
/**
* @author George
* @description
**/
public class UserServiceImpl implements UserService {
private UserDao userDao;
public int insertUser(User user) {
return userDao.insertUser(user);
}
public int deleteUser(int id) {
return userDao.deleteUser(id);
}
public int updateUser(User user) {
return userDao.updateUser(user);
}
public User queryUserById(int id) {
return userDao.queryUserById(id);
}
public List queryAll() {
return userDao.queryAll();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
四、resources
db.properties
#db config
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ooxx
jdbc.username=root
jdbc.password=123456
initialSize=5
minIdle=2
maxActive=10
maxWait=10000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
testWhileIdle=true
testOnBorrow=true
testOnReturn=false
poolPreparedStatements=true
maxPoolPreparedStatementPerConnectionSize=20
defaultAutoCommit=true
validationQuery=select 1
applicationContext_db.xml
五、测试类
package com.henu;
import com.henu.bean.User;
import com.henu.service.UserService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @author George
* @description
* 测试类中的方法,无返回值,无参数
**/
public class JdbcTest {
private ClassPathXmlApplicationContext context = null;
private UserService userService;
@Before
public void init(){
context = new ClassPathXmlApplicationContext("applicationContext_db.xml");
userService = (UserService) context.getBean(UserService.class);
}
@Test
public void insert(){
User user = new User(0,"乔治大哥","123456");
userService.insertUser(user);
}
@Test
public void deleteUser(){
userService.deleteUser(1);
}
@Test
public void updateUser(){
User user = new User(1,"康康","56345");
userService.updateUser(user);
System.out.println("success");
}
@Test
public void queryUserById(){
System.out.println(userService.queryUserById(2));
}
@Test
public void queryAll(){
List users = userService.queryAll();
for (User user : users) {
System.out.println(user);
}
}
@After
public void destory(){
context.close();
}
}