一、 构建数据库字段
CREATE TABLE `user` (
`id` int(100) NOT NULL AUTO_INCREMENT COMMENT 'id',
`uname` varchar(100) DEFAULT NULL COMMENT '姓名',
`uage` varchar(8) CHARACTER SET latin1 DEFAULT NULL COMMENT '年龄',
`sex` varchar(8) CHARACTER SET latin1 DEFAULT NULL COMMENT '性别',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
二、 创建数据库配置类及配置文件
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
//用于指定当前类是一个spring 配置类,当创建容器时会从该类上加载注解
@Configuration
//用于指定 spring 在初始化容器时要扫描的包
@ComponentScan("com.feitu.spring")
//使用此注解指定 properties 配置文件的位置,如果是在类路径下,需要写上 classpath
@PropertySource("classpath:JdbcConfiguration.properties")
@Import(JdbcConfig.class)
public class SpringConfiguration {
}
===============================================================================
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driverClassname;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassname);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://<地址>:<端口号>/<数据库名称>?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
三、 新建实体类
import java.io.Serializable;
public class User implements Serializable {
//id
private int id;
//姓名
private String uname;
//年龄
private int uage;
//性别
private int sex;
public String getUname() {
return uname;
}
public int getUage() {
return uage;
}
public int getSex() {
return sex;
}
public void setUname(String uname) {
this.uname = uname;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setUage(int uage) {
this.uage = uage;
}
public void setSex(int sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", uname='" + uname + '\'' +
", uage=" + uage +
", sex=" + sex +
'}';
}
}
四、 新建持久层
1. 接口
import com.feitu.spring.pojo.User;
import java.util.List;
public interface UserDao {
List<User> findAll(); //查询所有信息
int insert(User user); //新增人员信息
int delete(int id); //删除人员信息
int update(User user); //更新人员信息
User findOne(int id); //查询一人信息
}
2. 实现类
import com.feitu.spring.pojo.User;
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 java.util.List;
@Repository("userDaoImp")
public class UserDaoImp implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<User> findAll() {
try {
System.out.println("查询全部");
return jdbcTemplate.query("select * from USER", new BeanPropertyRowMapper<User>(User.class));
}catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int insert(User user) {
try {
System.out.println("增加" + user.getUname());
return jdbcTemplate.update("insert into USER (uname,uage,sex) value (?,?,?)", user.getUname(), user.getUage(), user.getSex());
}catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int delete(int id) {
try {
System.out.println("删除" + id);
return jdbcTemplate.update("delete from USER where id = ?", id);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int update(User user) {
try {
System.out.println("更新" + user.getUname());
return jdbcTemplate.update("update user set uname = ?,uage = ?,sex = ? where id = ?",user.getUname(),user.getUage(),user.getSex(),user.getId());
}catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public User findOne(int id) {
System.out.println("查找" + id);
return jdbcTemplate.queryForObject("select * from USER where id = ? ",new BeanPropertyRowMapper<User>(User.class),id);
}
}
五、 新建业务层
1. 接口
import com.feitu.spring.pojo.User;
import java.util.List;
public interface UserDao {
List<User> findAll(); //查询所有信息
int insert(User user); //新增人员信息
int delete(int id); //删除人员信息
int update(User user); //更新人员信息
User findOne(int id); //查询一人信息
}
2. 实现类
import com.feitu.spring.dao.UserDao;
import com.feitu.spring.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userServiceImp")
public class UserServiceImp implements UserService{
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
List<User> list = userDao.findAll();
for (User user: list) {
System.out.println(user);
}
return list;
}
@Override
public int insert(User user) {
int x = userDao.insert(user);
if(x==1){
System.out.println("执行成功");
}else{
System.out.println("执行失败");
}
return x;
}
@Override
public int delete(int id) {
return userDao.delete(id);
}
@Override
public int update(User user) {
return userDao.update(user);
}
@Override
public User findOne(int id) {
User user = userDao.findOne(id);
System.out.println(user);
return user;
}
}
六、测试类
import com.feitu.spring.config.SpringConfiguration;
import com.feitu.spring.pojo.User;
import com.feitu.spring.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class CRUDTest {
@Autowired
private UserService userService;
@Test
public void insert(){
User user = new User();
user.setUname("洪涛");
user.setUage(18);
user.setSex(1);
userService.insert(user);
}
@Test
public void delete(){
int id = 1;
userService.delete(id);
}
@Test
public void update(){
User user = new User();
user.setUname("鸿涛");
user.setUage(20);
user.setSex(0);
user.setId(1);
userService.update(user);
}
@Test
public void findAll(){
userService.findAll();
}
@Test
public void findOne(){
int id = 1;
userService.findOne(id);
}
}
七、涉及Jar包
1)aopalliance 2)aspectjrt 3)aspectjweaver 4)commons-logging
5)druid 6)hamcrest-core 7)junit 8)mysql-connector
9)spring-aop 10)spring-aspects 11)spring-beans 12)spring-context
13)spring-context-suppor 14)spring-core 15)spring-expression
16)spring-instrument 17)spring-jdbc 18)spring-jms
19)spring-messaging 20)spring-orm 21)spring-oxm
22)spring-test 23)spring-tx