关于SpringMVC与Spring结合Mybatis的Maven项目
各类文件位置
简单的POM.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.johe.scgcxx</groupId>
<artifactId>com.scgcxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- Servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring 上下文依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<!-- SpringMVC 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<!-- springmvc依赖的json支持包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- Spring JDBC 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<!-- aop依赖包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!-- Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<!-- mybatis整合spring插件 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 阿里巴巴的druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- MySQL依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- Oracle 依赖 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.1-atlassian-hosted</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
WEB.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>com.scgcxx</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置默认过滤 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载 SQLContext配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-*.xml</param-value>
</context-param>
<!-- 加载 applicationContext配置文件 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring-MVC.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 前端控制器配置文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 控制层注解 -->
<context:component-scan base-package="com.johe.scgcxx.controller"/>
<mvc:annotation-driven/><!-- 该配置实现了更多的功能,如支持json、日期格式转换等 -->
<mvc:default-servlet-handler/><!-- 支持静态资源访问 -->
<mvc:view-controller path="/" view-name="redirect:/safty/login/toLogin"/> <!-- 配置首页 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Spring-TX.xml事务配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 声明式事务配置文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd">
<!-- 通过注解说明事务,并制定使用哪一个事务管理器 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 针对数据源事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
Spring-Dao.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 数据访问层配置文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
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/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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
helperDialect=oracle
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 配置映射器的扫描路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.johe.scgcxx.dao" />
</bean>
</beans>
Spring-DS.xml数据源配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 数据源配置文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!-- druid数据源配置 -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<!--jdbc:mysql://localhost:3306/oa root root-->
<property name="username" value="scgcxx" />
<property name="password" value="scgcxx" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
</bean>
</beans>
Spring-Service.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 业务层配置文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 服务层注解 -->
<context:component-scan base-package="com.johe.scgcxx.service" />
</beans>
数据展示页面 JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,com.johe.scgcxx.model.*"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>花色号维护</title>
<link rel="stylesheet" href="../../css/bootstrap.min.css" />
<link rel="stylesheet" href="../../css/sweetalert2.min.css">
<link rel="stylesheet" href="../../css/animate.css" />
<link rel="stylesheet" href="../../css/all.css" />
<link rel="stylesheet" href="../../css/bootstrap-table.min.css" />
<script src="../../js/sweetalert2.min.js"></script>
<style type="text/css">
body {
font-size: 14px;
}
</style>
<body>
<div class="container-fluid" style="padding-left: 2px; padding-right: 2px;">
<div style="height: 50px">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="#">基础信息管理</a>
</li>
<li class="breadcrumb-item active" aria-current="page">
<a href="DepList">花色号维护</a>
</li>
</ol>
</nav>
</div>
<form action="doDelAll" method="post" id="myForm">
<div class="btn-group" style="margin-bottom: 5px;">
<button type="button" class="btn btn-outline-primary btn-sm" onclick="location='toAdd'">新增</button>
<button type="button" name="btn-delAll" class="btn btn-outline-danger btn-sm" >删除</button>
<div class="form-row">
<div class="col-sm-2">
<input type="number" min="1" class="form-control" value="${params.co_id }" name="co_id" placeholder="花色编号">
</div>
<div class="col-sm-2">
<input type="text" class="form-control" name="co_name" placeholder="花色名" value="${params.co_name }">
</div>
<div class="col-sm-2">
<input type="text" class="form-control" name="co_remark" placeholder="花色备注" value="${params.co_remark }">
</div>
<div>
<input id="bn-search" type="button" class="form-control btn-outline-success" value="搜索">
</div>
</div>
</div>
<table id="table"></table>
</form>
</div>
</body>
<script type="text/javascript" src="../../js/jquery-3.3.1.min.js" ></script>
<script type="text/javascript" src="../../js/popper.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap-table.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap-table-zh-CN.min.js"></script>
<script type="text/javascript" src="../../info/color/infoColorTable.js" ></script>
</html>
</body>
</html>
数据添加页面JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>花色新增</title>
<link rel="stylesheet" href="../../css/bootstrap.min.css" />
<link rel="stylesheet" href="../../css/sweetalert2.min.css">
<link rel="stylesheet" href="../../css/animate.css" />
<script src="../../js/sweetalert2.min.js"></script>
<style type="text/css">
body {
font-size: 14px;
}
</style>
</head>
<body>
<div class="container-fluid"
style="padding-left: 2px; padding-right: 2px;">
<div style="height: 50px">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#">基础信息管理</a></li>
<li class="breadcrumb-item"><a href="toList">花色号维护</a></li>
<li class="breadcrumb-item active" aria-current="page">花色号新增</li>
</ol>
</nav>
</div>
<form action="doAdd" method="post" name="myform">
<div class="form-group">
<label for="Info-color-co_id">花色编号</label> <input type="number"
class="form-control" id="Info-color-co_id" name="co_id" min="0"
placeholder="请输入花色编号">
</div>
<div class="form-group">
<label for="Info-color-co_name">花色名称</label> <input type="text"
class="form-control" id="Info-color-co_name" name="co_name"
placeholder="请输入花色名称">
</div>
<div class="form-group">
<label for="Info-color-co_remark">部门备注</label> <input type="text"
class="form-control" id="Info-color-co_remark" name="co_remark"
placeholder="请输入花色备注">
</div>
<button type="submit" id="sub_mit" class="btn btn-primary">提交花色信息</button>
</form>
</div>
</body>
<script type="text/javascript" src="../../js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../../js/popper.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../info/color/infoColor.js" ></script>
</html>
数据修改页面JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.johe.scgcxx.model.*,java.util.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>花色修改</title>
<link rel="stylesheet" href="../../css/bootstrap.min.css" />
<link rel="stylesheet" href="../../css/sweetalert2.min.css">
<link rel="stylesheet" href="../../css/animate.css" />
<script src="../../js/sweetalert2.min.js"></script>
<style type="text/css">
body {
font-size: 14px;
}
</style>
</head>
<body>
<div class="container-fluid"
style="padding-left: 2px; padding-right: 2px;">
<div style="height: 50px">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#">基础信息管理</a></li>
<li class="breadcrumb-item"><a href="toList">花色号维护</a></li>
<li class="breadcrumb-item active" aria-current="page">花色号修改</li>
</ol>
</nav>
</div>
<form action="doUpd" method="post" name="myform">
<div class="form-group">
<label for="infoColor-co_id">花色编号</label> <input type="number"
class="form-control" id="infoColor-co_id" name="co_id" min="0"
value="${infoColor.co_id}"
readonly="readonly">
</div>
<div class="form-group">
<label for="infoColor-co_name">花色名称</label> <input type="text"
class="form-control" id="infoColor-co_name" name="co_name"
value="${infoColor.co_name}" placeholder="请输入花色名称">
</div>
<div class="form-group">
<label for="infoColor-co_remark">花色备注</label> <input type="text"
class="form-control" id="infoColor-co_remark" name="co_remark"
value="${infoColor.co_remark}" placeholder="请输入花色备注">
</div>
<button type="submit" class="btn btn-primary"
onclick="return checkMSG()">提交花色信息</button>
</form>
</div>
</body>
<script type="text/javascript" src="../../js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../../js/popper.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../info/color/infoColor.js" ></script>
</html>
自定义BootStrap_Table需求JS
/* 员工信息关于BootStrap_Table 的显示 */
$(function() {
$('#table').bootstrapTable({
url: 'getPageList', // 请求路径
method: 'get', // 请求方式
toolbar:'#btn-toolbar',// 自定义工具栏 自定义DOM
striped: true, // 隔行换色
pagination: true, // 是否显示分页
sortable: true, // 排序
sortName: 'co_id', // 排序字段
sortOrder: 'asc', // 排序方式
// search: true, //是否显示表格搜索
sidePagination: 'server', // 分页方式client客户端 ,server服务端
pageSize: 3, // 初始化显示记录数 默认10
pageList: [3, 5, 10, 20], // 供选择的分页行数
paginationSuccessivelySize:2,// 允许最大连续页数
// paginationUseIntermediate:true,//计算显示中间页
// showToggle: true, //显示详细视图 和 列表视图切换
// showRefresh: true, //是否显示刷新按钮
// showColumns: true, //显示 选择列 展示按钮
// showPaginationSwitch: true, //是否显示分页
silentShort:true,
columns: [{
checkbox: true,
visible: true
}, {
field: "co_id",
title: "花色编号"
}, {
field: 'co_name',
title: '花色名'
}, {
field: 'co_remark',
title: '花色备注',
}, {
field: 'co_id',
title: '操作',
align: 'center',
valign: 'middle',
formatter: actionFormatter
}],
onLoadSuccess: () => {
toUpd();
doDel();
},
onDBlClickRow(row, $element) {
// var id = row.d_id;
},
queryParams:function(params) {
// 分页参数
// 模糊查询时需求参数
params['co_id']=$('input[name=co_id]').val();
params['co_name']=$('input[name=co_name]').val();
params['co_remark']=$('input[name=co_remark]').val();
return params;
}
});
});
$(function(){
$("button[type=button][name=btn-delAll]").click(function () {
var rows = $("#table").bootstrapTable('getSelections', function (row) {
return row;
});
if (rows.length== 0) {
alert("请先选择要删除的记录!");
return;
}
var co_ids = '';
for (var i = 0; i < rows.length; i++) {
co_ids += rows[i].co_id + ",";
}
co_ids = co_ids.substring(0, co_ids.length - 1);
doDelAll(co_ids);
});
});
// 关于编辑菜单的按钮
function actionFormatter(value, row, index) {
var id = value;
var status = row.d_status;
var result = "";
result += " <button type='button' class='btn btn-outline-primary btn-sm infoColorToUpd' "+
"data-url='toUpd' data-params='co_id=" + id +"'>修改</button> ";
result += " <button type='button' class='btn btn-outline-danger btn-sm infoColorDoDel'"+
"data-url='doDel' data-params='co_id=" + id +"'>删除</button>";
return result;
}
function toUpd(){
/* 修改页面跳转 */
$(".infoColorToUpd").click(function(){
location.href = this.getAttribute("data-url") + "?" + this.getAttribute("data-params");
});
}
function doDel(){
/* 删除提示操作 */
$(".infoColorDoDel").click(function(){
Swal({
title: '删除花色信息!!!',
type: 'warning',
showCancelButton: true,
confirmButtonText: '狠心删除',
cancelButtonText: '暂未决定',
}).then((rel) => {
if (rel.value) {
location.href = this.getAttribute("data-url") + "?" + this.getAttribute("data-params");
}
});
});
}
function doDelAll(co_ids){
$.ajax({
url: 'doDelAll',
data: {co_ids:co_ids},
method:'post',
dataType: 'json'
}).done((result) => {
location.href="toList";
});
/*.fail((xhr) => {
Swal({
type: 'error',
title: '服务器正在维护!',
})
});*/
}
$(function () {
$('#bn-search').click(function(){
//$("#myForm").attr("action","getPageList");
//$("#myForm").submit();
$('#table').bootstrapTable('refresh');
});
});
自定义操作JS
/*部门信息填写校验提示*/
$(function () {
$("button[type=submit]").click(function () {
var flag = false;
$("input").each(function () {
if (this.value == "") {
flag = true;
}
});
if (flag) {
Swal({
type: 'warning',
title: '花色信息填写不完整',
animation: false,
customClass: 'animated tada'
});
return false;
}
});
/*花色信息填写校验提示*/
$("#sub_mit").click(function(){
if (myform.co_id.value == "" || myform.co_name.value == "") {
Swal({
type: 'warning',
title: '花色信息填写不完整',
animation: false,
customClass: 'animated tada'
})
return false;
}
});
});
Controller
package com.johe.scgcxx.controller.info;
import java.util.HashMap;
import java.util.Map;
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.bind.annotation.ResponseBody;
import com.johe.scgcxx.dto.Info_Color;
import com.johe.scgcxx.service.info.InfoColorService;
//基础信息-花色号维护
@Controller
@RequestMapping("/info/color")
public class InfoColorController {
@Autowired
private InfoColorService infoColorService;
@RequestMapping("/getPageList")
@ResponseBody
public Map getList(Info_Color infoColor) {
System.out.println(infoColor.toString());
return infoColorService.getInfoColorPageMap(infoColor);
}
@RequestMapping("/toList")
public String toList() {
return "info/color/InfoColor";
}
@RequestMapping("/toAdd")
public String toAdd() {
return "info/color/InfoColorAdd";
}
@RequestMapping("/doAdd")
public String doAdd(Info_Color infoColor) {
infoColorService.addInfoColor(infoColor);
return "redirect:toList";
}
@RequestMapping("/toUpd")
public String toUpd(Model model,String co_id) {
com.johe.scgcxx.model.Info_Color infoColor = infoColorService.getInfoColorById(co_id);
model.addAttribute("infoColor", infoColor);
return "info/color/InfoColorUpd";
}
@RequestMapping("/doUpd")
public String doUpd(Info_Color infoColor) {
infoColorService.updInfoColor(infoColor);
return "redirect:toList";
}
@RequestMapping("/doDel")
public String doDel(String co_id) {
infoColorService.delInfoColorById(co_id);
return "redirect:toList";
}
@RequestMapping("/doDelAll")
@ResponseBody
public Map<String,Object> doDelAll(String co_ids) {
infoColorService.delInfoColorByIds(co_ids);
System.out.println(co_ids);
Map<String,Object> map = new HashMap<>();
map.put("result", "success");
return map;
}
}
ServiceImpl
package com.johe.scgcxx.service.info.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.johe.scgcxx.dao.info.InfoColorDao;
import com.johe.scgcxx.dto.Info_Color;
import com.johe.scgcxx.service.info.InfoColorService;
import com.johe.scgcxx.util.PageExec;
import com.johe.scgcxx.util.Utils;
//基础信息-花色号维护
@Service
public class InfoColorServiceImpl implements InfoColorService {
@Autowired
private InfoColorDao infoColorDao;
@Override
public List<com.johe.scgcxx.model.Info_Color> getInfoColorList() {
Info_Color infoColor = null;
return infoColorDao.getInfoColorList(infoColor);
}
@Override
public void addInfoColor(Info_Color infoColor) {
infoColorDao.addInfoColor(infoColor);
}
@Override
public Integer getInfoColorNum() {
return infoColorDao.getInfoColorNum();
}
@Override
public com.johe.scgcxx.model.Info_Color getInfoColorById(String co_id) {
return infoColorDao.getInfoColorById(co_id);
}
@Override
public void updInfoColor(Info_Color infoColor) {
infoColorDao.updInfoColor(infoColor);
}
@Override
public Map<String,Object> getInfoColorPageMap(Info_Color infoColor) {
return
Utils.getBootstrapTablePage(new PageExec<com.johe.scgcxx.model.Info_Color>() {
@Override
public List<com.johe.scgcxx.model.Info_Color> getList() {
return infoColorDao.getInfoColorList(infoColor);
}
}, infoColor);
}
@Override
public void delInfoColorById(String co_id) {
infoColorDao.delInfoColorById(co_id);
}
@Override
public void delInfoColorByIds(String co_ids) {
String[] co_idss = co_ids.split(",");
infoColorDao.delInfoColorByIds(co_idss);
}
}
Dao
package com.johe.scgcxx.dao.info;
import java.util.List;
import com.johe.scgcxx.dto.Info_Color;
//基础信息-花色号维护
public interface InfoColorDao {
List<com.johe.scgcxx.model.Info_Color> getInfoColorList(Info_Color infoColor);
void addInfoColor(Info_Color infoColor);
Integer getInfoColorNum();
com.johe.scgcxx.model.Info_Color getInfoColorById(String co_id);
void updInfoColor(Info_Color infoColor);
void delInfoColorById(String co_id);
void delInfoColorByIds(String[] co_idss);
}
DaoMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.johe.scgcxx.dao.info.InfoColorDao">
<select id="getInfoColorList"
resultType="com.johe.scgcxx.model.Info_Color">
select * from info_color
<trim prefix="where" prefixOverrides="and">
<if test="co_id != null">
and co_id = #{co_id}
</if>
<if test="co_name != null and co_name != ''">
and co_name like CONCAT(CONCAT('%',#{co_name}),'%')
</if>
<if test="co_remark != null and co_remark != ''">
and co_remark like CONCAT(CONCAT('%',#{co_remark}),'%')
</if>
</trim>
order by co_id
</select>
<select id="getInfoColorNum" resultType="Integer">
select count(1) from
info_color
</select>
<insert id="addInfoColor">
insert into info_color
values(#{co_id},#{co_name},#{co_remark})
</insert>
<select id="getInfoColorById"
resultType="com.johe.scgcxx.model.Info_Color">
select * from info_color where co_id = #{co_id}
</select>
<update id="updInfoColor">
update info_color
<trim prefix="set" suffixOverrides=",">
<if test="co_name!=null and co_name != ''">
co_name = #{co_name},
</if>
<if test="co_remark!=null">
co_remark = #{co_remark},
</if>
</trim>
where co_id = #{co_id}
</update>
<delete id="delInfoColorById">
delete from info_color where co_id=#{co_id}
</delete>
<delete id="delInfoColorByIds">
delete from info_color
<where>
co_id in
<foreach collection="array" index="index" item="co_id" open="("
close=")" separator=",">
#{co_id}
</foreach>
</where>
</delete>
</mapper>
PageHelper的分页设置
分页实体类
package com.johe.scgcxx.util;
public class PageParam {
private int offset;
private int limit;
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
}
分页工具类
package com.johe.scgcxx.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
public class Utils {
public static <T> PageInfo<T> getPageInfo(PageExec<T> exec,PageParam param){
PageHelper.startPage((param.getOffset()/param.getLimit())+1, param.getLimit());
List<T> list = exec.getList();
PageInfo<T> pageInfo = new PageInfo<>(list);
return pageInfo;
}
public static Map<String,Object> getBootstrapTablePage(Long num,List list){
Map<String,Object> map = new HashMap<String,Object>();
map.put("total",num);
map.put("rows",list);
return map;
}
public static <T> Map<String,Object> getBootstrapTablePage(PageExec<T> exec,PageParam param){
PageHelper.startPage(param.getOffset()/param.getLimit()+1, param.getLimit());
List<T> list = exec.getList();
PageInfo<T> pageInfo = new PageInfo<>(list);
return getBootstrapTablePage(pageInfo.getTotal(),pageInfo.getList());
}
}
分页接口
package com.johe.scgcxx.util;
import java.util.List;
public interface PageExec<T> {
public List<T> getList();
}