1. 基础框架--SSM(SpingMVC+Spring+Mybatis)
2.数据库-MySQL
3.前端框架-bootstrop快速搭建简洁美观的界面
4.项目依赖管理-Maven
5.分页-pagehelper
6.逆向工程-Mybatis Generator
package com.atwolf.crud.dao;
import java.util.List;
import com.atwolf.crud.bean.Department;
public interface DepartmentMapper {
//按主键ID删除一条数据
public int deleteByPrimaryKey(Integer deptId);
//保存全部数据
public int insert(Department record);
//有选择的保存部分数据
public int insertSelective(Department record);
//根据主键查询一条记录
public Department selectByPrimaryKey(Integer deptId);
//有选择的更新部分数据
public int updateByPrimaryKeySelective(Department record);
//更新全部数据
public int updateByPrimaryKey(Department record);
//查询所有
List Depts();
}
<mapper namespace="com.atwolf.crud.dao.DepartmentMapper">
<resultMap id="BaseResultMap" type="com.atwolf.crud.bean.Department">
<id column="dept_id" jdbcType="INTEGER" property="deptId" />
<result column="dept_name" jdbcType="VARCHAR" property="deptName" />
resultMap>
<sql id="Base_Column_List">
dept_id, dept_name
sql>
<select id="Depts" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tbl_dept
select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tbl_dept
where dept_id = #{deptId,jdbcType=INTEGER}
select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from tbl_dept
where dept_id = #{deptId,jdbcType=INTEGER}
delete>
<insert id="insert" parameterType="com.atwolf.crud.bean.Department">
insert into tbl_dept (dept_id, dept_name)
values (#{deptId,jdbcType=INTEGER}, #{deptName,jdbcType=VARCHAR})
insert>
<insert id="insertSelective" parameterType="com.atwolf.crud.bean.Department">
insert into tbl_dept
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="deptId != null">
dept_id,
if>
<if test="deptName != null">
dept_name,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="deptId != null">
#{deptId,jdbcType=INTEGER},
if>
<if test="deptName != null">
#{deptName,jdbcType=VARCHAR},
if>
trim>
insert>
<update id="updateByPrimaryKeySelective" parameterType="com.atwolf.crud.bean.Department">
update tbl_dept
<set>
<if test="deptName != null">
dept_name = #{deptName,jdbcType=VARCHAR},
if>
set>
where dept_id = #{deptId,jdbcType=INTEGER}
update>
<update id="updateByPrimaryKey" parameterType="com.atwolf.crud.bean.Department">
update tbl_dept
set dept_name = #{deptName,jdbcType=VARCHAR}
where dept_id = #{deptId,jdbcType=INTEGER}
update>
mapper>
package com.atwolf.crud.dao;
import java.util.List;
import com.atwolf.crud.bean.Employee;
public interface EmployeeMapper {
//ͨ通过主键删除一条记录
public int deleteByPrimaryKey(Integer empId);
//批量删除
public int deleteSelectiveEmp(List ids);
//保存全部数据
public int insert(Employee record);
//有选择的保存部分数据
public int insertSelective(Employee record);
//查询所有
public List selectByEmployee();
//ͨ通过主键ID查询数据
public Employee selectByPrimaryKey(Integer empId);
//有选择的更新字段
public int updateByPrimaryKeySelective(Employee record);
//更新全部字段
public int updateByPrimaryKey(Employee record);
//多表查询使用:查询员工表的同时,也把部门信息查询出来
public Employee selectByPrimaryKeywithDept(Integer empId);
//多表查询使用:查询员工表的同时,也把部门信息查询出来
public List selectByEmpWithDept();
//统计员工表的总记录数
public List selectByCountEmployee(Employee employee);
}
<mapper namespace="com.atwolf.crud.dao.EmployeeMapper">
<resultMap id="BaseResultMap" type="com.atwolf.crud.bean.Employee">
<id column="emp_id" jdbcType="INTEGER" property="empId" />
<result column="emp_name" jdbcType="VARCHAR" property="empName" />
<result column="gender" jdbcType="CHAR" property="gender" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="d_id" jdbcType="INTEGER" property="dId" />
resultMap>
<resultMap id="WithDept" type="com.atwolf.crud.bean.Employee">
<id column="emp_id" jdbcType="INTEGER" property="empId" />
<result column="emp_name" jdbcType="VARCHAR" property="empName" />
<result column="gender" jdbcType="CHAR" property="gender" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="d_id" jdbcType="INTEGER" property="dId" />
<association property="department" javaType="com.atwolf.crud.bean.Department">
<id column="dept_id" jdbcType="INTEGER" property="deptId" />
<result column="dept_name" jdbcType="VARCHAR" property="deptName" />
association>
resultMap>
<sql id="Base_Column_List">
emp_id, emp_name, gender, email, d_id
sql>
<sql id="WithDept_Column_List">
e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name
sql>
<select id="selectByCountEmployee" resultMap="WithDept" parameterType="com.atwolf.crud.bean.Employee">
select
<include refid="WithDept_Column_List"/>
from tbl_emp e left join tbl_dept d on d.dept_id=e.d_id
where e.emp_name=#{empName,jdbcType=VARCHAR}
select>
<select id="selectByPrimaryKeywithDept" parameterType="java.lang.Integer" resultMap="WithDept">
select <include refid="WithDept_Column_List" />
from tbl_emp e left join tbl_dept d
on d.dept_id=e.d_id
where emp_id=#{empId,jdbcType=INTEGER}
select>
<select id="selectByEmpWithDept" resultMap="WithDept">
select
<include refid="WithDept_Column_List"/>
from tbl_emp e left join tbl_dept d on d.dept_id=e.d_id
select>
<select id="selectByEmployee" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from tbl_emp
select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tbl_emp
where emp_id = #{empId,jdbcType=INTEGER}
select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from tbl_emp
where emp_id = #{empId,jdbcType=INTEGER}
delete>
<delete id="deleteSelectiveEmp" >
delete from tbl_emp
where emp_id in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
foreach>
delete>
<insert id="insert" parameterType="com.atwolf.crud.bean.Employee">
insert into tbl_emp (emp_id, emp_name, gender,
email, d_id)
values (#{empId,jdbcType=INTEGER}, #{empName,jdbcType=VARCHAR}, #{gender,jdbcType=CHAR},
#{email,jdbcType=VARCHAR}, #{dId,jdbcType=INTEGER})
insert>
<insert id="insertSelective" parameterType="com.atwolf.crud.bean.Employee">
insert into tbl_emp
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="empId != null">
emp_id,
if>
<if test="empName != null">
emp_name,
if>
<if test="gender != null">
gender,
if>
<if test="email != null">
email,
if>
<if test="dId != null">
d_id,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="empId != null">
#{empId,jdbcType=INTEGER},
if>
<if test="empName != null">
#{empName,jdbcType=VARCHAR},
if>
<if test="gender != null">
#{gender,jdbcType=CHAR},
if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
if>
<if test="dId != null">
#{dId,jdbcType=INTEGER},
if>
trim>
insert>
<update id="updateByPrimaryKeySelective" parameterType="com.atwolf.crud.bean.Employee">
update tbl_emp
<set>
<if test="empName != null">
emp_name = #{empName,jdbcType=VARCHAR},
if>
<if test="gender != null">
gender = #{gender,jdbcType=CHAR},
if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
if>
<if test="dId != null">
d_id = #{dId,jdbcType=INTEGER},
if>
set>
where emp_id = #{empId,jdbcType=INTEGER}
update>
<update id="updateByPrimaryKey" parameterType="com.atwolf.crud.bean.Employee">
update tbl_emp
set emp_name = #{empName,jdbcType=VARCHAR},
gender = #{gender,jdbcType=CHAR},
email = #{email,jdbcType=VARCHAR},
d_id = #{dId,jdbcType=INTEGER}
where emp_id = #{empId,jdbcType=INTEGER}
update>
mapper>
package com.atwolf.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atwolf.crud.bean.Department;
import com.atwolf.crud.dao.DepartmentMapper;
@Service
public class DepartmentService {
@Autowired
private DepartmentMapper departmentMapper;
//查询所有
public List getDepts() {
List list=departmentMapper.Depts();
return list;
}
}
package com.atwolf.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import com.atwolf.crud.bean.Employee;
import com.atwolf.crud.dao.EmployeeMapper;
@Service
public class EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
//单表查询所有
public List getAll() {
return employeeMapper.selectByEmployee();
}
//一对多查询:查询员工表对应的部门信息
public List getAllEmpWithDept(){
List emplist=employeeMapper.selectByEmpWithDept();
return emplist;
}
/**
* 保存员工
* @param employee
*/
public void saveEmp(Employee employee) {
employeeMapper.insertSelective(employee);
}
/**
* 服务端校验,用户名是否可用
* @param empName
* @return true:代表当前用户名可用,false:不可用
*/
public boolean checkUser(String empName) {
Employee emp=new Employee();
emp.setEmpName(empName);
List list=employeeMapper.selectByCountEmployee(emp);
int size=list.size();
System.out.println("size===="+list.size());
if(size==0){
return true; //用户名可用
}else{
return false;
}
}
/**
* 按照员工id查询员工
* @param id
* @return
*/
public Employee getEmp(Integer id) {
Employee employee=employeeMapper.selectByPrimaryKey(id);
return employee;
}
/**
* 修改:更新按钮的单击事件,保存修改后的信息
* @param employee
*/
public void updateEmp(Employee employee) {
employeeMapper.updateByPrimaryKeySelective(employee);
}
/**
* 删除:根据主键id删除一条记录
* @param id
*/
public void deleteEmp(Integer id) {
employeeMapper.deleteByPrimaryKey(id);
}
/**
* 批量删除
* @param ids
*/
public void deleteBatch(List ids) {
employeeMapper.deleteSelectiveEmp(ids);
}
}
package com.atwolf.crud.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.bind.annotation.ResponseBody;
import com.atwolf.crud.bean.Department;
import com.atwolf.crud.service.DepartmentService;
import com.atwolf.crud.utils.Msg;
/**
* 处理和部门有关的请求
* @author Administrator
*
*/
@Controller
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
/**
* 返回所有部门信息
*/
@RequestMapping(value="/depts")
@ResponseBody
public Msg getDepts(){
List list=departmentService.getDepts();
return Msg.success().add("depts", list);
}
}
package com.atwolf.crud.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.atwolf.crud.bean.Employee;
import com.atwolf.crud.service.EmployeeService;
import com.atwolf.crud.utils.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
/**
* 批量删除:也可以单个删(二合一)
* 批量删除:id的值 1-2-3
* 单个删除:id的值 1
*/
@ResponseBody
@RequestMapping(value="/emp/{ids}",method=RequestMethod.DELETE)
public Msg deleteEmp(@PathVariable("ids") String ids){
if(ids.contains("-")){
List del_ids=new ArrayList();
//如果参数ids中包含“-”,就是批量删除
String[] str_ids = ids.split("-");
//组装id的集合
for (String str : str_ids) {
Integer strids=Integer.parseInt(str);
del_ids.add(strids);
}
employeeService.deleteBatch(del_ids);
}else{
Integer id= Integer.parseInt(ids);
employeeService.deleteEmp(id);
}
return Msg.success();
}
/**
* 删除:根据主键id删除一条记录
* @param id
* @return
*/
/* @ResponseBody
@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
public Msg deleteEmpById(@PathVariable("id") Integer id){
employeeService.deleteEmp(id);
return Msg.success();
}*/
/**
* 修改:更新按钮的单击事件,保存修改后的数据
*/
@ResponseBody
@RequestMapping(value="/emp/{empId}",method=RequestMethod.PUT)
public Msg saveEmp(Employee employee){
employeeService.updateEmp(employee);
return Msg.success();
}
/**
* 修改:根据id查询所有员工数据
* @PathVariable("id")Integer id :@PathVariable("id")是指定参数 Integer id来源于上面路径中的id
*/
@RequestMapping(value="/emp/{id}",method=RequestMethod.GET)
@ResponseBody
public Msg getEmp(@PathVariable("id")Integer id){
Employee employee=employeeService.getEmp(id);
return Msg.success().add("emp", employee);
}
/**
* 后端校验:检查用户名是否可用,用户名的唯一性
* @param empName
* @return
*/
@RequestMapping("/checkuser")
@ResponseBody
public Msg checkuse(@RequestParam("empName")String empName){
//前后端保持校验信息一致
String regx="(^[a-zA-Z0-9_-]{6,16}$)|(^[\u2E80-\u9FFF]{2,5})";
boolean ide=empName.matches(regx);
if(!ide){
return Msg.fail().add("va_msg", "用户名必须是6-16位英文和数字的组合或者2-5位中文");
}
//数据库用户名重复校验
boolean iden=employeeService.checkUser(empName);
if(iden==true){
return Msg.success();//用户名可用
}else{
return Msg.fail().add("va_msg", "该用户名不可以使用");
}
}
/**
* 员工保存
* 1.支持JSR303校验
* 2.导入Hibernate-Validator包
* @Valid Employee employee 对封装的数据进行校验
* BindingResult result 封装校验结果
* @return
*/
@RequestMapping(value="/emp",method=RequestMethod.POST)
@ResponseBody
public Msg saveEmp(@Valid Employee employee,BindingResult result){
if(result.hasErrors()){
Map map =new HashMap();
//校验失败,应该返回失败,并在模态框中显示校验失败信息
List errors=result.getFieldErrors();
for (FieldError fieidError:errors) {
System.out.println("错误的字段名:"+fieidError.getField());
System.out.println("错误信息"+fieidError.getDefaultMessage());
map.put(fieidError.getField(), fieidError.getDefaultMessage());
}
return Msg.fail().add("errorFields", map);//传到浏览器显示
}else{
employeeService.saveEmp(employee);
return Msg.success();
}
}
/**
* 单表分页查询所有
* @param pn
* @param model
* @return
*/
@RequestMapping("/emps")
public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
//引入PageHelper分页插件
//在查询之前只需要调用,参数1:页码,参数2:每页有多少条数据
PageHelper.startPage(pn, 10);
//startPage后面紧跟的查询就是一个分页查询
List emps= employeeService.getAll();
//使用pageInfo包装查询后的结果,只需要将PageInfo交给页面就可以了
//PageInfo封装了分页的信息,包括查询出来的数据,传入连续显示的页数
PageInfo page=new PageInfo(emps,5);
model.addAttribute("pageInfo", page);
return "list";
}
/**
* 一对多分页查询所有
*/
@RequestMapping("/empwithdept")
public String getAllEmpWithDept(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model){
PageHelper.startPage(pn, 10);
List emps=employeeService.getAllEmpWithDept();
PageInfo page=new PageInfo(emps,5);
model.addAttribute("pageInfo", page);
return "list";
}
/**
*一对多分页查询所有 ,使用AJAX请求
*@ResponseBody自动将返回的对象转换成json字符串,@ResponseBody要能正常使用,需要导入jackson包
*/
@RequestMapping("/empwithdeptjson")
@ResponseBody
public Msg getEmpsWithJsion(@RequestParam(value="pn",defaultValue="1") Integer pn){
PageHelper.startPage(pn, 10);
List emps=employeeService.getAllEmpWithDept();
PageInfo page=new PageInfo(emps,5);
//直接返回一个对象,该对象会自动转换成json串
//return page;//写一个通用的处理返回的方法
return Msg.success().add("pageInfo", page);
}
}
package com.atwolf.crud.utils;
import java.util.HashMap;
import java.util.Map;
/**
* 返回json数据的通用返回类
* @author Administrator
*
*/
public class Msg {
//状态码 100-成功 200-失败
private int code;
//提示信息
private String msg;
//用户要返回给浏览器的数据
private Map extend =new HashMap();
public static Msg success(){
Msg result=new Msg();
result.setCode(100);
result.setMsg("处理成功");
return result;
}
public static Msg fail(){
Msg result=new Msg();
result.setCode(200);
result.setMsg("处理失败");
return result;
}
public Msg add(String key,Object value){
Map map=this.getExtend();
map.put(key, value);
return this;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Map getExtend() {
return extend;
}
public void setExtend(Map extend) {
this.extend = extend;
}
}
package com.atwolf.crud.bean;
public class Department {
private Integer deptId;
private String deptName;
public Department(Integer deptId, String deptName) {
super();
this.deptId = deptId;
this.deptName = deptName;
}
public Department() {
super();
// TODO Auto-generated constructor stub
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName == null ? null : deptName.trim();
}
}
package com.atwolf.crud.bean;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
public class Employee {
private Integer empId;
@Pattern(regexp="(^[a-zA-Z0-9_-]{6,16}$)|(^[\u2E80-\u9FFF]{2,5})",
message="用户名必须是2-5位中文或者6-16位英文和数字的组合")
private String empName;
private String gender;
//@Email 主要要使用转义字符/
@Pattern(regexp="^[a-z\\d]+(\\.[a-z\\d]+)*@([\\da-z](-[\\da-z])?)+(\\.{1,2}[a-z]+)+$",
message="邮箱格式不正确")
private String email;
private Integer dId;
//一对多查询中,员工表中添加部门信息
private Department department;
public Employee(Integer empId, String empName, String gender, String email, Integer dId) {
super();
this.empId = empId;
this.empName = empName;
this.gender = gender;
this.email = email;
this.dId = dId;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName == null ? null : empName.trim();
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender == null ? null : gender.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public Integer getdId() {
return dId;
}
public void setdId(Integer dId) {
this.dId = dId;
}
}
Bootstrop+jquery+Ajax操作数据
附源码下载网址:
基于Maven依赖的SSM AJAX增删改查demo