1、maven配置
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jdw</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<properties>
<!-- spring版本号 -->
<spring.version>4.2.5.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- pagehelper分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.3</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency> <!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.5</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end --> <!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.jdw" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--
<property name="configLocation" value="classpath:SqlMapConfig.xml" /> -->
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/jdw/mapping/*.xml"></property> <property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jdw.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- ================================事务相关控制================================================= -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.RuntimeException" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" /> <tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="select*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="pc"
expression="execution(public * com.jdw.service.*.*(..))" /> <!--把事务控制在Service层 -->
<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
</aop:config> </beans>
3、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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
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-4.2.xsd">
<!-- <mvc:view-controller path="/" view-name="index" /> --> <mvc:default-servlet-handler /> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.jdw.controller" /> <!-- 启用默认配置 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 配置Fastjson支持 -->
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" >
<array>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</array>
</property>
<property name="features">
<array>
<value>WriteMapNullValue</value>
<value>WriteNullStringAsEmpty</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
4、service代码
@Override
public Map<String,Object> query(String type, Integer pageNo, Integer pageSize) {
pageNo = pageNo == null?1:pageNo;
pageSize = pageSize == null?10:pageSize;
PageHelper.startPage(pageNo, pageSize);
List<Category> list = this.dao.selectCategoryByType(type);
for(Category c:list)
{
System.out.println(c);
}
//用PageInfo对结果进行包装
PageInfo<Category> page = new PageInfo<Category>(list); //测试PageInfo全部属性
/*
System.out.println(page.getPageNum());
System.out.println(page.getPageSize());
System.out.println(page.getStartRow());
System.out.println(page.getEndRow());
System.out.println(page.getTotal());
System.out.println(page.getPages());
System.out.println(page.getFirstPage());
System.out.println(page.getLastPage());
System.out.println(page.isHasPreviousPage());
System.out.println(page.isHasNextPage());*/
Map<String, Object> map=new HashMap<String, Object>();
map.put("total", page.getTotal());
map.put("rows", page.getList());
return map;
}
5、controller代码
@ResponseBody
@RequestMapping(value = "/list")
public Map<String, Object> getCategories(HttpServletRequest request,HttpServletResponse response) {
int page=Integer.parseInt(request.getParameter("page"));
int rows=Integer.parseInt(request.getParameter("rows"));
String type=request.getParameter("type");
return categoryService.query(type, page, rows);
}
6、easyui调用
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ include file="/public/head.jspf"%>
<style type="text/css">
body {
margin: 1px;
} .searchbox {
margin: -3;
}
</style>
<script type="text/javascript">
$(function() {
$('#dg')
.datagrid(
{
url : '${shop}/category/list',
//width:250,
idField : 'id',
pageSize : 5,
pageList : [ 5, 10, 15, 20, 25 ],
queryParams : {
type : ''
},
striped : true,
nowrap : true,
fitColumns : true,
singleSelect : false,
pagination : true,
toolbar : [
{
iconCls : 'icon-add',
text : '添加类别',
handler : function() {
parent
.$("#win")
.window(
{
title : '添加类别',
width : 400,
height : 300,
content : '<iframe src="send_category_add.action" frameborder="0" width="100%" height="100%" />'
});
}
},
'-',
{
iconCls : 'icon-edit',
text : '更新类别',
handler : function() {
var rows = $("#dg").datagrid(
"getSelections");
if (rows.length != 1) {
$.messager.show({
title : '错误提示',
msg : '一次只能更新一条记录。',
timeout : 5000,
showType : 'slide'
});
} else {
parent
.$("#win")
.window(
{
title : '更新类别',
width : 400,
height : 240,
content : '<iframe src="send_category_update.action" frameborder="0" width="100%" height="100%" />'
});
}
}
},
'-',
{
iconCls : 'icon-remove',
text : '删除类别',
handler : function() {
var rows = $("#dg").datagrid(
"getSelections");
if (rows.length == 0) {
$.messager.show({
title : '错误提示',
msg : '至少选中一条记录。',
timeout : 5000,
showType : 'slide'
}); } else {
$.messager
.confirm(
'删除确认对话框',
'是否删除选中记录?',
function(r) {
if (r) {
var ids = "";
for (var i = 0; i < rows.length; i++) {
ids += rows[i].id
+ ",";
}
ids = ids
.substring(
0,
ids
.lastIndexOf(','));
$
.post(
"category_deleteByIds.action",
{
ids : ids
},
function(
result) {
if (result == "true") {
//取消选中的所有行
$(
"#dg")
.datagrid(
'uncheckAll');
$(
"#dg")
.datagrid(
'reload');
} else {
$.messager
.show({
title : '删除异常',
msg : '删除失败,请检查操作。',
timeout : 5000,
showType : 'slide'
});
}
},
'text');
}
}); }
}
},
'-',
{ text : "<input id='ss' name='search' />" } ],
rowStyler : function(index, row) {
if (index % 2 == 0) {
return 'background-color:#6293BB;color:#fff;';
}
},
frozenColumns : [ [ {
field : 'xyz',
checkbox : true
}, {
field : 'cId',
title : '编号',
width : 100
} ] ],
columns : [ [ {
field : 'cName',
title : '类别',
width : 100,
formatter : function(value, row, index) {
return "<span title="+value+">"
+ value + "</span>";
}
},
{
field : 'hot',
title : '热点',
width : 100,
align : 'center',
formatter : function(value, row, index) {
if (value)
return "<input type='checkbox' checked disabled='true' />";
else
return "<input type='checkbox' disabled='true' />";
}
/*
styler : function(value, row, index) {
if (value < 20) {
return 'background-color:#ffee00;color:red;';
}
}*/
} ] ]
}); $('#ss').searchbox({
searcher : function(value, name) {
$('#dg').datagrid('load', {
type : value
});
},
prompt : 'sssssss'
}); });
</script>
</head>
<body>
<table id="dg"></table> </body>
</html>
7、总结说明
(1)注意bean的getter、setter写法,不能这么写getcName,要这么写getCName,否则fastjson会找不到cName属性
(2)暂时不要用fastjson 1.2.10,有bug.会抛出 java.lang.AbstractMethodError异常
(3)<mvc:default-servlet-handler />处理静态资源,不要用mvc:resources,不好用,容易出问题;
(4)静态资源最好用如下方式:
head.jspf:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set value="${pageContext.request.contextPath }" var="shop" />
<title>my website</title>
<link rel="stylesheet"
href="${shop}/jquery-easyui-1.3.5/themes/default/easyui.css" type="text/css" />
<link rel="stylesheet" href="${shop}/jquery-easyui-1.3.5/themes/icon.css"
type="text/css" />
<script type="text/javascript"
src="${shop}/jquery-easyui-1.3.5/jquery.min.js"></script>
<script type="text/javascript"
src="${shop}/jquery-easyui-1.3.5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${shop}/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js"></script>
其他页面:
<head>
<%@ include file="/public/head.jspf"%>
</head>