pom.xml所需要的基本依赖和插件:
插件:这里Tomcat做测试用,注意Tomcat中的servlet与依赖中的servlet的冲突,可以配置依赖servlet的scope,<scope>providedscope>
插件中的
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<path>/path>
<port>8080port>
<uriEncoding>UTF-8uriEncoding>
configuration>
plugin>
配置web.xml:
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
contextConfigLocation
classpath:spring/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
ec
org.springframework.web.filter.CharacterEncodingFilter
encoding
Utf-8
ec
/*
ds
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:mvc/MVC.xml
1
ds
/
配置springmvc的核心配置文件:
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
配置spring核心配置文件:appliacationContext.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
mybatis核心配置文件:
"http://mybatis.org/dtd/mybatis-3-config.dtd">
:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/kgc1?zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=123456
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
connectionURL="jdbc:mysql://localhost:3306/kgc1?characterEncoding=UTF-8"
userId="root"
password="123456">
MbgTest逆向生成启动方法:
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MbgTest {
@Test
public void create() throws Exception {
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/6/24 0024
Time: 下午 7:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--搜索框--%>
<%--数据展示页面--%>
编号 | 姓名 | 年龄 | 性别 | 生日 | 津贴 | 工资 | 部门 |
${emp.id} | ${emp.name} | ${emp.age} | ${emp.sex} | ${emp.birth} | ${emp.bonus} | ${emp.salary} | ${emp.deptid} |
<%--分页区--%>
首页 | 上一页 | ${i} | 下一页 | 跳转到第页 | 尾页 |
:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/6/25 0025
Time: 上午 10:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
层:
import com.mapper.DeptMapper;
import com.mapper.EmpMapper;
import com.pojo.Dept;
import com.pojo.Emp;
import com.pojo.EmpExample;
import com.pojo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImpl implements IEmpService {
@Autowired
private DeptMapper deptMapper;
@Autowired
private EmpMapper empMapper;
@Override
public List
return null;
}
@Override//分页查询所有员工
public List
//1.条件 name startBirth endBirth deptId
EmpExample e = new EmpExample();
//条件拼接
EmpExample.Criteria c = e.createCriteria();
if(page.getName()!=null){
c.andNameLike("%"+page.getName()+"%");
}
if(page.getStartbirth()!=null){
c.andBirthGreaterThanOrEqualTo(page.getStartbirth());
}
if(page.getEndBirth()!=null){
c.andBirthLessThanOrEqualTo(page.getEndBirth());
}
if(page.getDeptid()!=null&&page.getDeptid()!=0){
c.andDeptidEqualTo(page.getDeptid());
}
//分页
//查询总记录数
int count = (int) empMapper.countByExample(e);
int size = page.getSize();
int totalPage=(count%size==0)?(count/size):(count/size+1);
page.setCount(count);
page.setTotalPage(totalPage);
int startRow=(page.getCurrPage()-1)*size;
e.limit(startRow,size);
List
for(Emp emp:list){
Dept dept = deptMapper.selectByPrimaryKey(emp.getDeptid());
emp.setDept(dept);
}
return list;
}
@Override//查询所有的部门
public List
return deptMapper.selectByExample(null);
}
@Override
public int deleteById(int id) {
return empMapper.deleteByPrimaryKey(id);
}
@Override
public int addEmp(Emp emp) {
return empMapper.insertSelective(emp);
}
@Override
public Emp selectById(int id) {
return empMapper.selectByPrimaryKey(id);
}
@Override
public int updateEmp(Emp emp) {
return empMapper.updateByPrimaryKeySelective(emp);
}
}
:
public class PageBean {
//分页四大参数
private Integer currPage=1;
private Integer size=3;
private Integer count;
private Integer totalPage;
//搜索框查询条件
private Integer deptid;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private Date Startbirth;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private Date EndBirth;
private String name;
:
import com.pojo.Dept;
import com.pojo.Emp;
import com.pojo.PageBean;
import com.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
private IEmpService service;
//新增页面跳转
@RequestMapping("/add1")
public String add1(Model m){
//查询所有的部门--中下拉框展示所有的部门
List
m.addAttribute("depts",depts);
return "add";
}
//实现新增
@RequestMapping("/add2")
public String add2(Emp emp){
int i=service.addEmp(emp);
return "redirect:/emp/show";//去show方法 重查
}
//1.查询单条 2.页面跳转
@RequestMapping("/update1/{id}")
public String update1(@PathVariable String id, Model m){
Emp e=service.selectById(Integer.parseInt(id));
m.addAttribute("emp",e);
//查询所有的部门--中下拉框展示所有的部门
List
m.addAttribute("depts",depts);
return "update";//转发到update.jsp
}
//执行修改
@RequestMapping("/update2")
public String update2(Emp emp){
int i=service.updateEmp(emp);
return "redirect:/emp/show";//去show方法 重查
}
@RequestMapping("/deleteById/{id}")
public String deleteById(@PathVariable String id){
int i=service.deleteById(Integer.parseInt(id));
//删除后重查数据库
return "redirect:/emp/show";//去show方法 重查
}
//取调存转
@RequestMapping("/show")
public ModelAndView show(PageBean page, ModelAndView mvc){
//查询所有部门下拉框展示
List
//模糊分页查询
List
//存
mvc.addObject("depts",depts);
mvc.addObject("list",list);
mvc.addObject("page",page);
//转-页面跳转
mvc.setViewName("show");
return mvc;
}
}