SSM 电影后台管理项目
概述
通过对数据库中一张表的CRUD,将相应的操作结果渲染到页面上。
笔者通过这篇博客还原了项目(当然有一些隐藏的坑),然后将该项目上传到了Github、Gitee,在末尾会附上有源码地址,读者可参考。
电影后台管理系统的管理员在工作中需要查阅和管理如下信息:后台管理的管理员、电影信息、新闻信息以及类型信息。如下图:
项目展示
准备
- 环境:
- IDEA
- MySQL 5.1.47
- Tomcat 9
- Maven 3.6
- 要求:
- 掌握MySQL数据库
- 掌握Spring
- 掌握MyBatis
- 掌握SpringMVC
- 掌握简单的前端知识
实现
1.创建好项目架构
2.SSM架构
这部分如果感兴趣,想知道具体操作,可以查看文章 《SSM整合》
3.POJO层
因为设计的数据库中有4个表,分别是:user
、type
、news
、film
所以对应创建四个实体类,这里部分举例
public class user {
private Integer id;
private String username;
private String paw;
private Integer tele;
private String email;
// 有参\有参方法
// Get\Set方法
// toString()
}
public class news {
@NotNull
private Integer ISDN;
@NotNull
private String title;
@NotNull
private String author;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Past
private Date date;
@NotNull
private String description;
// 有参\有参方法
// Get\Set方法
// toString()
}
4.Dao层
public interface UserMapper {
/**
* 获取用户列表
* @return
*/
public List<user> getUserList();
/**
* id查用户
* @return
*/
public user getUserById(int id);
/**
* 添加用户
* @param user
* @return
*/
public int insertUser(user user);
/**
* 修改用户个人信息
* @return
*/
public int upUser(user user);
/**
* 修改密码
* @param user
* @return
*/
public int uppaw(user user);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zc.Dao.UserMapper">
<select id="getUserList" resultType="com.zc.pojo.user">
select * from film.user
</select>
<select id="getUserById" resultType="com.zc.pojo.user">
select * from film.user where id=#{id}
</select>
<insert id="insertUser" parameterType="com.zc.pojo.user" >
insert into film.user (username,paw,tele,email) values (#{username},#{paw},#{tele},#{email})
</insert>
<update id="upUser" parameterType="com.zc.pojo.user">
update film.user set username = #{username},tele = #{tele},email = #{email} where id = #{id}
</update>
<update id="uppaw" parameterType="com.zc.pojo.user">
update film.user set paw=#{paw} where id = #{id}
</update>
</mapper>
5.Service层
UserService
public interface UserService {
/**
* 得到全部User数据
* @return
*/
public List<user> getUserList();
/**
* 插入一个User
* @param user
* @return
*/
public int insertUser(user user);
/**
* 更新用户信息
* @param user
* @return
*/
public int upUser(user user);
/**
* 更新用户密码
* @param user
* @return
*/
public int uppaw(user user);
/**
* 通过id查找用户
* @param id
* @return
*/
public user getUserById(int id);
}
@Service
public class UserServiceimpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<user> getUserList() {
return userMapper.getUserList();
}
@Override
public int insertUser(user user) {
return userMapper.insertUser(user);
}
@Override
public int upUser(user user) {
return userMapper.upUser(user);
}
@Override
public int uppaw(user user) {
return userMapper.uppaw(user);
}
@Override
public user getUserById(int id) {
return userMapper.getUserById(id);
}
}
6.Controller层
@Controller
public class UserController {
@Autowired
private HttpServletRequest request;
@Autowired
@Qualifier("userServiceimpl")
private UserService userService;
/**
* 登录
* @param username
* @param password
* @param code
* @return
*/
@RequestMapping("/Login")
public String getUserList(String username, String password, String code){
List<user> userList = userService.getUserList();
for (user user : userList) {
System.out.println(user);
if(user.getUsername().equals(username)&&user.getPaw().equals(password)){
HttpSession session = request.getSession();
Object attribute = session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
if(code.equals(attribute)){
session.setAttribute("user",user);
return "main.jsp";
}else {
request.setAttribute("mgs", "验证码错误");
return "index.jsp";
}
}
}
// System.out.println(user.getUsername()+"-----"+user.getPaw());
request.setAttribute("mgs", "用户名或密码错误");
return "index.jsp";
}
/**
* 注销
* @return
*/
@RequestMapping("/exit")
public String exit(){
request.getSession().removeAttribute("user");
return "index.jsp";
}
/**
* 注册
* @param user
* @return
*/
@RequestMapping("/register")
public String insertUser(user user){
List<user> userList = userService.getUserList();
for (user user1 : userList) {
if (user1.getUsername().equals(user.getUsername())){
request.setAttribute("mgs1", "已经存在该用户");
return "index.jsp";
}else {
System.out.println(user);
userService.insertUser(user);
return "index.jsp";
}
}
return "index.jsp";
}
/**
* 修改用户信息
* @param user
* @return
*/
@RequestMapping("/upUser")
public String upUser(user user){
int i = userService.upUser(user);
System.out.println(user+"-----"+i);
if (i>0){
user user1 = userService.getUserById(user.getId());
request.getSession().setAttribute("user",user1);
request.setAttribute("mgs4","修改成功");
return "person/person_info.jsp";
}else{
request.setAttribute("mgs4","修改失败");
return "person/person_info.jsp";
}
}
/**
* 修改密码
* @param user
* @return
*/
@RequestMapping("/uppaw")
public String uppaw(user user, String paw1){
user userById = userService.getUserById(user.getId());
System.out.println(user+"----------"+paw1);
if(userById.getPaw().equals(paw1)){
userService.uppaw(user);
user user1 = userService.getUserById(user.getId());
request.getSession().setAttribute("user",user1);
request.setAttribute("mgs3","修改密码成功");
return "person/updatepwd.jsp";
}else{
request.setAttribute("mgs3","输入原始密码不对");
return "person/updatepwd.jsp";
}
}
}