创建数据库
create table user(
uid int primary key auto_increment,
name varchar(15) not null unique,
password varchar(15) not null,
regtime datetime not null
) charset utf8;
<dependencies>
<!-- 基础依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- c3p0数据源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- springMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- servlet api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置分发器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatcher-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<!-- spring -->
<!-- 配置扫描 -->
<context:component-scan base-package="top.it1002.SSM.controller,top.it1002.SSM.service,top.it1002.SSM.dao"/>
<!-- springMVC -->
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- mybatis -->
<!-- c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="maxPoolSize" value="10" />
<property name="minPoolSize" value="2" />
<property name="initialPoolSize" value="3" />
</bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-conf.xml" />
</bean>
</beans>
mybatis-conf.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="top.it1002.SSM.domain.User" alias="_user"/>
</typeAliases>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
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="user">
<!-- 查 -->
<select id="select" parameterType="int" resultType="_user">
select * from user where uid = #{uid}
</select>
<select id="selectAll" resultType="_user">
select * from user
</select>
<select id="loginSelect" parameterType="_user" resultType="_user">
select * from user where name = #{name} and password = #{password}
</select>
<!-- 改 -->
<update id="update" parameterType="_user">
update user
<set>
<if test="password != null">password = #{password},</if>
<if test="name != null">name = #{name}</if>
<if test="regtime != null">regtime = #{regtime}</if>
</set>
where uid = #{uid}
</update>
<!-- 增 -->
<insert id="insert">
insert user(name, password, regtime) values(#{name}, #{password}, #{regtime})
</insert>
<!-- 删 -->
<delete id="delete" parameterType="int">
delete from user where uid = #{uid}
</delete>
<delete id="deleteAll">
delete from user;
</delete>
</mapper>
package top.it1002.SSM.domain;
import org.springframework.stereotype.Repository;
@Repository
public class User {
private int uid;
private String name;
private String password;
private String regtime;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRegtime() {
return regtime;
}
public void setRegtime(String regtime) {
this.regtime = regtime;
}
}
package top.it1002.SSM.dao;
import java.util.List;
public interface BaseDao<T> {
public int insert(T t);
public void deleteOne(int id);
public void update(T t);
public T selectOne(int id);
public List<T> selectAll();
}
package top.it1002.SSM.dao;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import top.it1002.SSM.domain.User;
import javax.annotation.Resource;
import java.util.List;
@Repository
public class UserDao extends SqlSessionDaoSupport implements BaseDao<User>{
@Resource(name = "sqlSessionFactoryBean")
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
public int insert(User user) {
return getSqlSession().insert("user.insert", user);
}
public void deleteOne(int uid) {
getSqlSession().delete("user.delete", uid);
}
public void update(User user) {
getSqlSession().update("user.update", user);
}
public User selectOne(int id) {
return getSqlSession().selectOne("user.select", id);
}
public List<User> selectAll() {
return getSqlSession().selectList("user.selectAll");
}
public User loginSelect(User user){
return getSqlSession().selectOne("user.loginSelect", user);
}
}
package top.it1002.SSM.service;
import java.util.List;
public interface BaseService<T> {
public int insert(T t);
public void delete(int id);
public void update(T t);
public T select(int id);
public List<T> selectAll();
}
package top.it1002.SSM.service;
import top.it1002.SSM.domain.User;
public interface UserService extends BaseService<User>{
public User loginCheck(User user);
}
package top.it1002.SSM.service.impl;
import org.springframework.stereotype.Service;
import top.it1002.SSM.dao.UserDao;
import top.it1002.SSM.domain.User;
import top.it1002.SSM.service.UserService;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource(name = "userDao")
private UserDao userDao;
public int insert(User user) {
return userDao.insert(user);
}
public void delete(int id) {
userDao.deleteOne(id);
}
public void update(User user) {
userDao.update(user);
}
public User select(int id) {
return userDao.selectOne(id);
}
public List<User> selectAll() {
return userDao.selectAll();
}
public User loginCheck(User user) {
return userDao.loginSelect(user);
}
}
package top.it1002.SSM.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import top.it1002.SSM.domain.User;
import top.it1002.SSM.service.impl.UserServiceImpl;
import top.it1002.SSM.util.SSMUtil;
import javax.servlet.http.HttpSession;
import java.util.Date;
@Controller
public class IndexController {
/**
* 主页展示
* @return
*/
@RequestMapping("/index")
public String index(){
return "redirect:/index.jsp";
}
/**
* 前往注册界面
* @return
*/
@RequestMapping("/toReg")
public String toReg(){
return "redirect:/reg.jsp";
}
/**
* 登录功能
* @param user
* @param sess
* @return
*/
@RequestMapping("/doLogin")
public String doLogin(User user, HttpSession sess){
String name = user.getName();
String password = user.getPassword();
if(!name.equals("") && !password.equals("")){
UserServiceImpl userService = (UserServiceImpl)SSMUtil.getAC().getBean("userServiceImpl");
User user1 = userService.loginCheck(user);
if(user1 != null){
sess.setAttribute("user", user1);
return "redirect:/admin.jsp";
}else{
sess.setAttribute("warn", "账号或者密码错误!");
return "redirect:/index.jsp";
}
}
sess.setAttribute("warn", "账号或者密码为空!请输入完整!");
return "redirect:/index";
}
/**
* 注册功能
* @param user
* @param sess
* @return
*/
@RequestMapping("/doReg")
public String doReg(User user, HttpSession sess){
user.setRegtime(new Date().toLocaleString());
UserServiceImpl userService = (UserServiceImpl)SSMUtil.getAC().getBean("userServiceImpl");
try{
userService.insert(user);
sess.setAttribute("user", user);
return "redirect:/admin.jsp";
}catch (Exception e){
sess.setAttribute("warn", "账号" + user.getName() + "已存在!请选择其他用户名!");
return "redirect:/reg.jsp";
}
}
/**
* 退出登录
* @param sess
* @return
*/
@RequestMapping("/doLogout")
public String doLogout(HttpSession sess){
try {
sess.removeAttribute("user");
}catch (Exception e){
}
try {
sess.removeAttribute("warn");
}catch (Exception e){
}
return "redirect:/index.jsp";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta content="text/html" charset="utf-8">
<title>登录测试</title>
</head>
<body>
<table style="margin:20% auto;border:1px solid red;">
<c:if test="${sessionScope.warn != null}">
<h2 color="red" align="center"><c:out value="${sessionScope.warn}" /></h2>
</c:if>
<form action=' ' method="post">
<tr>
<td>用户名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
<td><a style="text-decoration:none;border:1px solid red;background-color: aqua;color:red;" href=' '>前往注册</a></td>
</tr>
</form>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta content="text/html" charset="utf-8">
<title>注册测试</title>
</head>
<body>
<table style="margin:20% auto;border:1px solid red;">
<form action=' ' method="post">
<tr>
<td>用户名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="注册"></td>
<td><a style="text-decoration:none;border:1px solid red;background-color: aqua;color:red;" href=' '>前往登录</a></td>
</tr>
</form>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta content="text/html" charset="utf-8">
<title>成功登录后台系统</title>
<style>
td{
border: 1px solid red;
}
</style>
</head>
<body>
<c:if test="${sessionScope.user != null}">
<table style="margin:20% auto;border:1px solid red;">
<caption>欢迎用户<c:out value="${sessionScope.user.name}" /></caption>
<tr>
<td>用户名</td>
<td><c:out value="${sessionScope.user.name}" /></td>
</tr>
<tr>
<td>密码</td>
<td><c:out value="${sessionScope.user.password}" /></td>
</tr>
<tr border="1px">
<td>注册时间</td>
<td><c:out value="${sessionScope.user.regtime}" /></td>
</tr>
<tr>
<td><a href=' '>退出登录</a></td>
</tr>
</table>
</c:if>
<c:if test="${sessionScope.user == null}">
<h1 align="center" color="red">你尚未登录,点击<a href=' '>此处</a>进行登录!</h1>
</c:if>
</body>
</html>
package top.it1002.SSM.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SSMUtil {
public static ApplicationContext getAC(){
return new ClassPathXmlApplicationContext("dispatcher-servlet.xml");
}
}