Java方向(SSM+SpringBoot)项目实训
Day3(2020.7.22)
事先数据库操作:
1.创建ssm_learn数据库,并创建如下表结构的user表;
2.向user表中插入数据以便测试;
SSM项目操作:
1.创建如下结构目录包结构;
并导入所需的页面及羡慕配置文件;
2.开始新建所需的类与代码编写;
package com.roger0123.bean;
public class User {
private int id;
private String username;
private String password;
public User(){
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
db.properties:
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssm_learn?useSSl=false
jdbc.username = root
jdbc.password = ******
2.3 resources下导入applicationContext.xml和spring-mvc.xml配置文件,并依据自己创建的项目改错修正;
2.4 resources下创建mapper包然后其中新建一个UserMapper.xml文件;
UserMapper.xml:
<?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.roger0123.dao.UserDao" >
<select id="findByUserName" parameterType="String" resultType="user">
select * from ssm_user where username=#{
username}
</select>
</mapper>
package com.roger0123.dao;
import com.roger0123.bean.User;
public interface UserDao {
User findByUserName(String username);
}
UserService:
package com.roger0123.service;
public interface UserService {
int login(String username,String password);
}
2.6 service下创建impl包并在其中创建UserServiceImpl类;
UserServiceImpl:
package com.roger0123.service.impl;
import com.roger0123.bean.User;
import com.roger0123.dao.UserDao;
import com.roger0123.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public int login(String username,String password){
User user = userDao.findByUserName(username);
if (user!=null && user.getPassword().equals(password)){
return user.getId();
}else{
System.out.println("无该用户或密码不匹配!");
}
return -1;
}
}
<?xml version="1.0" encoding="UTF-8"?>
-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 配置加载类路径的配置文件 -->
contextConfigLocation
classpath*:applicationContext.xml
<!-- 配置监听器 -->
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
<!-- 解决中文乱码过滤器 -->
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
<!-- 前端控制器(加载classpath:spring-mvc.xml 服务器启动创建servlet) -->
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
*.do
-app>
2.8 controller下创建UserController类,实现登录成功并跳转页面;
UserController:
package com.roger0123.controller;
import com.roger0123.bean.User;
import com.roger0123.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
int id =userService.login(user.getUsername(),user.getPassword());
ModelAndView mv = new ModelAndView();
if (id!=-1){
mv.setViewName("main");
}else {
mv.setViewName("../failer");//不同级页面,返回上一级
}
return mv;
}
}
运行程序进行测试:
Test:
1.登录页面打开成功:
2.输入存在的用户名以及匹配的密码:
登录成功页面跳转成功;
3.输入存在的用户名,不匹配的密码:
登录失败页面跳转成功;
1.修改UserDao接口;
UserDao中添加:
List<User> findAll();
2.修改UserMapper.xml文件;
UserMapper中添加:
<select id="findAll" resultType="user">
select * from ssm_user
</select>
3.修改UserService接口;
UserService中添加:
List<User> findALl();
4.修改UserServiceImpl类;
UserServiceImpl中添加:
@Override
public List<User> findAll(){
return userDao.findAll();
}
5.修改UserController类;
UserController中添加:
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<User> users = userService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("users",users);
mv.setViewName("user-list");
return mv;
}
Test:
1.登录成功后,点击侧边栏中系统管理中的用户管理;
2.用户管理界面跳转成功;
1.修改UserDao接口;
UserDao中添加:
void add(User user);
2.修改UserMapper.xml文件;
UserMapper中添加:
<insert id="add" parameterType="user" >
insert into ssm_user (username,password) values (#{
username},#{
password})
</insert>
3.修改UserService接口;
UserService中添加:
void add(User user);
4.修改UserServiceImpl类;
UserServiceImpl中添加:
@Override
public void add(User user){
userDao.add(user);
}
5.修改UserController类;
UserController中添加:
@RequestMapping("/add.do")
public String add(User user){
userService.add(user);
return "redirect:findAll.do";
}
Test:
1.点击用户管理界面的新建按钮跳转至新建用户信息界面;
2.进入新建用户信息界面,输入需要新建的用户名及密码,点击保存;
3.保存成功,用户管理界面信息增加成功;
数据库增加用户信息成功;
1.修改UserDao接口;
UserDao中添加:
void deleteById(int id);
2.修改UserMapper.xml文件;
UserMapper中添加:
<delete id="deleteById" parameterType="int">
delete from ssm_user where id=#{
id}
</delete>
3.修改UserService接口;
UserService中添加:
void deleteById(int id);
4.修改UserServiceImpl类;
UserServiceImpl中添加:
@Override
public void deleteById(int id){
userDao.deleteById(id);
}
5.修改UserController类;
UserController中添加:
@RequestMapping("/deleteById.do")
public String deleteById(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
Test:
1.点击用户管理界面的中的操作中的删除按钮以删除用户信息;
2.删除操作成功,用户管理界面信息删除成功;
1.修改UserDao接口;
UserDao中添加:
User selectUserById(int id);
void update(User user);
2.修改UserMapper.xml文件;
UserMapper中添加:
<select id="selectUserById" parameterType="int" resultType="user">
select * from ssm_user where id=#{
id}
</select>
<update id="update" parameterType="user">
update ssm_user set username=#{
username},password=#{
password} where id=#{
id}
</update>
3.修改UserService接口;
UserService中添加:
User selectUserById(int id);
void update(User user);
4.修改UserServiceImpl类;
UserServiceImpl中添加:
@Override
public User selectUserById(int id) {
return userDao.selectUserById(id);
}
@Override
public void update(User user){
userDao.update(user);
}
5.修改UserController类;
UserController中添加:
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id){
User user = userService.selectUserById(id);
ModelAndView mv = new ModelAndView();
mv.setViewName("user-update");
mv.addObject("user",user);
return mv;
}
@RequestMapping("/update.do")
public String update(User user){
userService.update(user);
return "redirect:findAll.do";
}
6.修改user-update.jsp;
6.1将修改用户信息时候的输入密码的type由password改为text:即修改密码时密码可见;
6.2设置user.id为只读类型,即修改用户信息时不可修改用户Id;
Test:
1.点击用户管理界面的中的操作中的更新按钮以更新用户信息;