一.SSM集成
1.1 创建项目
1.2 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.crm</groupId>
<artifactId>crm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>crm Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<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>
<org.springframework.version>4.2.5.RELEASE</org.springframework.version>
<org.hibernate.version>4.3.8.Final</org.hibernate.version>
<spring-data-jpa.version>1.9.0.RELEASE</spring-data-jpa.version>
<com.fasterxml.jackson.version>2.5.0</com.fasterxml.jackson.version>
</properties>
<dependencies>
<!-- Spring的支持包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</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-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<!-- 引入web前端的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- SpringMCV上传需要用到io包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 文件上传用到的包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- SpringMVC的json支持包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${com.fasterxml.jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${com.fasterxml.jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${com.fasterxml.jackson.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<!-- 測試包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!-- 这个scope 只能作用在编译和测试时,同时没有传递性。表示在运行的时候不添加此jar文件 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- 导入poi的支持-->
<!-- poi支持的jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
-->
<!--mybatis-->
<!--mybatis的核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<!--mybatis集成spring的框架-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<!--
日志包
-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!--mybatis的代码生成器的核心包-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!--
jar包导入
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.1</version>
</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>
<verbose>false</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.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.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</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>
1.3 项目基本结构图
注意:修改配置文件(根据自己项目的情况)
1.4 mapper层:
xml中的SQL是代码生成器生成的(自己写也可以)!
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> {
}
1.5 Service层
IBaseService
public interface IBaseService<T> {
void save(T t);
void update(T t);
void delete(Long id);
T findOne(Long id);
List<T> findAll();
}
BaseServiceImpl
package cn.itsource.crm.service.impl;
import cn.itsource.crm.mapper.BaseMapper;
import cn.itsource.crm.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
@Autowired
private BaseMapper<T> mapper;
@Override
@Transactional
public void save(T t) {
mapper.insert(t);
}
@Override
@Transactional
public void update(T t) {
mapper.updateByPrimaryKey(t);
}
@Override
@Transactional
public void delete(Long id) {
mapper.deleteByPrimaryKey(id);
}
@Override
public T findOne(Long id) {
return mapper.selectByPrimaryKey(id);
}
@Override
public List<T> findAll() {
return mapper.selectAll();
}
}
IDepartmentService
public interface IDepartmentService extends IBaseService<Department> {
}
DepartmentServiceImpl
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department>
implements IDepartmentService {
}
1.6 完成Controller
- 准备web.xml
- 基本展示出来即可!!
二 集成easyui
2.1 加入最小级别的easyui
2.2 上下文路径
配置了上下文路径
解决上下文路径的方案
- 方案一:
${pageContext.request.contextPath}
- 方案二: base标签
<%
String path = request.getContextPath();
// http://localhost:80/crm/
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%--所有访问前都会访问这个--%>
<base href="<%=basePath %>">
2.3 easyui的CRUD
2.4 准备了一个AjaxResult
三 分页
3.1 引入mybatis的分页插件
pom.xml中引入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.1</version>
</dependency>
applicationContext中加上插件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引入数据源-->
<property name="dataSource" ref="dataSource" />
<!--为咱们的domain取别名-->
<property name="typeAliasesPackage" value="cn.itsource.crm.domain" />
<!-- 引入mybatis的分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>helperDialect=mysql</value>
</property>
</bean>
</array>
</property>
<!--扫描所有映射文件-->
<property name="mapperLocations" value="classpath:cn/itsource/crm/mapper/*.xml" />
</bean>
3.2 准备Query
在
query
包中
- BaseQuery
public class BaseQuery {
//这两个名称是easyui支持的名称
private int page = 1; //当前页
private int rows = 10; //每页条数
//省略getter,setter
}
- DepartmentQuery
public class DepartmentQuery extends BaseQuery {
//查询条件
}
3.3 准备返回的PageResult
在
util
包中
/**
* Easyui需要的分页结果
* @param <T>
*/
public class PageResult<T> {
private long total; //总条数
private List<T> rows; //数据
public PageResult() {}
public PageResult(int total, List<T> rows) {
this.total = total;
this.rows = rows;
}
//省略getter,setter
}
3.4 mapper中准备查询
- BaseMapper
List<T> findByQuery(BaseQuery query);
- departmentMapper.xml
<!--查询的方法-->
<select id="findByQuery" parameterType="cn.itsource.crm.query.DepartmentQuery" resultMap="BaseResultMap" >
select id, sn, name, dirPath, state, manager_id, parent_id
from t_department
</select>
3.5 service完成功能
- IBaseService
PageResult<T> findByQuery(BaseQuery query);
- BaseServiceImpl
@Override
public PageResult<T> findByQuery(BaseQuery query) {
PageHelper.startPage(query.getPage(), query.getRows());
//拿到所有数据
Page<T> page = (Page<T>) mapper.selectAll();
//创建一个easyui能认识的分页对象
PageResult<T> pageResult = new PageResult<T>();
pageResult.setTotal(page.getTotal());
pageResult.setRows(page.getResult());
return pageResult;
}