首先我们需要建立好一个emaven项目,并且在pom.xml中导入响应的jar包,
<?xml version="1.0" encoding="UTF-8"?> <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>cn.itsource</groupId>
<artifactId>crm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <name>crm</name>
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- spring版本号 -->
<spring.version>4.1.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.1</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.2</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<!-- jackson包版本 -->
<jackson.version>2.5.0</jackson.version>
</properties> <dependencies>
<!--JUnit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!--Spring核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</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-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency> <!--SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> <!--aop依赖包-->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency> <!--文件上传-->
<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.2</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency> <!--数据库连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.3</version>
</dependency> <!--Mybatis集成-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency> <!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency> <!--slf4j-->
<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>
<scope>test</scope>
</dependency> <!--数据库连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency> <!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies> <build>
<finalName>crm</finalName>
<plugins>
<!--代码生成器插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--自定义代码生成器的路径-->
<!--<configurationFile>yourLocation/mybatis-generator-config.xml</configurationFile>-->
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</build>
</project>
2接着是配置web.xml文件,
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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"
version="3.0">
<display-name>crm</display-name>
<!-- Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring监听器 ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Spring MVC 核心配置开始 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.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> <!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3配置数据库的链接信息文件也就是jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///数据库名
jdbc.username=用户名
jdbc.password=密码
4配置applictionContext.xml文件,在applicationContext中需要注意,配置mapper映射,这样就能直接在我们的类中注入mapper对象,注意用到包的地方时自己的包
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
"> <!--扫描Service层-->
<context:component-scan base-package="cn.itsource.crm.service" /> <!--引入jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--创建数据源(dataSource)-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--
咱们以前学过JPA,JPA的话需要配置EntityManagerFactory(需要读取配置,做很多事情都能配置)
咱们使用了一个FactoryBean完成 -> EntityManagerFactoryBean
配置SqlSessionFactory需要读取配置,做很多事情都能配置),它有一个叫做SqlSessionFactoryBean
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--XML的映射-->
<property name="mapperLocations" value="classpath:cn/itsource/crm/mapper/*.xml" />
<!--为所有相应的包中的类取别名-->
<property name="typeAliasesPackage" value="cn.itsource.crm.domain" />
</bean> <!--一劳永逸的方案:直接创建所有的映射器mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.crm.mapper" />
</bean> <!--我们需要配置一个事务管理器
以前学习JPA ,是有一个类JpaTransactionManager的事务对象
mybatis用的是:DataSourceTransactionManager
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置标签支持事务-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
接下来就是applicationContext-mvc.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
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
">
<!--扫描Controller-->
<context:component-scan base-package="cn.itsource.crm.web.controller" />
<!--支持SpringMVC的注解-->
<mvc:annotation-driven />
<!--静态资源放行-->
<mvc:default-servlet-handler />
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!--上传解析器-->
<!--文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean> </beans>
我们的基本配置就完成了
通过代码生成器生成最基本的功能
准备好相应的代码生成器插件,在pom.xml中已经导入成功
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--自定义代码生成器的路径-->
<!--<configurationFile>yourLocation/mybatis-generator-config.xml</configurationFile>-->
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
准备generatorConfig.xml,注意这个名字必须是这个名字
?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 自动生成器的配置(根目录,不做过多介绍)-->
<generatorConfiguration>
<!--
classPathEntry:可以配置多个,也不配置
数据库驱动:这里找到相应的驱动jar包就可以了(注:不同数据库的jar不一样)
location:里面的是路径(也可以直接写绝对路径 -> 如:E:\mybatis\mysql-connector-java-5.1.26-bin.jar)
-->
<classPathEntry location="F:\opensource\mysql-connector-java-5.1.26-bin.jar"/>
<!--
context:用于生成一组对象的环境(至少配置1个,可以配置多个)
id:表达唯一的名称
targetRuntime:用于指定生成的代码的运行环境(MyBatis3/MyBatis3Simple)
MyBatis3:默认值
MyBatis3Simple:不会生成与Example(案例)相关的方法
-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple" >
<!--
用于配置如果生成注释信息(最多可以配置一下)
suppressAllComments:阻止生成注释 ,默认为false
suppressDate:阻止生成的注释时间戳,默认为false
addRemarkComments:注释是否添加数据库表的备注信息,默认为false
-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--
配置连接数据库的基本信息
-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql:///crm"
userId="root" password="root">
</jdbcConnection>
<!--
用于指定JDBC类型和Java类型如何转换,最多可以配置一个
forceBigDecimals:控制是否强制将DECIMAL和NUMERIC类型的JDBC字段转换成Java类型的 BigDecimal
默认为false,一般不需要配置
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!--
javaModelGenerator:用来控制生成的实体类
targetPackage:生成Model类存放位置(包名)
targetProject:指定目标项目路径(根目录)
对应的子属性:
trimStrings:判断是否对数据库查询结果进行trim操作(默认false)
-->
<javaModelGenerator targetPackage="cn.itsource.crm.domain" targetProject="src/main/java">
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--
sqlMapGenerator:生成映射文件存放位置(Mapper.xml文件)
targetPackage:生成SQL映射文件(XML文件)在哪个包中
targetProject:指定目标项目路径(根目录)
-->
<sqlMapGenerator targetPackage="cn.itsource.crm.mapper" targetProject="src/main/resources">
</sqlMapGenerator> <!--
javaClientGenerator:Java客户端生成器(生成Dao/Mapper的接口)
该 标签可选(最多配置一个),如果不配置,就不会生成Mapper接口
type:选择客户端代码生成器
MyBatis3
ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件
MIXEDMAPPER:XML和注解混合形式
XMLMAPPER:所有方法都在XML中(接口调用依赖XML)
MyBatis3Simple
ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件
XMLMAPPER:所有方法都在XML中(接口调用依赖XML)
targetPackage:生成Mapper接口存放的包名
targetProject:指定目标项目路径
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.itsource.crm.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--
table:生成对应表及类名
tableName:对应表名(注:%代表所有)
domainObjectName:对应的类名
generatedKey:主键自增的id字段(针对当前 数据库配置MySQL)
-->
<table tableName="t_department" domainObjectName="Department">
<generatedKey column="id" sqlStatement="MySql" />
</table>
</context> </generatorConfiguration>
然后,我们就可以在idea中的右边菜单栏中的点击如图所示生成基础代码了
接下里就是三层的配置了
mapper层
BaseMapper
public interface BaseMapper<T> {
int deleteByPrimaryKey(Long id);
int insert(T record);
T selectByPrimaryKey(Long id);
List<T> selectAll();
int updateByPrimaryKey(T record);
}
子类继承父类的基本方法即可
DepartmentMapper
public interface DepartmentMapper extends BaseMapper<Department> { }
service层同理
IBaseService接口
ublic interface IBaseService<T> {
void save(T t);
void update(T t);
void delete(Long id);
T findOne(Long id);
List<T> findAll();
}
BaseService的实现
@Transactional
public class BaseServiceImpl<T> implements IBaseService<T> { @Autowired
private BaseMapper<T> baseMapper; @Override
public void save(T t) {
baseMapper.insert(t);
} @Override
public void update(T t) {
baseMapper.updateByPrimaryKey(t);
} @Override
public void delete(Long id) {
baseMapper.deleteByPrimaryKey(id);
} @Override
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public T findOne(Long id) {
return baseMapper.selectByPrimaryKey(id);
} @Override
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public List<T> findAll() {
return baseMapper.selectAll();
}
}
IDepartmentService
public interface IDepartmentService extends IBaseService<Department> { }
DepartmentServiceImpl
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
}
Controller层
Controller中的测试代码
@Controller
@RequestMapping("/department")
public class DepartmentController { @Autowired
private IDepartmentService departmentService; @RequestMapping("/index")
public String index(){
return "department/index";
} @RequestMapping("/list")
@ResponseBody
public List<Department> list(){
return departmentService.findAll();
}
}
准备EasyUI(上下文路径的配置)
引入easyui,只需要引入基本的就可以了
locale : 国际化支持的文件夹 themes :主题(eaayui的样式) jquery.easyui.min.js : easyui核心的js功能 jquery.min.js : jQuery的支持
配置上下文路径
dea中配置上下文路径 tomcat编辑 -> Deployment ->Application context
所有的请求都必需加上这个路径
方案一:在请求前 : ${pageContext.request.contextPath}
方案二:使用Base(建议的方案详情在下面的common中
准备common.jsp (/WEB-INF/views/common/common.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//获到上下文路径
String path = request.getContextPath();
//拼接相应的路径前缀: http://localhost:80/crm2/
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>"> <link rel="stylesheet" type="text/css" href="static/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="static/js/easyui/themes/icon.css">
<script type="text/javascript" src="static/js/jquery.min.js"></script>
<script type="text/javascript" src="static/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="static/js/easyui/locale/easyui-lang-zh_CN.js"></script>
完成CRUD
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%@include file="/WEB-INF/views/common/common.jsp" %>
<%--引入当前这个模块对应的js文件--%>
<script type="text/javascript" src="static/js/model/department.js"></script>
</head>
<body>
<!-- 准备grid控件 -->
<table id="departmentGrid"></table>
<!-- grid控件工具栏的按钮 -->
<div id="toolbar">
<a href="javascript:;" data-method="add" plain="true" class="easyui-linkbutton c1">添加</a>
<a href="javascript:;" data-method="update" plain="true" class="easyui-linkbutton c2">修改</a>
<a href="javascript:;" data-method="delete" plain="true" class="easyui-linkbutton c3">删除</a>
<a href="javascript:;" data-method="refresh" plain="true" class="easyui-linkbutton c4">刷新</a>
</div> <!--添加与修改的弹出框 -->
<div id="departmentDialog">
<form id="departmentForm" method="post">
<input type="hidden" name="id">
<table cellpadding="5">
<tr>
<td>编码:</td>
<td><input class="easyui-textbox" type="text" name="sn" data-options="required:true"></input></td>
</tr>
<tr>
<td>名称:</td>
<td><input class="easyui-textbox" type="text" name="name" data-options="required:true"></input></td>
</tr>
<tr>
<td>路径:</td>
<td><input class="easyui-textbox" type="text" name="dirpath" data-options="required:true"></input></td>
</tr>
<tr>
<td>状态:</td>
<td>
<select class="easyui-combobox" name="state" style="width:200px; " data-options="panelHeight:'auto'">
<option value="-1">作废</option>
<option value="0">正常</option>
<option value="1">异常</option>
</select>
</td>
</tr>
</table>
</form>
</div>
<div id="departmentDialogButtons">
<a href="javascript:;" data-method="save" plain="true" class="easyui-linkbutton c5">确定</a>
<a href="javascript:;" data-method="close" plain="true" class="easyui-linkbutton c6">取消</a>
</div> </body>
</html>
department.js 代码
$(function () { //wc公共的组件抽取
var departmentGrid = $('#departmentGrid');
var departmentDialog = $('#departmentDialog');
var departmentForm = $('#departmentForm'); //事情注册
$("*[data-method]").on("click",function () {
var methodName = $(this).data("method");
itsource[methodName]();
}) itsource = {
add(){
//居中并且打开
departmentDialog.dialog("center").dialog("open");
//保存每次打开,数据都是空的
departmentForm.form("clear");
},
update(){
//判断是否选中一行
var row = departmentGrid.datagrid("getSelected");
if(!row){
$.messager.alert('提示',"请选中后再进行操作!","info");
return ;
}
//居中并且打开
departmentDialog.dialog("center").dialog("open");
//保存每次打开,数据都是空的
departmentForm.form("clear");
//进行回显
departmentForm.form("load",row);
},
save(){
//提交表单
departmentForm.form('submit', {
url:"department/save",
onSubmit: function(){
return $(this).form('validate');
},
success:function(data){
//把它转成json
var result = JSON.parse(data);
if(result.success){
itsource.refresh();
itsource.close();
}else{
$.messager.alert('操作失败',result.msg,"error");
}
}
});
},
delete(){
//选中一行才可以删除
var row = departmentGrid.datagrid("getSelected");
if(!row){
$.messager.alert('提示',"请选中后再进行操作!","info");
return ;
}
//问:是否真的要删除?
$.messager.confirm('确认','您确认想要删除记录吗?',function(r){
if (r){
$.get("department/delete",{id:row.id},function (result) {
if(result.success){
itsource.refresh();
}else{
$.messager.alert('操作失败',result.msg,"error");
}
})
}
}); },
refresh(){
//刷新一次
departmentGrid.datagrid("reload");
},
close(){
departmentDialog.dialog("close");
}
}; //通过js创建咱们的grid
departmentGrid.datagrid({
fit:true,
fitColumns:true,
singleSelect:true,
url:'department/page',
toolbar:"#toolbar",
pageList:[5,10,20,30,40,100],
pagination:true,
columns:[[
{field:'sn',title:'编码',width:100},
{field:'name',title:'名称',width:100},
{field:'dirpath',title:'路径',width:100,align:'right'},
{field:'state',title:'状态',width:100,align:'right'}
]]
}); //通过js创建咱们的弹出框
departmentDialog.dialog({
title: '编辑数据',
// width: 400,
// height: 200,
closed: true, //默认关闭
modal: true,
buttons:"#departmentDialogButtons"
});
})
准备Query(完成分页)
BaseQuery(正好与easyui传的值对应上的),用于接收传入的分页值
public abstract class BaseQuery { private int page = 1; //默认是第一页
private int rows = 10; //默认每页10条数据 //getter与setter省略
}
public class DepartmentQuery extends BaseQuery {
}
修改departmentMapper.xml。添加一个新的方法
<select id="queryAll" parameterType="cn.itsource.crm.query.DepartmentQuery" resultMap="BaseResultMap" >
select id, sn, name, dirPath, state, manager_id, parent_id
from t_department
</select>
同样在BaseMapper中一样添加接口i
Page<T> queryAll(BaseQuery baseQuery);
修改applicationContext.xml
在pom.xml中引入响应的插件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.1</version>
</dependency>
在application.xml中配置拦截器和分页 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--XML的映射-->
<property name="mapperLocations" value="classpath:cn/itsource/crm/mapper/*.xml" />
<!--为所有相应的包中的类取别名-->
<property name="typeAliasesPackage" value="cn.itsource.crm.domain" />
<!--配置一个拦截器(分页插件):注意一下版本问题-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>helperDialect=mysql</value>
</property>
</bean>
</array>
</property>
</bean>
修改了service
创建了一个PageResult:直接返回Easyui中要的数据
public class PageResult<T> { private long total; //总条数 private List<T> rows; //这一行的数据 public PageResult(Page<T> page) {
this.total = page.getTotal();
this.rows = page.getResult();
}
//...
}
baseService中添加方法
@Override
public PageResult<T> queryAll(BaseQuery query) {
//配置分页的值
PageHelper.startPage(query.getPage(),query.getRows());
Page<T> page = baseMapper.queryAll(query);
return new PageResult<T>(page);
}
修改controller
@RequestMapping("/page")
@ResponseBody
public PageResult<Department> page(DepartmentQuery query){
return departmentService.queryAll(query);
}
这样我们就完成了基本的CRUD
多模块开发
- 我们开发的时候在追求:高内聚,低耦合
- 我们以前就是这么做的(Spring,接口,分层)
- 以前是根据包来进行分组 -> 改成模块
- 各个模块的耦合度更低,更加有利于组件(代码)的重用
模块划分
- basic-util:工具(不依赖于其它模块,但是其它模块都要依赖)
- basic-core:所有模块的公共代码部分
- crm-common:crm这个模块的公共代码部分
- crm-mapper:crm这个模块的持久层部分
- crm-service:crm这个模块的业务层
- crm-web:crm这个模块的web层
创建多模块的项目
- 父项目中怎么知道它有哪些子模块
- pom.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<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>cn.itsource</groupId>
<artifactId>maven-many-project</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version> <!--父模块中引入相应的子模块-->
<modules>
<module>basic-util</module>
<module>oa-service</module>
</modules> </project>
子模块中引入其它子模块
在当前项目的pom.xml中配置依赖的座标
<dependencies>
<dependency>
<groupId>cn.itsource</groupId>
<artifactId>basic-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>