文章目录
- 一、创建数据库与表
-
- 1、创建数据库
- 2、创建用户表
- 3、用户表添加记录
- 二、打开Spring项目
- 三、添加数据库相关依赖
- 四、创建用户实体类
- 五、创建用户数据访问接口
- 六、创建用户数据访问接口实现类
- 七、创建用户服务类
- 八、创建数据库配置属性文件
- 九、创建Spring配置文件
- 十、创建用户服务测试类
-
- 1、测试按编号查询用户方法
- 2、测试查询全部用户方法
- 3、测试添加用户方法
- 4、测试更新用户方法
- 5、测试删除用户方法
- 6、测试用户登录方法
一、创建数据库与表
1、创建数据库
- 执行命令:
CREATE DATABASE simonshop DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
- 或者利用菜单方式创建数据库 - simonshop
2、创建用户表
- 创建用户表 -
t_user
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条记录
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
三、添加数据库相关依赖
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.3.25version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.49version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.16version>
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
<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()
方法
- 运行测试方法
testFindUserById()
,查看结果
- 修改测试方法里的
id
值,再运行,查看结果
- 修改测试方法,捕获异常,再运行,查看结果
2、测试查询全部用户方法
- 创建
testFindAllUsers()
方法,并运行
3、测试添加用户方法
- 创建
testAddUser()
方法,并运行
- 查看用户表记录
4、测试更新用户方法
- 创建
testUpdateUser()
方法,并运行
- 查看用户表记录
5、测试删除用户方法
- 创建
testDeleteUserById()
方法,并运行
- 查看用户表记录,哇哈哈记录不见了
6、测试用户登录方法
- 创建
testLogin()
方法,并运行
- 修改
testLogin()
方法,并运行