目录
Springmvc+Spring+Mybatis+Mave
发工具:Eclipse Java EE IDE for Web Developers Version: Oxygen.3a Release
开发环境:Jdk 1.8.0_171
服务器:apache-tomcat-7.0.78-windows-x64
看见工程有红叉,是因为缺少web.xml 文件,解决:
右键项目 --> Java EE Tools --> Generate Deployment Descriptor Stub
4.0.0
com.ljh
maven_ssm
0.0.1-SNAPSHOT
war
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.7
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
8080
UTF-8
javax.servlet
servlet-api
3.0-alpha-1
provided
junit
junit
3.8.1
test
org.springframework
spring-test
4.2.5.RELEASE
org.springframework
spring-webmvc
4.3.12.RELEASE
org.springframework
spring-core
4.3.12.RELEASE
org.springframework
spring-aop
4.3.12.RELEASE
aspectj
aspectjweaver
1.5.4
aspectj
aspectjrt
1.5.4
org.codehaus.jackson
jackson-mapper-asl
1.9.11
org.mybatis
mybatis
3.1.1
org.mybatis
mybatis-spring
1.1.1
mysql
mysql-connector-java
5.1.21
c3p0
c3p0
0.9.1.2
jar
org.slf4j
slf4j-api
1.7.5
org.slf4j
slf4j-log4j12
1.7.5
log4j
log4j
1.2.17
org.springframework
spring-context-support
4.3.12.RELEASE
jar
compile
javax.servlet
jstl
1.2
package com.ljh.user.pojo;
public class User {
private Integer userId;
private String userName;
private String userPwd;
private String userEmail;
private Integer userAge;
private String userIdCardNo;
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", userPwd=" + userPwd + ", userEmail=" + userEmail
+ ", userAge=" + userAge + ", userIdCardNo=" + userIdCardNo + "]";
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public Integer getUserAge() {
return userAge;
}
public void setUserAge(Integer userAge) {
this.userAge = userAge;
}
public String getUserIdCardNo() {
return userIdCardNo;
}
public void setUserIdCardNo(String userIdCardNo) {
this.userIdCardNo = userIdCardNo;
}
}
package com.ljh.page.pojo;
import java.util.List;
/**
* 分页工具类.泛型类
*/
public class PageUtil{
//属性
private int pageNumber;//总记录数
private int pageCount;//总页数
private int pageIndex;//当前页
private int pageSize;//每页大小
private List list;//当前页的数据
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
package com.ljh.user.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ljh.page.pojo.PageUtil;
import com.ljh.user.pojo.User;
import com.ljh.user.service.UserService;
/**
* 控制层
* @author 46656
*
*/
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userServiceImp;
/**
* 用户注册
* @param user
* @return
*/
@RequestMapping("reg")
public ModelAndView reg(User user) {
userServiceImp.reg(user);
ModelAndView mav = new ModelAndView();
mav.setViewName("forward:/user/getAllUser.action");
return mav;
}
/**
* 查询全部用户
* @return
*/
@RequestMapping("getAllUser")
public ModelAndView getAllUser() {
List userList = userServiceImp.getAllUser();
ModelAndView mav = new ModelAndView();
mav.setViewName("getAllUser");
mav.addObject("userList", userList);
return mav;
}
/**
* 根据ID查找用户
* @param user
* @return
*/
@RequestMapping("getUserById")
public ModelAndView getUserById(User user) {
User userGet = userServiceImp.getUserById(user);
ModelAndView mav = new ModelAndView();
mav.setViewName("updateUser");
mav.addObject("userGet", userGet);
return mav;
}
/**
* 修改用户
* @param user
* @return
*/
@RequestMapping("updateUser")
public ModelAndView updateUser(User user) {
userServiceImp.updateUser(user);
ModelAndView mav = new ModelAndView();
mav.setViewName("forward:/user/getAllUser.action");
return mav;
}
/**
* 删除用户
* 带事务控制
* @param user
* @return
*/
@RequestMapping("delUser")
public ModelAndView delUser(User user) {
userServiceImp.delUser(user);
ModelAndView mav = new ModelAndView();
mav.setViewName("redirect:/user/getAllUser.action");
return mav;
}
/**
* 登录
* @param user
* @return
*/
@RequestMapping("login")
public ModelAndView login(User user) {
User userLogined = userServiceImp.login(user);
ModelAndView mav = new ModelAndView();
if(null == userLogined || "".equals(userLogined)) {
mav.setViewName("forward:/login.jsp");
mav.addObject("fail", "fail");
}else {
mav.setViewName("forward:/user/getAllUser.action");
mav.addObject("userLogined", userLogined);
}
return mav;
}
/**
* 分页
* @param request
* @param model
* @return
*/
@RequestMapping("getUserByPage")
public ModelAndView getUserByPage(HttpServletRequest request, Model model) {
int pageIndex = 1; //设置初始的当前页,页面显示的都是第一页
int pageSize = 10; //此处的值与sql语句的limit后面的数值保持一致
PageUtil pageUtil = new PageUtil(); List list = new ArrayList();
if (request.getParameter("pageIndex") != null) {
pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
} //获取页面上点击'上一页或者下一页'传过来的pageindex
pageUtil.setPageIndex(pageIndex); //保存
int number = userServiceImp.pageCount(); //获得总数据量
pageUtil.setPageNumber(number); //保存
pageUtil.setPageSize(pageSize); //保存
pageUtil.setPageCount((int) Math.ceil((double) (pageUtil.getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
int index = (pageIndex - 1) * pageSize; //计算出每一页从数据库中第几条数据开始取值,也就是limit后面的第一个数字
list = userServiceImp.getUserByPage(index); //取得数据库中的值
pageUtil.setList(list); //保存到工具类中的集合
model.addAttribute("pageUtil", pageUtil); //传递到页面,存入值栈中
return new ModelAndView("getUserByPage"); //跳转的相关页面
}
}
package com.ljh.user.service.imp;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.ljh.user.mapper.UserMapper;
import com.ljh.user.pojo.User;
import com.ljh.user.service.UserService;
public class UserServiceImp implements UserService{
@Autowired
private UserMapper userMapperImp;
public void reg(User user) {
userMapperImp.reg(user);
}
public List getAllUser() {
return userMapperImp.getAllUser();
}
public void updateUser(User user) {
userMapperImp.updateUser(user);
}
public User getUserById(User user) {
return userMapperImp.getUserById(user);
}
public void delUser(User user) {
userMapperImp.delUser(user);
//int i = 1/0; //测试事务控制
}
public User login(User user) {
return userMapperImp.login(user);
}
//获取总页数
public int pageCount(){
return userMapperImp.pageCount();
}
//分页显示所有用户
public List getUserByPage(int index){
return userMapperImp.getUserByPage(index);
}
}
insert into userinfo(userName,userPwd,userEmail,userAge,userIdCardNo)
values
(#{userName},#{userPwd},#{userEmail},#{userAge},#{userIdCardNo})
update userinfo
userName=#{userName},userPwd=#{userPwd},userEmail=#{userEmail},userAge=#{userAge},userIdCardNo=#{userIdCardNo}
userId=#{userId}
delete from userinfo where userId=#{userId}
maven_ssm
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
maven_ssm
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
1
maven_ssm
*.action
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath*:spring/applicationContext-*.xml
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
exam0723 登录页面
Login ${fail }
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
maven_ssm 分页查询所有用户
欢迎 ${userLogined.userName}
用户名
密码
邮箱
年龄
身份证号码
操作
${user.userName }
${user.userPwd }
${user.userEmail }
${user.userAge }
${user.userIdCardNo }
删除修改
当前第${pageUtil.pageIndex}页/共${pageUtil.pageCount}页 共${pageUtil.pageNumber}条记录
首页
下一页
上一页
尾页