对于ssm框架,此处不再做多的说明,该项目运用了:IDEA+Maven+Mysql(8.0+)
因为我在写这个项目的时候,我做了多个不同的控制器获取前端页面返回数据的方式,所以这里说明一下,返回的方式:
1、直接把表单的参数写在Controller相应的方法的形参中(String UserName);
2、通过HttpServletRequest接收,post方式和get方式都可以,
String username=request.getParameter(“username”);
3、通过一个bean来接收,post方式和get方式都可以(User user);
4、使用@ModelAttribute注解获取POST请求的FORM表单数据
(@ModelAttribute(“user”) User user);
接下来进入项目:
一、项目结构
Controller层(控制器)、Dao(数据库访问层)、Pojo(User对象)、Service(业务层)
、resources (配置文件)
二、通过Maven仓库加载相关jar包
junit
junit
4.12
mysql
mysql-connector-java
8.0.27
com.mchange
c3p0
0.9.5.2
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2
org.mybatis
mybatis
3.5.2
org.mybatis
mybatis-spring
2.0.2
org.springframework
spring-webmvc
5.1.9.RELEASE
org.springframework
spring-jdbc
5.1.9.RELEASE
org.projectlombok
lombok
1.18.20
三、编译实体类(Pojo)和数据库层(Dao)
在Pojo包中创建User类,注意,此处的setter和get,还有构造方法都通过jar包lombok里面的实现方法@Data、@AllArgsConstructor、@NoArgsConstructor省略了
package Pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int Id;
private String UserName;
private String PassWord;
}
在Dao包中创建UserDao接口和UserDao.xml配置文件,在UserDao.xml完成对数据库的操作
package Dao;
import Pojo.User;
import org.apache.ibatis.annotations.Param;
public interface UserDao {
//登录
User Login(@Param("UserName") String UserName,@Param("PassWord") String PassWord);
//注册
int Register(User user);
}
insert into user.admin(UserName, PassWord) value (#{UserName},#{PassWord})
配置Mybatis相关配置文件和Dao层
创建database.properties,配置相关的连接数据库内容
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/user?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
jdbc.username=root
jdbc.password=011124
创建spring-dao.xml,连接数据库
创建mybatis-config.xml
四、编译Service(业务层)
在Service包下,创建UserService接口,和UserServiceImpl实现类
package Service;
import Pojo.User;
public interface UserService {
//登录
User Login(String UserName,String PassWord);
//注册
int Register(User user);
}
package Service;
import Dao.UserDao;
import Pojo.User;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public User Login(String UserName, String PassWord) {
User user = userDao.Login(UserName, PassWord);
if (user != null && user.getUserName().equals(UserName) && user.getPassWord().equals(PassWord)) {
return user;
}
return null;
}
@Override
public int Register(User user) {
return userDao.Register(user);
}
}
创建spring-service.xml配置Service
五、配置web.xml和SpringMVC
web.xml文件配置
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
DispatcherServlet
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
15
创建spring-mvc.xml配置SpringMVC,记得在WEB-INF下创建jsp文件
六、Controller(控制层)
package Controller;
import Pojo.User;
import Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/User")
public class UserController {
@Autowired
@Qualifier("UserServiceImpl")
private UserService userService;
@RequestMapping("/toLogin")
public String toLogin() {
return "Login";
}
@RequestMapping("/Login")
public String Login(User user) {
user = userService.Login(user.getUserName(), user.getPassWord());
if (user != null) {
return "success";
}
return "redirect:/User/toLogin";
}
@RequestMapping("/Login1")
public String Login(String UserName, String PassWord) {
User user = userService.Login(UserName, PassWord);
if (user != null) {
return "success";
}
return "redirect:/User/toLogin";
}
@RequestMapping("/Login2")
public String Login(HttpServletRequest request) {
String UserName = request.getParameter("UserName");
String PassWord = request.getParameter("PassWord");
User user = userService.Login(UserName, PassWord);
if (user != null) {
return "success";
}
return "redirect:/User/toLogin";
}
@RequestMapping("/toRegister")
public String toRegister(User user){
return "register";
}
@RequestMapping("/Register")
public String Register(User user){
System.out.println("用户名"+user.getUserName()+"密码"+user.getPassWord());
userService.Register(user);
return "redirect:/User/toLogin";
}
}
七、前端页面
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
请先登录
注册
Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
用户注册
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
登录成功!
运行截图