智能商贸项目-基本框架
一、后台支持
(一)Dao层
import org. springframework. data. jpa. repository. JpaRepository;
import org. springframework. data. jpa. repository. JpaSpecificationExecutor;
import org. springframework. data. repository. NoRepositoryBean;
import java. io. Serializable;
@NoRepositoryBean
public interface BaseRepository < T, ID extends Serializable > extends JpaRepository < T, ID> , JpaSpecificationExecutor {
}
public interface EmployeeRepository extends BaseRepository < Employee, Long> {
}
(二)Service层
1.创建父接口
import com. lty. aisell. query. BaseQuery;
import org. springframework. data. domain. Page;
import java. io. Serializable;
import java. util. List;
public interface IBaseService < T, ID extends Serializable > {
void save ( T t) ;
void delete ( ID id) ;
T findOne ( ID id) ;
List< T> findAll ( ) ;
List< T> queryAll ( BaseQuery query) ;
Page< T> queryPage ( BaseQuery query) ;
List< Object> queryByJpql ( String jpql, Object. . . params) ;
}
2.创建父接口的实现类
import com. lty. aisell. query. BaseQuery;
import com. lty. aisell. repository. BaseRepository;
import com. lty. aisell. service. IBaseService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. data. domain. Page;
import org. springframework. data. domain. PageRequest;
import org. springframework. data. domain. Pageable;
import org. springframework. data. domain. Sort;
import org. springframework. data. jpa. domain. Specification;
import org. springframework. transaction. annotation. Propagation;
import org. springframework. transaction. annotation. Transactional;
import javax. persistence. EntityManager;
import javax. persistence. PersistenceContext;
import javax. persistence. Query;
import java. io. Serializable;
import java. util. List;
@Transactional ( readOnly = true , propagation = Propagation. SUPPORTS)
public abstract class BaseServiceImpl < T, ID extends Serializable > implements IBaseService < T, ID> {
@Autowired
private BaseRepository< T, ID> baseRepository;
@PersistenceContext
EntityManager entityManager;
@Override
@Transactional
public void save ( T t) {
baseRepository. save ( t) ;
}
@Override
@Transactional
public void delete ( ID id) {
baseRepository. delete ( id) ;
}
@Override
public T findOne ( ID id) {
return baseRepository. findOne ( id) ;
}
@Override
public List< T> findAll ( ) {
return baseRepository. findAll ( ) ;
}
@Override
public List< T> queryAll ( BaseQuery query) {
Specification spec = query. createSpec ( ) ;
return baseRepository. findAll ( spec) ;
}
@Override
public Page< T> queryPage ( BaseQuery query) {
Sort sort = query. createSort ( ) ;
Pageable pageable = new PageRequest ( query. getJPACurrentPage ( ) , query. getPageSize ( ) , sort) ;
Specification spec = query. createSpec ( ) ;
return baseRepository. findAll ( spec, pageable) ;
}
@Override
public List< Object> queryByJpql ( String jpql, Object. . . params) {
Query query = entityManager. createQuery ( jpql) ;
for ( int i = 0 ; i < params. length ; i++ ) {
query. setParameter ( i+ 1 , params[ i] ) ;
}
return query. getResultList ( ) ;
}
}
3.创建子接口
import com. lty. aisell. domain. Employee;
public interface IEmployeeService extends IBaseService < Employee, Long> {
}
4.创建子接口的实现类
import com. lty. aisell. domain. Employee;
import com. lty. aisell. service. IEmployeeService;
import org. springframework. stereotype. Service;
@Service
public class EmployeeServiceImpl extends BaseServiceImpl < Employee, Long> implements IEmployeeService {
}
二、集成SpringMVC
1.配置applicationContext-mvc.xml
< 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
" >
< context: component-scan base-package = " com.lty.aisell.controller" > context: component-scan>
< mvc: annotation-driven> mvc: annotation-driven>
< mvc: default-servlet-handler> mvc: default-servlet-handler>
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/views/" > property>
< property name = " suffix" value = " .jsp" > property>
bean>
< bean id = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" >
< property name = " maxUploadSize" >
< value> 10485760 value>
property>
bean>
beans>
2.配置web.xml
< 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"
id = " WebApp_ID" version = " 3.1" >
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener listener-class>
listener>
< context-param>
< param-name> contextConfigLocation param-name>
< param-value> classpath:applicationContext.xml param-value>
context-param>
< servlet>
< servlet-name> dispatcherServlet 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> dispatcherServlet servlet-name>
< url-pattern> / url-pattern>
servlet-mapping>
< 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>
web-app>
3.Controller层
import com. lty. aisell. common. JsonResult;
import com. lty. aisell. common. UIPage;
import com. lty. aisell. domain. Employee;
import com. lty. aisell. query. EmployeeQuery;
import com. lty. aisell. service. IEmployeeService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. data. domain. Page;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. ResponseBody;
import java. util. List;
@Controller
@RequestMapping ( "/employee" )
public class EmployeeController {
@Autowired
IEmployeeService employeeService;
@RequestMapping ( "/index" )
public String index ( ) {
return "employee/index" ;
}
@RequestMapping ( "/list" )
@ResponseBody
public List< Employee> list ( ) {
List< Employee> list = employeeService. findAll ( ) ;
return list;
}
@RequestMapping ( "/page" )
@ResponseBody
public UIPage< Employee> page ( EmployeeQuery query) {
Page< Employee> page = employeeService. queryPage ( query) ;
return new UIPage < Employee> ( page) ;
}
}
三、集成EasyUI
(一)把相应的文件拷备到项目中
(二)抽取引入easyui的代码
把所有公共的引入(css,js) 都放在head.jsp中
以后要使用easyui直接引入即可
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 引入easyui的样式 --%>
<%-- 引入easyui的图标样式 --%>
<%-- 引入jQuery --%>
<%--jQuery的扩展包 --%>
<%--引入easyui的核心文件--%>
<%-- 引入一个国际化(中文)文件 --%>
(三)员工的查询页面
1.employee.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入头部文件--%>
<%@include file="/WEB-INF/views/head.jsp"%>
<%--引入当前模块对应的js--%>
<%--工具栏--%>
<%--
fitColumns : 自适应列
fit : 自适应父容器
singleSelect :单选
pagination : 分页栏
url : 获取数据的路径
--%>
2.Page对象封装
前台要集成Easyui,SpringDataJpa返回的数据和EasyUI中的数据匹配不上
Easyui在分页时想要的数据结构{total:xx,rows:[...]}
准备UIPage
来匹配这个结构
import org. springframework. data. domain. Page;
import java. util. ArrayList;
import java. util. List;
public class UIPage < T> {
private Long total;
private List< T> rows = new ArrayList < > ( ) ;
public UIPage ( Page page) {
this . total = page. getTotalElements ( ) ;
this . rows = page. getContent ( ) ;
}
}
3.修改抽取的查询对象
EasyUI查询传参的名称如果和BaseQuery中接收的名称不一致,需要修改BaseQuery这个类进行功能的兼容
import org. apache. commons. lang3. StringUtils;
import org. springframework. data. domain. Sort;
import org. springframework. data. jpa. domain. Specification;
public abstract class BaseQuery {
private int currentPage = 1 ;
private int pageSize = 10 ;
private String orderName;
private boolean orderType = false ;
public abstract Specification createSpec ( ) ;
public int getCurrentPage ( ) {
return currentPage;
}
public int getJPACurrentPage ( ) {
return currentPage- 1 ;
}
public void setCurrentPage ( int currentPage) {
this . currentPage = currentPage;
}
public void setPage ( int page) {
this . currentPage = page;
}
public int getPageSize ( ) {
return pageSize;
}
public void setPageSize ( int pageSize) {
this . pageSize = pageSize;
}
public void setRows ( int rows) {
this . pageSize = rows;
}
}
4.当前模块对应的js
引入jquery.serializejson.js这个控件,可以直接拿到一个表单中的所有值,并且封装成一个json数据
$ ( function ( ) {
var datagrid = $ ( "#datagrid" ) ;
var searchForm = $ ( "#searchForm" )
$ ( "a[data-method]" ) . on ( "click" , function ( ) {
var methodName = $ ( this ) . data ( "method" ) ;
lty[ methodName] ( ) ;
} )
lty = {
add ( ) {
} ,
update ( ) {
} ,
delete ( ) {
} ,
search: function ( ) {
var params = searchForm. serializeObject ( ) ;
datagrid. datagrid ( "load" , params) ;
}
}
} )