框架搭建就不重复了可以参考下面
https://blog.csdn.net/frank_hsu_/article/details/80286812
项目大致流程
pom.xml-web.xml-pojo-UserMapper.xml-UserMapper-IUserService-UserServiceImpl-UserController
pom.xml
4.0.0
org.ssmtest
ssm
war
0.0.1-SNAPSHOT
ssm Maven Webapp
http://maven.apache.org
4.0.2.RELEASE
3.2.6
1.7.7
1.2.17
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.30
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.alibaba
fastjson
1.1.41
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
ssm
org.apache.maven.plugins
maven-compiler-plugin
1.7
web.xml
Archetype Created Web Application
contextConfigLocation
classpath:spring-mybatis.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
SpringMVC
/
default
*.jpg
default
*.js
default
*.css
default
*.gif
/index.jsp
spring-mvc.xml
text/html;charset=UTF-8
spring-mybatis.xml
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test
username=root
password=root
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
log4j.properties
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%
User
package com.cn.ssm.pojo;
import javax.persistence.Column;
public class User {
private Integer id;
@Column(name="user_name")
private String userName;
private String password;
private Integer age;
/**
* @param id
* @param userName
* @param password
* @param age
*/
public User() {
super();
}
public User(Integer id, String userName, String password, Integer age) {
super();
this.id = id;
this.userName = userName;
this.password = password;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", age=" + age + "]";
}
}
UserMapper.xml
id, user_name, password, age
delete from user_t
where id = #{id,jdbcType=INTEGER}
insert into user_t (id, user_name, password,
age)
values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
insert into user_t
id,
user_name,
password,
age,
#{id,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
update user_t
user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
where id = #{id,jdbcType=INTEGER}
update user_t
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
UserMapper
package com.cn.ssm.dao;
import com.cn.ssm.pojo.User;
public interface UserMapper {
/* int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);*/
/**
* 查找用户名和密码
* @param username 登录用户名
* @param password 密码
* @return
*/
User findByUsername(String userName);
}
IUserService
package com.cn.ssm.service;
import com.cn.ssm.pojo.User;
//Service层接口
public interface IUserService {
/* public User getUserById(int userId);*/
//检验用户登录
public User checkUser(String userName,String password);
}
UserServiceImpl
package com.cn.ssm.service.impl;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.cn.ssm.dao.UserMapper;
import com.cn.ssm.pojo.User;
import com.cn.ssm.service.IUserService;
@Service
@Transactional
public class UserServiceImpl implements IUserService {
@Resource
public UserMapper userMapper;
/* @Override
//按ID查找用户名
public User getUserById(int userId) {
return this.userMapper.selectByPrimaryKey(userId);
}*/
@Override
public User checkUser(String userName, String password) {
// TODO Auto-generated method stub
User user=userMapper.findByUsername(userName);
if(user!=null&&user.getPassword().equals(password)){
System.out.println(user+"Service");
return user;
}
return null;
}
}
UserController
package com.cn.ssm.controller;
import javax.annotation.Resource;
/*import javax.servlet.http.HttpServletRequest;*/
import javax.servlet.http.HttpSession;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cn.ssm.pojo.User;
import com.cn.ssm.service.IUserService;
@Controller
@Scope(value="prototype")
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
/* @RequestMapping("userShow")
public String toIndex(HttpServletRequest request, Model model) {
int userId = Integer.parseInt(request.getParameter("id"));
System.out.println(userId);
User user = userService.getUserById(userId);
model.addAttribute("user", user);
return "showUser";
}*/
// 正常访问login页面
@RequestMapping("/login")
public String login() {
System.out.println("已经进去login");
return "login";
}
//表单提交过来的路径
@RequestMapping("/checkLogin")
//org.springframework.ui.Model springMVC返回的model
public String checkLogin(User user,Model model)throws Exception{
//调用service方法
System.out.println("已经进去checklogin");
user=userService.checkUser(user.getUserName(), user.getPassword());
//如果有user则添加到model中并跳转到成功页面
if(user!=null){
model.addAttribute(user);
System.out.println("Controller"+user);
return "success";
}
System.out.println(user+"Controller");
return "fail";
}
//测试超链接跳转到另一个页面是否可以取到session值
@RequestMapping("/anotherpage")
public String hrefpage(){
return "anotherpage";
}
//注销方法
@RequestMapping("/outLogin")
public String outLogin(HttpSession session){
//通过session.invalidata()方法来注销当前的session
session.invalidate();
return "login";
}
}
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
Insert title here
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Insert title here
<%-- welcome,${sessionScope.user.userName}! --%>
欢迎,${requestScope.user.userName}!
this is success page!
点我跳到另一个页面
fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
登陆失败!请输入正确的账号密码!~
anotherpage.jsp 测试sessionScope使用
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
${sessionScope.user.userName}!!!!!