一、创建数据库与表
1、创建数据库
- 执行命令:
CREATE DATABASE simonshop DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
- 或者利用菜单方式创建数据库 - simonshop
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7754d20061bdd3d87d85dfd6deb06880.png)
2、创建用户表
- 创建用户表 -
t_user
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9dd03083339143ab56463bb28b8051dc.png)
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(20) DEFAULT NULL,
`telephone` varchar(11) DEFAULT NULL,
`register_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`popedom` int(11) DEFAULT NULL COMMENT '0:管理员;1:普通用户',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
3、用户表添加记录
- 给用户表添加4条记录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/af04b16efafde2b1045f8735de8b7269.png)
INSERT INTO `t_user` VALUES ('1', 'admin', '12345', '15734345678', '2016-12-02 08:40:35', '0');
INSERT INTO `t_user` VALUES ('2', '郑晓红', '11111', '13956567889', '2016-12-20 09:51:43', '1');
INSERT INTO `t_user` VALUES ('3', '温志军', '22222', '13956678907', '2016-12-20 09:52:36', '1');
INSERT INTO `t_user` VALUES ('4', '涂文艳', '33333', '15890905678', '2016-12-05 09:52:56', '1');
二、打开Spring项目
- Spring项目 -
SpringDemo
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5f23d8fa833e1cd21403c998a2cc7872.png)
三、添加数据库相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.25</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
四、创建用户实体类
- 在
net.shuai.spring
包里创建day06.bean
子包,然后在子包里面创建User
类
package net.shuai.spring.day06.bean;
import java.util.Date;
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
private int popedom;
public int getId() {
return id;
}
public void setId(int 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 String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
public int getPopedom() {
return popedom;
}
public void setPopedom(int popedom) {
this.popedom = popedom;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
", popedom=" + popedom +
'}';
}
}
五、创建用户数据访问接口
- 在
net.shuai.spring
包里创建day06.dao
子包,在子包里创建UserDao
接口
package net.shuai.spring.day06.dao;
import net.shuai.spring.day06.bean.User;
import java.util.List;
public interface UserDao {
int insert(User user);
int deleteById(int id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
}
六、创建用户数据访问接口实现类
- 在
net.shuai.spring.day06.dao
包里创建impl
子包,然后在子包里创建UserDaoImpl
类
package net.shuai.spring.day06.dao.impl;
import net.shuai.spring.day06.bean.User;
import net.shuai.spring.day06.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public int insert(User user) {
String strSQL = "INSERT INTO t_user (username, password, telephone, " +
"register_time, popedom) VALUES (?, ?, ?, ?, ?)";
return jdbcTemplate.update(strSQL, user.getUsername(), user.getPassword(),
user.getTelephone(), user.getRegisterTime(), user.getPopedom());
}
public int deleteById(int id) {
String strSQL = "DELETE FROM t_user WHERE id = ?";
return jdbcTemplate.update(strSQL, id);
}
public int update(User user) {
String strSQL = "UPDATE t_user SET username = ?, password = ?, " +
"telephone = ?, register_time = ?, popedom = ? WHERE id = ?";
return jdbcTemplate.update(strSQL, user.getUsername(), user.getPassword(),
user.getTelephone(), user.getRegisterTime(), user.getPopedom(), user.getId());
}
public User findById(int id) {
String strSQL = "SELECT * FROM t_user WHERE id = ?";
return jdbcTemplate.queryForObject(strSQL, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int row) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
user.setPopedom(rs.getInt("popedom"));
return user;
}
}, id);
}
public List<User> findAll() {
List<User> users = new ArrayList<>();
String strSQL = "SELECT * FROM t_user";
List<Map<String, Object>> rows = jdbcTemplate.queryForList(strSQL);
for (Map<String, Object> row : rows) {
User user = new User();
user.setId((Integer) row.get("id"));
user.setUsername((String) row.get("username"));
user.setPassword((String) row.get("password"));
user.setTelephone((String) row.get("telephone"));
user.setRegisterTime((Date) row.get("register_time"));
user.setPopedom((Integer) row.get("popedom"));
users.add(user);
}
return users;
}
public User login(String username, String password) {
String strSQL = "SELECT * FROM t_user WHERE username = ? AND password = ?";
return jdbcTemplate.queryForObject(strSQL, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int row) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
user.setPopedom(rs.getInt("popedom"));
return user;
}
}, username, password);
}
}
七、创建用户服务类
- 在
net.shuai.spring.day06
包里创建service
子包,然后在子包里创建UserService
类
package net.shuai.spring.day06.service;
import net.shuai.spring.day06.bean.User;
import net.shuai.spring.day06.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService")
public class UserService {
@Autowired
private UserDao userDao;
public int addUser(User user) {
return userDao.insert(user);
}
public int deleteUserById(int id) {
return userDao.deleteById(id);
}
public int updateUser(User user) {
return userDao.update(user);
}
public User findUserById(int id) {
return userDao.findById(id);
}
public List<User> findAllUsers() {
return userDao.findAll();
}
public User login(String username, String password) {
return userDao.login(username, password);
}
}
八、创建数据库配置属性文件
- 在
resources
目录里创建数据库配置属性文件 - jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/simonshop?useSSL=false
jdbc.username=root
jdbc.password=1234
九、创建Spring配置文件
- 在
resources
里创建Spring
配置文件 - spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="net.shuai.spring.day06"/>
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
十、创建用户服务测试类
- 在
test/java
目录的net.shuai.spring
包里创建day06.service
子包,然后在子包里创建TestUserService
类
package net.shuai.spring.day06.service;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUserService {
private UserService userService;
private ClassPathXmlApplicationContext context;
@Before
public void init() {
context = new ClassPathXmlApplicationContext("spring-config.xml");
userService = (UserService) context.getBean("userService");
}
@After
public void destroy() {
context.close();
}
}
1、测试按编号查询用户方法
- 创建
testFindUserById()
方法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e7e95be2c84abd45862ad22edd94ece5.png)
- 运行测试方法
testFindUserById()
,查看结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cddbd7f6c819ab451805309d973bc0da.png)
- 修改测试方法里的
id
值,再运行,查看结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e769bfb679424eb51a0174c071e3d1bd.png)
- 修改测试方法,捕获异常,再运行,查看结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/af1495e13e2ba05a58d5e41843ed58b6.png)
2、测试查询全部用户方法
- 创建
testFindAllUsers()
方法,并运行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0a20cd17cb386baf63d5b2c5b84a4d59.png)
3、测试添加用户方法
- 创建
testAddUser()
方法,并运行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/934aa70d200e4b6b6226292598204dee.png)
- 查看用户表记录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c5c96797b906dd784981ab7e2d279186.png)
4、测试更新用户方法
- 创建
testUpdateUser()
方法,并运行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3634979f3e78f0aa736b41241f73fdb3.png)
- 查看用户表记录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f0cb28fb44e5acb710963046a14cacbd.png)
5、测试删除用户方法
- 创建
testDeleteUserById()
方法,并运行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/67771a515b1546e18512ce6f1d5acfc5.png)
- 查看用户表记录,哇哈哈记录不见了
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6467517c36804de1e53e6c5449b244ce.png)
6、测试用户登录方法
- 创建
testLogin()
方法,并运行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/03d565d427e6c91a5d3bad5e7592201f.png)
- 修改
testLogin()
方法,并运行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0f11ea5bc223c262e7117e1e097bc44b.png)