4.0.0
com.test
TestMaven4
war
0.0.1-SNAPSHOT
TestMaven4 Maven Webapp
http://maven.apache.org
4.3.3.RELEASE
3.4.0
1.7.7
1.2.17
junit
junit
4.12
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.3.0
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.25
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}
com.fasterxml.jackson.core
jackson-databind
2.7.5
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
aspectj
aspectjweaver
1.5.4
SSM-Project
org.apache.maven.plugins
maven-compiler-plugin
1.8
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springDispatcherServlet
/
DROP TABLE IF EXISTS tb_user;
CREATE TABLE tb_user (
id INT NOT NULL AUTO_INCREMENT,
userName VARCHAR(40),
password VARCHAR(100),
email VARCHAR(40),
phone VARCHAR(40),
roleName VARCHAR(40),
PRIMARY KEY(id)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
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
log4j.rootLogger=INFO,Console,File
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.File = org.apache.log4j.RollingFileAppender
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
log4j.appender.File.File = logs/ssm.log
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
log4j.appender.File.MaxFileSize = 10MB
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
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%n
springmvc.xml
User
和PageBean
user.java
package com.ssmtest.entity;
/**
* 用户实体类
* 对应数据表tb_user
* @author Peng
* @Date2016年12月13日上午9:36:23
*/
public class User {
private Integer id;
private String username;
private String password;
private String email;
private String phone;
private String rolename;
//角色名称 系统管理员、销售主管、客户经理、高管
public Integer getId() {
return id;
}
public User() {
super();
}
public User(String username, String password, String email, String phone, String rolename) {
super();
this.username = username;
this.password = password;
this.email = email;
this.phone = phone;
this.rolename = rolename;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename == null ? null : rolename.trim();
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", phone="
+ phone + ", rolename=" + rolename + "]";
}
}
PageBean.java
package com.ssmtest.entity;
import java.util.List;
/**
* 分页实体类
* @author Peng
* @Date2016年12月13日 上午9:40:10
*/
public class PageBean {
private int currPage;//当前页数
private int pageSize;//每页显示的记录数
private int totalCount;//总记录数
private int totalPage;//总页数
private List lists;//每页的显示的数据
public PageBean() {
super();
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
}
UserMapper.xml
id, userName, password, email, phone, roleName
delete from tb_user
where id = #{id,jdbcType=INTEGER}
insert into tb_user (id, userName, password,
email, phone, roleName
)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{rolename,jdbcType=VARCHAR}
)
insert into tb_user
id,
userName,
password,
email,
phone,
roleName,
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{rolename,jdbcType=VARCHAR},
update tb_user
userName = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
roleName = #{rolename,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update tb_user
set userName = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
roleName = #{rolename,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
UserDao.java
package com.ssmtest.dao;
import java.util.HashMap;
import java.util.List;
import com.ssmtest.entity.User;
public interface UserDao {
/**
* 根据主键删除一条用户数据
* @param id
* @return
*/
int deleteByPrimaryKey(Integer id);
/**
* 插入一条用户数据 ,可以不完整
* @param record
* @return
*/
int insertSelective(User record);
/**
* 根据主键查询一条用户数据
* @param id
* @return
*/
User selectByPrimaryKey(Integer id);
/**
* 用户登录
* @param record
* @return
*/
User loginByUserNameAndPassword(User record);
/**
* 根据主键更新一条用户数据,可以不完整 -
* @param record
* @return
*/
int updateByPrimaryKeySelective(User record);
/**
* 查询所有用户数据
* @return
*/
List selectUserList();
/**
* 查询用户记录总数
* @return
*/
int selectCount();
/**
* 分页操作,调用findByPage limit分页方法
* @param map
* @return
*/
List findByPage(HashMap map);
}
userService.java
package com.ssmtest.service;
import java.util.List;
import com.ssmtest.entity.PageBean;
import com.ssmtest.entity.User;
/**
* User类业务层接口
* @author Peng
* @Date2016年12月13日上午9:54:40
*/
public interface UserService {
int deleteByPrimaryKey(Integer id);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
User loginByUserNameAndPassword(User record);
List selectUserList();
int selectCount();
int updateByPrimaryKeySelective(User record);
PageBean findByPage(int currentPage);
}
UserServiceImpl.java
package com.ssmtest.service.impl;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ssmtest.dao.UserDao;
import com.ssmtest.entity.PageBean;
import com.ssmtest.entity.User;
import com.ssmtest.service.UserService;
/**
* User类业务层实现类
* @author Peng
* @Date2016年12月13日上午9:54:56
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public int deleteByPrimaryKey(Integer id) {
return userDao.deleteByPrimaryKey(id);
}
@Override
public int insertSelective(User record) {
return userDao.insertSelective(record);
}
@Override
public User selectByPrimaryKey(Integer id) {
return userDao.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(User record) {
return userDao.updateByPrimaryKeySelective(record);
}
@Override
public List selectUserList() {
return userDao.selectUserList();
}
@Override
public int selectCount() {
return userDao.selectCount();
}
@Override
public User loginByUserNameAndPassword(User record) {
return userDao.loginByUserNameAndPassword(record);
}
@Override
public PageBean findByPage(int currentPage) {
HashMap map = new HashMap();
PageBean pageBean = new PageBean();
//封装当前页数
pageBean.setCurrPage(currentPage);
//每页显示的数据
int pageSize=5;
pageBean.setPageSize(pageSize);
//封装总记录数
int totalCount = userDao.selectCount();
pageBean.setTotalCount(totalCount);
//封装总页数
double tc = totalCount;
Double num =Math.ceil(tc/pageSize);//向上取整
pageBean.setTotalPage(num.intValue());
map.put("start",(currentPage-1)*pageSize);
map.put("size", pageBean.getPageSize());
//封装每页显示的数据
List lists = userDao.findByPage(map);
pageBean.setLists(lists);
return pageBean;
}
}
package com.ssmtest.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.ssmtest.entity.User;
import com.ssmtest.service.UserService;
@SessionAttributes("currentUser")
@Controller
public class UserController {
@Resource
private UserService userService;
/**
* 用户登录
* @param user
* @param request
* @return
* @throws Exception
*/
@RequestMapping("/login")
public String login(@RequestParam("userName")String userName,
@RequestParam("password")String password,Model model) throws Exception{
User user = new User();
user.setUsername(userName);
user.setPassword(password);
User userresult = userService.loginByUserNameAndPassword(user);
if(userresult!=null){
//登录成功
List lists = userService.selectUserList();
model.addAttribute("userLists", lists);//回显用户信息
model.addAttribute("currentUser", userresult.getUsername());
return "redirect:main";
}
return "error";
}
@RequestMapping("/main")
public String main(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage,Model model){
model.addAttribute("pagemsg", userService.findByPage(currentPage));//回显分页数据
return "main";
}
/**
* 跳到编辑页面
* @param currentPage
* @param model
* @return
*/
@RequestMapping("/edit")
public String editpage(@RequestParam("id") int id,Model model){
User user =userService.selectByPrimaryKey(id);
model.addAttribute("returnUser", user);
return "edit";
}
/**
* 保存用户数据
* @return
*/
@RequestMapping("/save")
public String save(User user){
System.out.println(user.toString());
if(user.getId()==null){
//id为null是保存
userService.insertSelective(user);
}else{
//有id值为修改
userService.updateByPrimaryKeySelective(user);
}
return "redirect:main";
}
/**
* 删除用户数据
* @param id
* @return
*/
@RequestMapping("/delete")
public String delete(@RequestParam("id") int id){
userService.deleteByPrimaryKey(id);
return "redirect:main";
}
/**
* 添加一个用户数据
* @return
*/
@RequestMapping("/add")
public String add(Model model){
model.addAttribute("returnUser", new User());
return "edit";
}
}
index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Index.jsp
欢迎登陆
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
主页
欢迎你:${currentUser}
<%-- 当前用户:${pageScope.currentUser}
当前用户:${requestScope.currentUser}-${roleName} 有重定向数据就访问不到
当前用户:${sessionScope.currentUser}-${roleName} 可以使用
--%>
添加用户
没有任何用户信息!
编号
用户名
密码
邮件
联系电话
职位
Edit
Delete
${u.id }
${u.username }
${u.password }
${u.email }
${u.phone }
${u.rolename }
Edit
Delete
第${requestScope.pagemsg.currPage }/ ${requestScope.pagemsg.totalPage}页
总记录数:${requestScope.pagemsg.totalCount } 每页显示:${requestScope.pagemsg.pageSize}
[首页]
[上一页]
[下一页]
[尾页]
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
登录发生错误,请重新登录
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
编辑页面
欢迎你:${currentUser}
用户名:
密码:
邮件:
联系电话:
职位:
请选择职位
客户经理
高管
销售主管
系统管理员