博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
搭建SSM框架的配置文件
阅读量:5157 次
发布时间:2019-06-13

本文共 14735 字,大约阅读时间需要 49 分钟。

pom.xml所需要的基本依赖和插件:

<dependency>

            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <!--逆向生成带分页的插件-->
        <dependency>
            <groupId>com.itfsw</groupId>
            <artifactId>mybatis-generator-plugin</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!--2.Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <!--3.SpringMvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <!--import org.junit.Test  @Test-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
        <!--4.c3p0-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!--pojo转换成 json -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!--servlet 2.5 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <!--此处的servlet仅在编译和测试的时候使用,运行的时候不使用,使用的是tomcat中的servlet-->
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

插件:这里Tomcat做测试用,注意Tomcat中的servlet与依赖中的servlet的冲突,可以配置依赖servlet的scope,<scope>provided</scope>

         插件中的 <pluginManagement>是插件管理,如果要运行Tomcat,需要注释掉这个插件管理

org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
/
8080
UTF-8
配置web.xml:
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的核心配置文件:
配置spring核心配置文件:appliacationContext.xml:
mybatis核心配置文件:

db.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/kgc1?zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=123456

 

mbg逆向生成配置文件:

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); } }

show.jsp

<%@ 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" %>
<html>
<head>
    <title>数据展示页面</title>
    <script src="/js/jquery-3.3.1.min.js"/>
</head>
<body>
<%--搜索框--%>
<form  id="myForm" action="/emp/show" method="post" style="text-align: center">
    姓名:<input type="text" name="name" value="${page.name}">
    生日:<input type="date" name="startBirth"  value="<fmt:formatDate value='${page.startBirth}' pattern='yyyy-MM-dd'/>" >-
    <input type="date" name="endBirth" value="<fmt:formatDate value='${page.endBirth}' pattern='yyyy-MM-dd'/>" >
    部门:<select name="deptId" >
    <option value="0">--请选择--</option>
    <c:forEach var="dept" items="${depts}">
        <option value="${dept.did}"
                <c:if test="${dept.did==page.deptId}">selected=true</c:if>
        >${dept.dname}</option>
    </c:forEach>
</select>
    <input type="hidden" id="pageNum" name="currPage" value="1">
    <input type="submit" value="搜索">
</form>
<%--数据展示页面--%>
<table border="1" cellpadding="10" cellspacing="1">
    <tr>
        <td>编号</td>
        <td>姓名</td>
        <td>年龄</td>
        <td>性别</td>
        <td>生日</td>
        <td>津贴</td>
        <td>工资</td>
        <td>部门</td>
    </tr>
    <c:forEach var="emp" items="${list}">
        <tr>
            <td>${emp.id}</td>
            <td>${emp.name}</td>
            <td>${emp.age}</td>
            <td>${emp.sex}</td>
            <td>${emp.birth}</td>
            <td>${emp.bonus}</td>
            <td>${emp.salary}</td>
            <td>${emp.deptid}</td>
        </tr>
    </c:forEach>
</table>
<%--分页区--%>
<table align="center" cellpadding="10" cellspacing="10" border="1">
    <tr>
        <td><a href="javascript:goPage(1)">首页</a></td>
        <td><a href="javascript:goPage(${page.currPage-1})">上一页</a></td>
        <c:forEach var="i" begin="1" end="${page.totalPage}">
            <td><a href="javascript:goPage(${i})">${i}</a></td>
        </c:forEach>
        <td><a href="javascript:goPage(${page.currPage+1})">下一页</a></td>
        <td><a href="javascript:toPage()">跳转</a>到第<input size="2" type="text" value="${page.currPage}" id="go">页</td>
        <td><a href="javascript:goPage(${page.totalPage})">尾页</a></td>
    </tr>
</table>
</body>
<!--js部分-->
<script>
    function goPage(page) {
        var total=${page.totalPage};
        if(page>total){
            page=total;
        }
        if(page<1){
            page=1;
        }
        $("#pageNum").val(page);
        $("#myForm").submit()
    }
    function toPage() {
        var page=$("#go").val();
        goPage(page)
    }
</script>
</html>

add.jsp

<%@ 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" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/emp/add2" method="post">
    姓名:<input type="text" name="name"/> <br>
    年龄:<input type="text" name="age"/><br>
    性别:<input type="text" name="sex"/><br>
    生日:<input type="date" name="birth"/><br>
    津贴:<input type="text" name="bonus"/><br>
    工资:<input type="text" name="salary"/><br>
    部门:<select name="deptid">
    <c:forEach var="dept" items="${depts}">
        <option value="${dept.did}">${dept.dname}</option>
    </c:forEach>
</select><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

 

update.jsp

<%@ 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" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/emp/update2" method="post">
    <input type="hidden" name="id" value="${emp.id}">
    姓名:<input type="text" name="name" value="${emp.name}"/> <br>
    年龄:<input type="text" name="age" value="${emp.age}"/><br>
    性别:<input type="text" name="sex" value="${emp.sex}"/><br>
    生日:<input type="date" name="birth"
              value="<fmt:formatDate value='${emp.birth}' pattern='yyyy-MM-dd'/>" /><br>
    津贴:<input type="text" name="bonus" value="${emp.bonus}"/><br>
    工资:<input type="text" name="salary" value="${emp.salary}"/><br>
    部门:<select name="deptid">
    <c:forEach var="dept" items="${depts}">
        <option value="${dept.did}" <c:if test="${dept.did==emp.deptid}" >selected='true'</c:if> >
                ${dept.dname}
        </option>
    </c:forEach>
</select><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

service层:

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<Dept> selectDept() {
        return null;
    }
    @Override//分页查询所有员工
    public List<Emp> selectAll(PageBean page) {
        //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<Emp> list = empMapper.selectByExample(e);
        for(Emp emp:list){
            Dept dept = deptMapper.selectByPrimaryKey(emp.getDeptid());
            emp.setDept(dept);
        }
        return  list;
    }
    @Override//查询所有的部门
    public List<Dept> selectDepts() {
        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);
    }
}

 

pagebean:

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;

 

controller:

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<Dept> depts=service.selectDepts();
        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<Dept> depts=service.selectDepts();
        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<Dept> depts=service.selectDept();
    //模糊分页查询
        List<Emp> list=service.selectAll(page);
    //存
    mvc.addObject("depts",depts);
    mvc.addObject("list",list);
    mvc.addObject("page",page);
    //转-页面跳转
        mvc.setViewName("show");
        return mvc;
    }
}

 

转载于:https://www.cnblogs.com/xiaoshenke/p/11110774.html

你可能感兴趣的文章
单调栈与单调队列
查看>>
Linr PS toolkit(Photoshop开发人员辅助工具)(转载)
查看>>
discuz 抱歉,该附件无法读取的解决方法
查看>>
spring mvc+mybatis+多数据源切换
查看>>
[转]SHSH, APTicket以及iOS降級
查看>>
python中的魔法属性
查看>>
lodash camelCase 驼峰写法
查看>>
一分钟了解负载均衡的一切,学习学习
查看>>
介绍网络课程给大家
查看>>
如何:声明、实例化和使用委托(C# 编程指南)
查看>>
C# SpeechSynthesizer 使用
查看>>
leetcode roman to integer
查看>>
心急的C小加
查看>>
邻接矩阵,邻接表
查看>>
javaweb 程序一会能操作一会不能操作,一会能连上数据库一会不能!!!
查看>>
分布式文件系统HDFS 练习
查看>>
编译原理 First,Follow,select集求法
查看>>
maven package跳过测试
查看>>
不要轻易相信用户
查看>>
javascript
查看>>