1、项目目录和页面
2、配置文件
applicationContext.xml
springMVC.xml
web.xml
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
mvc-dispatcher
/
3、逻辑代码
package com.ssm.entity;
/***
* 用户表实体
* @author lvjun
*
*/
public class UserEntity {
private int id;
private String uname;
private int sex;//0女 1男 3保密
private int age;
private String card;
private String addr;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "UserEntity [id=" + id + ", uname=" + uname + ", sex=" + sex + ", age=" + age + ", card=" + card
+ ", addr=" + addr + "]";
}
}
package com.ssm.mapper;
import java.util.List;
import com.ssm.entity.UserEntity;
/***
*
* @author lvjun
*
*/
public interface UserMapper {
public List listM();//查询所有
public UserEntity getM(int id); // 查询单条
public int addM(UserEntity userEntity); // 添加
public void deleteM(int id); // 删除
public int editM(UserEntity userEntity); // 修改
}
delete from users where id= #{id}
update users set uname=#{uname},sex=#{sex},age=#{age},card=#{card},addr=#{addr} where id= #{id}
INSERT INTO users(uname,sex,age,card,addr)VALUES(#{uname},#{sex},#{age},#{card},#{addr})
package com.ssm.service;
import java.util.List;
import com.ssm.entity.UserEntity;
/***
*
* @author lvjun
*
*/
public interface UserService {
List list();//查询所有
UserEntity get(int id); // 查询单条
int add(UserEntity userEntity); // 添加
void delete(int id); // 删除
int edit(UserEntity userEntity); // 修改
}
package com.ssm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.entity.UserEntity;
import com.ssm.mapper.UserMapper;
import com.ssm.service.UserService;
/***
*
* @author lvjun
*
*/
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
@Override
public List list() {
// TODO Auto-generated method stub
return userMapper.listM();
}
@Override
public UserEntity get(int id) {
// TODO Auto-generated method stub
return userMapper.getM(id);
}
@Override
public int add(UserEntity userEntity) {
// TODO Auto-generated method stub
return userMapper.addM(userEntity);
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
userMapper.deleteM(id);
}
@Override
public int edit(UserEntity userEntity) {
// TODO Auto-generated method stub
return userMapper.editM(userEntity);
}
}
package com.ssm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ssm.entity.UserEntity;
import com.ssm.service.UserService;
/***
*
*
* @author lvjun
*
*/
@Controller
@RequestMapping("")
public class UserController {
@Autowired
UserService userService;
// 返回
@RequestMapping("fanhui")
public ModelAndView fanhui() {
ModelAndView mav = new ModelAndView("redirect:/list");
return mav;
}
// 查询列表
@RequestMapping("list")
public ModelAndView list() {
ModelAndView mav = new ModelAndView();
List ls = userService.list();
mav.addObject("ls", ls);
mav.setViewName("index");
return mav;
}
// 查询明细
@RequestMapping("get")
public ModelAndView get(int id) {
UserEntity c = userService.get(id);
ModelAndView mav = new ModelAndView("detail");
mav.addObject("c", c);
return mav;
}
// 单条明细修改跳转到修改页面
@RequestMapping("editgo")
public ModelAndView editgo(int id) {
UserEntity c = userService.get(id);
ModelAndView mav = new ModelAndView("edit");
mav.addObject("c", c);
return mav;
}
// 修改
@RequestMapping("edit")
public ModelAndView edit(UserEntity userEntity) {
userService.edit(userEntity);
ModelAndView mav = new ModelAndView("redirect:/list");
return mav;
}
// 删除
@RequestMapping("delete")
public ModelAndView delete(int id) {
userService.delete(id);
ModelAndView mav = new ModelAndView("redirect:/list");
return mav;
}
// 添加 跳转 到添加页面
@RequestMapping("addgo")
public ModelAndView addgo() {
ModelAndView mav = new ModelAndView("add");
return mav;
}
// 添加数据
@RequestMapping("add")
public ModelAndView add(UserEntity userEntity) {
userService.add(userEntity);
ModelAndView mav = new ModelAndView("redirect:/list");
return mav;
}
}
4、页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
编号
姓名
性别
年龄
身份证
籍贯
操作
${d.id}
${d.uname}
${d.sex}
${d.age}
${d.card}
${d.addr}
详情
修改
删除
添加
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
修改页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
详情页面
编号
姓名
性别
年龄
身份证
籍贯
${c.id}
${c.uname}
${c.sex}
${c.age}
${c.card}
${c.addr}
返回
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
添加