博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring4+mybaits3整合—项目Demo
阅读量:5904 次
发布时间:2019-06-19

本文共 20960 字,大约阅读时间需要 69 分钟。

hot3.png

项目下载地址:

项目下载地址2:

这里就简单的粘一下部分重要代码,方便以后查看

一、框架简介

       这个demo采用目前最新的技术Spring4+Mybatis3+maven +freemarker+Bootstrap3构建的系统部分底层框架。目前以初具模型,可以直接用在项目上。

      系统运行环境:tomcat7+ 、JDK7+、MySql 5.5+

二、系统部分代码

      这里只粘贴部分代码,不做过多的解说,如果想了解具体内容请加QQ:864060165 详谈。

     1、web.xml

      

SSR Web Application
httpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
httpMethodFilter
Spring MVC Dispatcher Servlet
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
Spring MVC Dispatcher Servlet
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring-config.xml
Spring MVC Dispatcher Servlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
Spring MVC Dispatcher Servlet
/
30
index.jsp
2、Spring-config.xml

3、Spring-mvc.xml

json=application/json xml=application/xml
4、sqlMapConfig.xml

5、ecache配置

6、数据库映射文件

dic_id as dicId, dic_name as dicName, dic_value as dicValue, dic_group as dicGroup, dic_type as dicType, dic_order as dicOrder, dic_status as dicStatus, dic_parent_id as dicParentId
and dic_id = #{dicId}
and dic_name = #{dicName}
and dic_value = #{dicValue}
and dic_group = #{dicGroup}
and dic_type = #{dicType}
and dic_order = #{dicOrder}
and dic_status = #{dicStatus}
and dic_parent_id = #{dicParentId}
and dic_name like CONCAT("%",#{dicNameLike},"%")
and dic_group like CONCAT("%",#{dicGroupLike},"%")
order by ${sorting}
limit #{offset}, #{limit}
delete from sys_dictionary where dic_id = #{dicId}
delete from sys_dictionary
insert into sys_dictionary( dic_id, dic_name, dic_value, dic_group, dic_type, dic_order, dic_status, dic_parent_id ) values ( #{dicId}, #{dicName}, #{dicValue}, #{dicGroup}, #{dicType}, #{dicOrder}, #{dicStatus}, #{dicParentId} )
update sys_dictionary
dic_name = #{dicName},
dic_value = #{dicValue},
dic_group = #{dicGroup},
dic_type = #{dicType},
dic_order = #{dicOrder},
dic_status = #{dicStatus},
dic_parent_id = #{dicParentId},
where dic_id = #{dicId}
update sys_dictionary set dic_name = #{dicName}, dic_value = #{dicValue}, dic_group = #{dicGroup}, dic_type = #{dicType}, dic_order = #{dicOrder}, dic_status = #{dicStatus}, dic_parent_id = #{dicParentId} where dic_id = #{dicId}
7、Mybatis通用BaseDao实现

package com.viathink.frame.core.dao.impl;import java.util.List;import java.util.Map;import org.apache.commons.lang3.StringUtils;import org.apache.ibatis.session.RowBounds;import org.apache.ibatis.session.SqlSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageImpl;import org.springframework.data.domain.Pageable;import org.springframework.transaction.annotation.Transactional;import org.springframework.util.Assert;import com.viathink.frame.core.dao.BaseDao;import com.viathink.frame.core.dao.constants.SqlId;import com.viathink.frame.core.dao.domain.Identifiable;import com.viathink.frame.core.exception.DaoException;import com.viathink.frame.core.utils.BeanUtils;import com.viathink.frame.core.utils.UUIDUtils;/** * 基础Dao接口实现类,实现改类的子类必须设置泛型类型 * @author LiuJunGuang * @date 2014年3月3日下午1:02:31 */public abstract class BaseDaoImpl
implements BaseDao
{ @Autowired(required = true) protected SqlSession sqlSessionTemplate; public static final String SQLNAME_SEPARATOR = "."; /** * @fields sqlNamespace SqlMapping命名空间 */ private String sqlNamespace = getDefaultSqlNamespace(); /** * 获取泛型类型的实体对象类全名 * @author LiuJunGuang * @return * @date 2014年3月3日下午6:17:46 */ protected String getDefaultSqlNamespace() { Class
genericClass = BeanUtils.getGenericClass(this.getClass()); return genericClass == null ? null : genericClass.getName(); } /** * 获取SqlMapping命名空间 * @author LiuJunGuang * @return SqlMapping命名空间 * @date 2014年3月4日上午12:33:15 */ public String getSqlNamespace() { return sqlNamespace; } /** * 设置SqlMapping命名空间。 以改变默认的SqlMapping命名空间, * 不能滥用此方法随意改变SqlMapping命名空间。 * @author LiuJunGuang * @param sqlNamespace SqlMapping命名空间 * @date 2014年3月4日上午12:33:17 */ public void setSqlNamespace(String sqlNamespace) { this.sqlNamespace = sqlNamespace; } /** * 将SqlMapping命名空间与给定的SqlMapping名组合在一起。 * @param sqlName SqlMapping名 * @return 组合了SqlMapping命名空间后的完整SqlMapping名 */ protected String getSqlName(String sqlName) { return sqlNamespace + SQLNAME_SEPARATOR + sqlName; } /** * 生成主键值。 默认使用{@link UUIDUtils#create()}方法 * 如果需要生成主键,需要由子类重写此方法根据需要的方式生成主键值。 * @param entity 要持久化的对象 */ protected String generateId() { return UUIDUtils.create(); } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectOne(java.io.Serializable) */ @Override public
V selectOne(T query) { Assert.notNull(query); try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT), params); } catch (Exception e) { throw new DaoException(String.format("查询一条记录出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectById(java.io.Serializable) */ @Override public
V selectById(String id) { Assert.notNull(id); try { return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_BY_ID), id); } catch (Exception e) { throw new DaoException(String.format("根据ID查询对象出错!语句:%s", getSqlName(SqlId.SQL_SELECT_BY_ID)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectList(java.io.Serializable) */ @Override public
List
selectList(T query) { try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), params); } catch (Exception e) { throw new DaoException(String.format("查询对象列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectAll() */ @Override public
List
selectAll() { try { return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT)); } catch (Exception e) { throw new DaoException(String.format("查询所有对象列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectMap(java.io.Serializable, java.lang.String) */ @Override public
Map
selectMap(T query, String mapKey) { Assert.notNull(mapKey, "[mapKey] - must not be null!"); try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.selectMap(getSqlName(SqlId.SQL_SELECT), params, mapKey); } catch (Exception e) { throw new DaoException(String.format("查询对象Map时出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /** * 设置分页 * @param pageInfo 分页信息 * @return SQL分页参数对象 */ protected RowBounds getRowBounds(Pageable pageable) { RowBounds bounds = RowBounds.DEFAULT; if (null != pageable) { bounds = new RowBounds(pageable.getOffset(), pageable.getPageSize()); } return bounds; } /** * 获取分页查询参数 * @param query 查询对象 * @param pageable 分页对象 * @return Map 查询参数 */ protected Map
getParams(T query, Pageable pageable) { Map
params = BeanUtils.toMap(query, getRowBounds(pageable)); if (pageable != null && pageable.getSort() != null) { String sorting = pageable.getSort().toString(); params.put("sorting", sorting.replace(":", "")); } return params; } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectList(com.viathink.core.dao.domain.Identifiable, org.springframework.data.domain.Pageable) */ @Override public
List
selectList(T query, Pageable pageable) { try { return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable)); } catch (Exception e) { throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectPageList(com.viathink.core.dao.domain.Identifiable, org.springframework.data.domain.Pageable) */ @Override public
Page
selectPageList(T query, Pageable pageable) { try { List
contentList = sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable)); return new PageImpl
(contentList, pageable, this.selectCount(query)); } catch (Exception e) { throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectMap(com.viathink.core.dao.domain.Identifiable, java.lang.String, org.springframework.data.domain.Pageable) */ @Override public
Map
selectMap(T query, String mapKey, Pageable pageable) { try { return sqlSessionTemplate.selectMap(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable), mapKey); } catch (Exception e) { throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectCount() */ @Override public Long selectCount() { try { return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT)); } catch (Exception e) { throw new DaoException(String.format("查询对象总数出错!语句:%s", getSqlName(SqlId.SQL_SELECT_COUNT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectCount(java.io.Serializable) */ @Override public Long selectCount(T query) { try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT), params); } catch (Exception e) { throw new DaoException(String.format("查询对象总数出错!语句:%s", getSqlName(SqlId.SQL_SELECT_COUNT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#insert(java.io.Serializable) */ @Override public void insert(T entity) { Assert.notNull(entity); try { if (StringUtils.isBlank(entity.getId())) entity.setId(generateId()); sqlSessionTemplate.insert(getSqlName(SqlId.SQL_INSERT), entity); } catch (Exception e) { throw new DaoException(String.format("添加对象出错!语句:%s", getSqlName(SqlId.SQL_INSERT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#delete(java.io.Serializable) */ @Override public int delete(T query) { Assert.notNull(query); try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE), params); } catch (Exception e) { throw new DaoException(String.format("删除对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#deleteById(java.io.Serializable) */ @Override public int deleteById(String id) { Assert.notNull(id); try { return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE_BY_ID), id); } catch (Exception e) { throw new DaoException(String.format("根据ID删除对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE_BY_ID)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#deleteAll() */ @Override public int deleteAll() { try { return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE)); } catch (Exception e) { throw new DaoException(String.format("删除所有对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#updateById(java.io.Serializable) */ @Override public int updateById(T entity) { Assert.notNull(entity); try { return sqlSessionTemplate.update(getSqlName(SqlId.SQL_UPDATE_BY_ID), entity); } catch (Exception e) { throw new DaoException(String.format("根据ID更新对象出错!语句:%s", getSqlName(SqlId.SQL_UPDATE_BY_ID)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#updateByIdSelective(java.io.Serializable) */ @Override @Transactional public int updateByIdSelective(T entity) { Assert.notNull(entity); try { return sqlSessionTemplate.update(getSqlName(SqlId.SQL_UPDATE_BY_ID_SELECTIVE), entity); } catch (Exception e) { throw new DaoException(String.format("根据ID更新对象某些属性出错!语句:%s", getSqlName(SqlId.SQL_UPDATE_BY_ID_SELECTIVE)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#deleteByIdInBatch(java.util.List) */ @Override @Transactional public void deleteByIdInBatch(List
idList) { if (idList == null || idList.isEmpty()) return; for (String id : idList) { this.deleteById(id); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#updateInBatch(java.util.List) */ @Override @Transactional public void updateInBatch(List
entityList) { if (entityList == null || entityList.isEmpty()) return; for (T entity : entityList) { this.updateByIdSelective(entity); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#insertInBatch(java.util.List) */ @Override public void insertInBatch(List
entityList) { if (entityList == null || entityList.isEmpty()) return; for (T entity : entityList) { this.insert(entity); } }}
9、通用BaseControllerImpl实现

package com.viathink.frame.core.web.controller;import java.util.Arrays;import org.apache.commons.lang3.ArrayUtils;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.web.PageableDefault;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.viathink.frame.core.dao.BaseService;import com.viathink.frame.core.dao.domain.Identifiable;import com.viathink.frame.core.web.domain.ControllerPath;import com.viathink.frame.core.web.domain.Result;import com.viathink.frame.core.web.domain.Result.Status;/** * 基础控制器接口实现类 * @author LiuJunGuang * @date 2014年3月5日下午12:03:13 */public abstract class BaseControllerImpl
implements BaseController
{ private Logger log = LoggerFactory.getLogger(BaseControllerImpl.class); /** * @fields path 页面路径信息 */ protected ControllerPath path = new ControllerPath(this.getClass()); /** * 获取基础的服务 * @return BaseService */ protected abstract BaseService
getBaseService(); @Override @ResponseBody @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) public Result deleteList(String[] ids) { if (ArrayUtils.isEmpty(ids)) { log.error("未设置批量删除对象的ID号!对象:{}", path.getEntityName()); return new Result(Status.ERROR, "没有传入要删除的ID号数组!"); } try { getBaseService().deleteByIdInBatch(Arrays.asList(ids)); } catch (Exception e) { log.error("批量删除对象失败!对象:" + path.getEntityName(), e); return new Result(Status.ERROR, "批量删除失败!"); } return new Result(Status.OK, ids.length); } @Override @ResponseBody @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) public Result deleteOne(@PathVariable("id") String id) { if (StringUtils.isBlank(id)) { log.error("要删除的ID号为null或空字符串!对象:{}", path.getEntityName()); return new Result(Status.ERROR, "没有传入要删除的ID号!"); } int count = getBaseService().deleteById(id); if (count == 0) return new Result(Status.ERROR, "要删除的记录不存在!"); log.debug("成功删除{}个对象,id:{},对象:{}", count, id, path.getEntityName()); return new Result(Status.OK, count); } @Override @RequestMapping(method = RequestMethod.POST) public ModelAndView addOne(T entity) { getBaseService().insert(entity); return new ModelAndView(path.getRedirectListPath()); } @Override @RequestMapping(value = "/add", method = RequestMethod.GET) public ModelAndView addView() { return new ModelAndView(path.getAddViewPath()); } @Override @RequestMapping(method = RequestMethod.GET) public ModelAndView selectList(Q query, @PageableDefault Pageable pageable) { Page
page = getBaseService().queryPageList(query, pageable); ModelAndView mav = new ModelAndView(path.getListViewPath(), "page", page); mav.addObject("query", query); return mav; } @Override @RequestMapping(value = "/{id}", method = RequestMethod.GET) public ModelAndView viewOne(@PathVariable("id") String id) { Object obj = getBaseService().queryById(id); return new ModelAndView(path.getOneViewPath(), path.getEntityName(), obj); } @Override @ResponseBody @RequestMapping(method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) public Result editOne(T entity) { getBaseService().updateById(entity); return new Result(Status.OK, entity); } @Override @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) public ModelAndView editView(@PathVariable("id") String id) { Object obj = getBaseService().queryById(id); return new ModelAndView(path.getEditViewPath(), path.getEntityName(), obj); }}
10、列表结果页面:

<#include "/common/common.ftl"><@pnotify/>字典列表	<#include "/common/navbar.ftl"> 	<#-- 内容开始 -->	
<#-- 查询条件 -->
查询条件
名称
<#-- 结果列表 -->
字典列表
<#list page.content as result>
序号 名称 类型 排序 状态 操作
${result_index+1} ${result.dicName} ${result.dicValue} ${result.dicGroup} ${result.dicType.label} ${result.dicOrder} ${result.dicStatus.label}
<@tablePage/>
<#-- 内容结束 --> <#include "/common/footer.ftl"> <@modal/>
11、编辑页面:

<#assign base=request.contextPath />	
三、运行结果图:

项目下载地址:

项目下载地址2:

转载于:https://my.oschina.net/java2010/blog/356421

你可能感兴趣的文章
我的友情链接
查看>>
CDN相关
查看>>
Tomcat的设置4——Tomcat的体系结构与设置基于端口号的虚拟主机
查看>>
三种判断端口存活的方法和链接200的判断方法
查看>>
我的友情链接
查看>>
ftp协议基础
查看>>
顺时针打印矩阵
查看>>
访问共享经常中断
查看>>
人生的交易
查看>>
MySql
查看>>
算法分析与设计——贪心法实验报告
查看>>
js时间戳与日期格式的相互转换
查看>>
POJ - 1062 昂贵的聘礼(Dijkstra)
查看>>
Java多态和动态绑定是如何实现的
查看>>
sql server 下载安装标记
查看>>
Android学习6—单元测试的使用
查看>>
js运算符(运算符的结合性)
查看>>
最长上升子序列问题
查看>>
C#中的析构函数
查看>>
idea 编译级别的设置
查看>>