spring4+mybaits3整合—项目Demo


一、框架简介

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

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

二、系统部分代码

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

     1、web.xml

      

[java] view plain copy
  1. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  3.     version="3.0">  
  4.     <display-name>SSR Web Application</display-name>  
  5.   
  6.     <!-- Enables use of HTTP methods PUT and DELETE,默认参数名: _method -->  
  7.     <filter>  
  8.         <filter-name>httpMethodFilter</filter-name>  
  9.         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  10.     </filter>  
  11.   
  12.     <filter-mapping>  
  13.         <filter-name>httpMethodFilter</filter-name>  
  14.         <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>  
  15.     </filter-mapping>  
  16.   
  17.     <!-- 字符编码过滤器 -->  
  18.     <filter>  
  19.         <filter-name>encodingFilter</filter-name>  
  20.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  21.         <init-param>  
  22.             <param-name>encoding</param-name>  
  23.             <param-value>UTF-8</param-value>  
  24.         </init-param>  
  25.         <init-param>  
  26.             <param-name>forceEncoding</param-name>  
  27.             <param-value>true</param-value>  
  28.         </init-param>  
  29.     </filter>  
  30.       
  31.     <filter-mapping>  
  32.         <filter-name>encodingFilter</filter-name>  
  33.         <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>  
  34.     </filter-mapping>  
  35.       
  36.     <!-- Spring 容器加载 -->  
  37.     <listener>  
  38.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  39.     </listener>  
  40.     <context-param>  
  41.         <param-name>contextConfigLocation</param-name>  
  42.         <param-value>classpath:spring-config.xml</param-value>  
  43.     </context-param>  
  44.       
  45.     <!-- The front controller of this Spring Web application, responsible for handling all application requests -->  
  46.     <servlet>  
  47.         <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>  
  48.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  49.         <init-param>  
  50.             <param-name>contextConfigLocation</param-name>  
  51.             <param-value>classpath:spring-mvc.xml</param-value>  
  52.         </init-param>  
  53.         <load-on-startup>1</load-on-startup>  
  54.     </servlet>  
  55.           
  56.     <!-- Map all *.spring requests to the DispatcherServlet for handling -->  
  57.     <servlet-mapping>  
  58.         <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>  
  59.         <url-pattern>/</url-pattern>  
  60.     </servlet-mapping>  
  61.       
  62.     <session-config>  
  63.         <session-timeout>30</session-timeout>  
  64.     </session-config>  
  65.   
  66.       
  67.     <!-- 欢迎页面 -->  
  68.     <welcome-file-list>  
  69.         <welcome-file>index.jsp</welcome-file>  
  70.     </welcome-file-list>  
  71.   
  72. </web-app>  
2、Spring-config.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:jpa="http://www.springframework.org/schema/data/jpa"  
  6.     xmlns:security="http://www.springframework.org/schema/security"  
  7.     xsi:schemaLocation="  
  8.         http://www.springframework.org/schema/aop   
  9.         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  10.         http://www.springframework.org/schema/security   
  11.         http://www.springframework.org/schema/security/spring-security-3.2.xsd  
  12.         http://www.springframework.org/schema/beans   
  13.         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  14.         http://www.springframework.org/schema/data/jpa   
  15.         http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
  16.         http://www.springframework.org/schema/tx   
  17.         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
  18.         http://www.springframework.org/schema/context   
  19.         http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  20.   
  21.   
  22.     <!-- Activates annotation-based bean configuration -->  
  23.     <context:annotation-config />  
  24.   
  25.     <!-- Scans for application @Components to deploy -->  
  26.     <context:component-scan base-package="com.viathink" />  
  27.   
  28.     <!-- 数据库配置文件位置 -->  
  29.     <context:property-placeholder location="classpath:/jdbc.properties" />  
  30.     <!-- 配置dbcp数据源 -->  
  31.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  32.         destroy-method="close">  
  33.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  34.         <property name="url" value="${jdbc.url}" />  
  35.         <property name="username" value="${jdbc.username}" />  
  36.         <property name="password" value="${jdbc.password}" />  
  37.         <property name="minIdle" value="${jdbc.minIdle}" />        <!-- 队列中的最小等待数 -->  
  38.         <property name="maxIdle" value="${jdbc.maxIdle}" />        <!-- 队列中的最大等待数 -->  
  39.         <property name="maxWait" value="${jdbc.maxWait}" />        <!-- 最长等待时间,单位毫秒 -->  
  40.         <property name="maxActive" value="${jdbc.maxActive}" />    <!-- 最大活跃数 -->  
  41.         <property name="initialSize" value="${jdbc.initialSize}" /><!--  
  42.             初始大小 -->  
  43.     </bean>  
  44.   
  45.     <!-- 使用JDBC事物 -->  
  46.     <bean id="transactionManager"  
  47.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  48.         <property name="dataSource" ref="dataSource" />  
  49.     </bean>  
  50.   
  51.     <!-- AOP配置事物 -->  
  52.     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">  
  53.         <tx:attributes>  
  54.             <tx:method name="query*" read-only="true" propagation="REQUIRED" />  
  55.             <tx:method name="delete*"  propagation="REQUIRED" />  
  56.             <tx:method name="update*" propagation="REQUIRED" />  
  57.             <tx:method name="insert*" propagation="REQUIRED" />  
  58.             <tx:method name="*" propagation="REQUIRED" />  
  59.         </tx:attributes>  
  60.     </tx:advice>  
  61.       
  62.     <!-- 配置AOP切面 -->  
  63.     <aop:config>  
  64.         <aop:pointcut id="transactionPointcut" expression="execution(* com.viathink..service.*.*(..))"/>  
  65.         <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/>  
  66.     </aop:config>  
  67.       
  68.   
  69.     <!-- 使用annotation注解方式配置事务 -->  
  70.     <tx:annotation-driven transaction-manager="transactionManager" />  
  71.   
  72.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  73.         <property name="dataSource" ref="dataSource" />  
  74.         <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>  
  75.         <property name="mapperLocations" value="classpath:mapper/mysql/**/*Mapper.xml"></property>  
  76.     </bean>  
  77.   
  78.     <!-- 配置SQLSession模板 -->  
  79.     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
  80.         <constructor-arg index="0" ref="sqlSessionFactory" />  
  81.     </bean>  
  82.   
  83. </beans>  

3、Spring-mvc.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="  
  5.     http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  7.     http://www.springframework.org/schema/context   
  8.     http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  9.     http://www.springframework.org/schema/mvc    
  10.     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  11.   
  12.   
  13.     <!-- XML转码器 -->  
  14.     <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />  
  15.   
  16.     <!-- 配置freeMarker的模板路径 -->  
  17.     <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  18.         <property name="configLocation" value="classpath:freemarker.properties" />  
  19.         <property name="templateLoaderPath" value="/WEB-INF/ftl/" />  
  20.         <property name="freemarkerVariables">  
  21.             <map>  
  22.                 <entry key="xml_escape" value-ref="fmXmlEscape" />  
  23.             </map>  
  24.         </property>  
  25.     </bean>  
  26.   
  27.     <!-- 配置freeMarker视图解析器 -->  
  28.     <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  29.         <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />  
  30.         <property name="contentType" value="text/html; charset=utf-8" />  
  31.         <property name="cache" value="true" />  
  32.         <property name="suffix" value=".ftl" />  
  33.         <property name="order" value="1" />  
  34.   
  35.         <property name="requestContextAttribute" value="request" />  
  36.         <property name="exposeRequestAttributes" value="true" />  
  37.         <property name="exposeSessionAttributes" value="true" />  
  38.         <property name="exposeSpringMacroHelpers" value="true" />  
  39.     </bean>  
  40.   
  41.   
  42.     <!-- jsp视图解析器 -->  
  43.     <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  44.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
  45.         <property name="prefix" value="/WEB-INF/jsp/" />  
  46.         <property name="suffix" value=".jsp" />  
  47.         <property name="order" value="2" />  
  48.     </bean>  
  49.       
  50.     <!-- 扫描控制器类 -->  
  51.     <context:component-scan base-package="com.viathink/**/web/controller/**" />  
  52.       
  53.     <!-- 配置静态资源 -->  
  54.     <mvc:resources location="/resources/" mapping="/resources/**" />  
  55.   
  56.     <!-- 采用注解方式配置MVC -->  
  57.     <mvc:annotation-driven  content-negotiation-manager="contentNegotiationManager">  
  58.         <mvc:argument-resolvers>  
  59.             <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver" />  
  60.         </mvc:argument-resolvers>  
  61.     </mvc:annotation-driven>  
  62.       
  63.     <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">  
  64.     <property name="favorPathExtension" value="true" />  
  65.     <property name="favorParameter" value="true" />  
  66.     <property name="defaultContentType" value="text/html"/>  
  67.     <property name="mediaTypes" >  
  68.         <value>  
  69.             json=application/json  
  70.             xml=application/xml  
  71.         </value>  
  72.     </property>  
  73. </bean>  
  74.   
  75. </beans>  
  76.       
  77.       

4、sqlMapConfig.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  3. <configuration>  
  4.     <settings>  
  5.         <setting name="cacheEnabled" value="true" />  
  6.         <setting name="lazyLoadingEnabled" value="true" />  
  7.         <setting name="multipleResultSetsEnabled" value="true" />  
  8.         <setting name="useColumnLabel" value="true" />  
  9.         <setting name="defaultExecutorType" value="REUSE" />  
  10.         <setting name="defaultStatementTimeout" value="25000" />  
  11.     </settings>  
  12.       
  13. </configuration>  

5、ecache配置

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">  
  3.     <!-- Sets the path to the directory where cache .data files are created.  
  4.   
  5.         If the path is a Java System Property it is replaced by  
  6.         its value in the running VM.  
  7.   
  8.         The following properties are translated:  
  9.         user.home - User's home directory  
  10.         user.dir - User's current working directory  
  11.         java.io.tmpdir - Default temp file path -->  
  12.     <diskStore path="java.io.tmpdir" />  
  13.     <defaultCache   
  14.             maxElementsInMemory="10000"   
  15.             maxElementsOnDisk="10000000"   
  16.             eternal="false"   
  17.             overflowToDisk="true"   
  18.             timeToIdleSeconds="120"   
  19.             timeToLiveSeconds="120"   
  20.             diskPersistent="false"   
  21.             diskExpiryThreadIntervalSeconds="120"   
  22.             memoryStoreEvictionPolicy="LRU"   
  23.             />   
  24.               
  25.     <!--  
  26.     name:Cache的唯一标识  
  27.     maxElementsInMemory:内存中最大缓存对象数  
  28.     maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大  
  29.     eternal:Element是否永久有效,一但设置了,timeout将不起作用  
  30.     overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中  
  31.     timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大  
  32.     timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大   
  33.     diskPersistent:是否缓存虚拟机重启期数据  
  34.     diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒  
  35.     diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区  
  36.     memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)   
  37.     -->  
  38.   
  39. </ehcache>  

6、数据库映射文件

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
  3. <mapper namespace="com.viathink.sys.domain.SysDictionary">  
  4.     <cache type="org.mybatis.caches.ehcache.LoggingEhcache" readOnly="true"/>  
  5.   
  6.     <resultMap id="BaseResultMap" type="com.viathink.sys.domain.vo.SysDictionaryVo">  
  7.         <id column="dic_id" property="dicId" jdbcType="VARCHAR" />  
  8.         <result column="dic_name" property="dicName" jdbcType="VARCHAR" />  
  9.         <result column="dic_value" property="dicValue" jdbcType="VARCHAR" />  
  10.         <result column="dic_group" property="dicGroup" jdbcType="VARCHAR" />  
  11.         <result column="dic_type" property="dicType" jdbcType="VARCHAR" />  
  12.         <result column="dic_order" property="dicOrder" jdbcType="INTEGER" />  
  13.         <result column="dic_status" property="dicStatus" jdbcType="VARCHAR" />  
  14.         <result column="dic_parent_id" property="dicParentId" jdbcType="VARCHAR" />  
  15.     </resultMap>  
  16.       
  17.     <sql id="Base_Column_List">  
  18.         dic_id as dicId,  
  19.         dic_name as dicName,  
  20.         dic_value as dicValue,   
  21.         dic_group as dicGroup,   
  22.         dic_type as dicType,  
  23.         dic_order as dicOrder,  
  24.         dic_status as dicStatus,  
  25.         dic_parent_id as dicParentId  
  26.     </sql>  
  27.       
  28.       
  29.     <sql id="Base_Where_Clause">  
  30.         <where>  
  31.             <trim prefixOverrides="and">  
  32.                 <if test="dicId != null"> and dic_id = #{dicId}</if>  
  33.                 <if test="dicName != null">and dic_name = #{dicName}</if>  
  34.                 <if test="dicValue != null">and dic_value = #{dicValue}</if>  
  35.                 <if test="dicGroup != null">and dic_group = #{dicGroup}</if>  
  36.                 <if test="dicType != null">and dic_type = #{dicType}</if>  
  37.                 <if test="dicOrder != null">and dic_order = #{dicOrder}</if>  
  38.                 <if test="dicStatus != null">and dic_status = #{dicStatus}</if>  
  39.                 <if test="dicParentId != null">and dic_parent_id = #{dicParentId}</if>  
  40.                   
  41.                 <!-- 模糊查询 -->  
  42.                 <if test="dicNameLike != null">and dic_name like CONCAT("%",#{dicNameLike},"%")</if>  
  43.                 <if test="dicGroupLike != null">and dic_group like CONCAT("%",#{dicGroupLike},"%")</if>  
  44.             </trim>  
  45.         </where>  
  46.         <if test="sorting != null">order by ${sorting}</if>  
  47.         <if test="offset != null and limit != null">  
  48.             limit #{offset}, #{limit}  
  49.         </if>  
  50.     </sql>  
  51.       
  52.     <!-- 查询总数 -->  
  53.     <select id="selectCount" resultType="java.lang.Long" parameterType="java.util.Map">  
  54.         select count(dic_id)  
  55.         from sys_dictionary  
  56.         <include refid="Base_Where_Clause" />  
  57.     </select>  
  58.       
  59.     <!-- 查询 -->  
  60.     <select id="select" resultMap="BaseResultMap" parameterType="java.util.Map">  
  61.         select  
  62.         <include refid="Base_Column_List" />  
  63.         from sys_dictionary  
  64.         <include refid="Base_Where_Clause" />  
  65.     </select>  
  66.       
  67.     <!-- 根据ID查询 -->  
  68.     <select id="selectById" resultMap="BaseResultMap" parameterType="java.lang.String">  
  69.         select  
  70.         <include refid="Base_Column_List" />  
  71.         from sys_dictionary  
  72.         where dic_id = #{dicId}  
  73.     </select>  
  74.       
  75.     <!-- 根据ID删除 -->  
  76.     <delete id="deleteById" parameterType="java.lang.String">  
  77.         delete from sys_dictionary  
  78.         where dic_id = #{dicId}  
  79.     </delete>  
  80.       
  81.     <!-- 删除 -->  
  82.     <delete id="delete" parameterType="java.util.Map">  
  83.         delete from sys_dictionary  
  84.         <include refid="Base_Where_Clause" />  
  85.     </delete>  
  86.   
  87.     <!-- 添加 -->     
  88.     <insert id="insert" parameterType="com.viathink.sys.domain.SysDictionary">  
  89.         insert into sys_dictionary(  
  90.             dic_id,   
  91.             dic_name,   
  92.             dic_value,  
  93.             dic_group,   
  94.             dic_type,   
  95.             dic_order,  
  96.             dic_status,   
  97.             dic_parent_id  
  98.         )  
  99.         values (  
  100.             #{dicId},   
  101.             #{dicName},  
  102.             #{dicValue},  
  103.             #{dicGroup},   
  104.             #{dicType},   
  105.             #{dicOrder},  
  106.             #{dicStatus},   
  107.             #{dicParentId}  
  108.         )  
  109.     </insert>  
  110.       
  111.       
  112.     <!-- 通过ID更新 -->  
  113.     <update id="updateByIdSelective" parameterType="com.viathink.sys.domain.SysDictionary">  
  114.         update sys_dictionary  
  115.         <set>  
  116.             <if test="dicName != null">dic_name = #{dicName},</if>  
  117.             <if test="dicValue != null">dic_value = #{dicValue},</if>  
  118.             <if test="dicGroup != null">dic_group = #{dicGroup},</if>  
  119.             <if test="dicType != null">dic_type = #{dicType},</if>  
  120.             <if test="dicOrder != null">dic_order = #{dicOrder},</if>  
  121.             <if test="dicStatus != null">dic_status = #{dicStatus},</if>  
  122.             <if test="dicParentId != null">dic_parent_id = #{dicParentId},</if>  
  123.         </set>  
  124.         where dic_id = #{dicId}  
  125.     </update>  
  126.       
  127.     <update id="updateById" parameterType="com.viathink.sys.domain.SysDictionary">  
  128.         update sys_dictionary  
  129.         set dic_name = #{dicName},  
  130.         dic_value = #{dicValue},  
  131.         dic_group = #{dicGroup},  
  132.         dic_type = #{dicType},  
  133.         dic_order = #{dicOrder},  
  134.         dic_status = #{dicStatus},  
  135.         dic_parent_id = #{dicParentId}  
  136.         where dic_id = #{dicId}  
  137.     </update>  
  138.       
  139. </mapper>  

7、Mybatis通用BaseDao实现

[java] view plain copy
  1. package com.viathink.frame.core.dao.impl;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import org.apache.commons.lang3.StringUtils;  
  7. import org.apache.ibatis.session.RowBounds;  
  8. import org.apache.ibatis.session.SqlSession;  
  9. import org.springframework.beans.factory.annotation.Autowired;  
  10. import org.springframework.data.domain.Page;  
  11. import org.springframework.data.domain.PageImpl;  
  12. import org.springframework.data.domain.Pageable;  
  13. import org.springframework.transaction.annotation.Transactional;  
  14. import org.springframework.util.Assert;  
  15.   
  16. import com.viathink.frame.core.dao.BaseDao;  
  17. import com.viathink.frame.core.dao.constants.SqlId;  
  18. import com.viathink.frame.core.dao.domain.Identifiable;  
  19. import com.viathink.frame.core.exception.DaoException;  
  20. import com.viathink.frame.core.utils.BeanUtils;  
  21. import com.viathink.frame.core.utils.UUIDUtils;  
  22.   
  23. /** 
  24.  * 基础Dao接口实现类,实现改类的子类必须设置泛型类型 
  25.  * @author LiuJunGuang 
  26.  * @date 2014年3月3日下午1:02:31 
  27.  */  
  28. public abstract class BaseDaoImpl<T extends Identifiable> implements BaseDao<T> {  
  29.     @Autowired(required = true)  
  30.     protected SqlSession sqlSessionTemplate;  
  31.   
  32.     public static final String SQLNAME_SEPARATOR = ".";  
  33.   
  34.     /** 
  35.      * @fields sqlNamespace SqlMapping命名空间  
  36.      */  
  37.     private String sqlNamespace = getDefaultSqlNamespace();  
  38.   
  39.     /** 
  40.      * 获取泛型类型的实体对象类全名 
  41.      * @author LiuJunGuang 
  42.      * @return 
  43.      * @date 2014年3月3日下午6:17:46 
  44.      */  
  45.     protected String getDefaultSqlNamespace() {  
  46.         Class<?> genericClass = BeanUtils.getGenericClass(this.getClass());  
  47.         return genericClass == null ? null : genericClass.getName();  
  48.     }  
  49.   
  50.     /** 
  51.      * 获取SqlMapping命名空间  
  52.      * @author LiuJunGuang 
  53.      * @return SqlMapping命名空间  
  54.      * @date 2014年3月4日上午12:33:15 
  55.      */  
  56.     public String getSqlNamespace() {  
  57.         return sqlNamespace;  
  58.     }  
  59.   
  60.     /** 
  61.      * 设置SqlMapping命名空间。 以改变默认的SqlMapping命名空间, 
  62.      * 不能滥用此方法随意改变SqlMapping命名空间。  
  63.      * @author LiuJunGuang 
  64.      * @param sqlNamespace SqlMapping命名空间  
  65.      * @date 2014年3月4日上午12:33:17 
  66.      */  
  67.     public void setSqlNamespace(String sqlNamespace) {  
  68.         this.sqlNamespace = sqlNamespace;  
  69.     }  
  70.   
  71.     /** 
  72.      * 将SqlMapping命名空间与给定的SqlMapping名组合在一起。 
  73.      * @param sqlName SqlMapping名  
  74.      * @return 组合了SqlMapping命名空间后的完整SqlMapping名  
  75.      */  
  76.     protected String getSqlName(String sqlName) {  
  77.         return sqlNamespace + SQLNAME_SEPARATOR + sqlName;  
  78.     }  
  79.   
  80.     /** 
  81.      * 生成主键值。 默认使用{@link UUIDUtils#create()}方法 
  82.      * 如果需要生成主键,需要由子类重写此方法根据需要的方式生成主键值。  
  83.      * @param entity 要持久化的对象  
  84.      */  
  85.     protected String generateId() {  
  86.         return UUIDUtils.create();  
  87.     }  
  88.   
  89.     /* (non-Javadoc) 
  90.      * @see com.viathink.core.dao.BaseDao#selectOne(java.io.Serializable) 
  91.      */  
  92.     @Override  
  93.     public <V extends T> V selectOne(T query) {  
  94.         Assert.notNull(query);  
  95.         try {  
  96.             Map<String, Object> params = BeanUtils.toMap(query);  
  97.             return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT), params);  
  98.         } catch (Exception e) {  
  99.             throw new DaoException(String.format("查询一条记录出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
  100.         }  
  101.     }  
  102.   
  103.     /* (non-Javadoc) 
  104.      * @see com.viathink.core.dao.BaseDao#selectById(java.io.Serializable) 
  105.      */  
  106.     @Override  
  107.     public <V extends T> V selectById(String id) {  
  108.         Assert.notNull(id);  
  109.         try {  
  110.             return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_BY_ID), id);  
  111.         } catch (Exception e) {  
  112.             throw new DaoException(String.format("根据ID查询对象出错!语句:%s", getSqlName(SqlId.SQL_SELECT_BY_ID)), e);  
  113.         }  
  114.     }  
  115.   
  116.     /* (non-Javadoc) 
  117.      * @see com.viathink.core.dao.BaseDao#selectList(java.io.Serializable) 
  118.      */  
  119.     @Override  
  120.     public <V extends T> List<V> selectList(T query) {  
  121.         try {  
  122.             Map<String, Object> params = BeanUtils.toMap(query);  
  123.             return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), params);  
  124.         } catch (Exception e) {  
  125.             throw new DaoException(String.format("查询对象列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
  126.         }  
  127.     }  
  128.   
  129.     /* (non-Javadoc) 
  130.      * @see com.viathink.core.dao.BaseDao#selectAll() 
  131.      */  
  132.     @Override  
  133.     public <V extends T> List<V> selectAll() {  
  134.         try {  
  135.             return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT));  
  136.         } catch (Exception e) {  
  137.             throw new DaoException(String.format("查询所有对象列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
  138.         }  
  139.     }  
  140.   
  141.     /* (non-Javadoc) 
  142.      * @see com.viathink.core.dao.BaseDao#selectMap(java.io.Serializable, java.lang.String) 
  143.      */  
  144.     @Override  
  145.     public <K, V extends T> Map<K, V> selectMap(T query, String mapKey) {  
  146.         Assert.notNull(mapKey, "[mapKey] - must not be null!");  
  147.         try {  
  148.             Map<String, Object> params = BeanUtils.toMap(query);  
  149.             return sqlSessionTemplate.selectMap(getSqlName(SqlId.SQL_SELECT), params, mapKey);  
  150.         } catch (Exception e) {  
  151.             throw new DaoException(String.format("查询对象Map时出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
  152.         }  
  153.     }  
  154.   
  155.     /** 
  156.      * 设置分页 
  157.      * @param pageInfo 分页信息 
  158.      * @return SQL分页参数对象 
  159.      */  
  160.     protected RowBounds getRowBounds(Pageable pageable) {  
  161.         RowBounds bounds = RowBounds.DEFAULT;  
  162.         if (null != pageable) {  
  163.             bounds = new RowBounds(pageable.getOffset(), pageable.getPageSize());  
  164.         }  
  165.         return bounds;  
  166.     }  
  167.   
  168.     /** 
  169.      * 获取分页查询参数 
  170.      * @param query 查询对象 
  171.      * @param pageable 分页对象 
  172.      * @return Map 查询参数 
  173.      */  
  174.     protected Map<String, Object> getParams(T query, Pageable pageable) {  
  175.         Map<String, Object> params = BeanUtils.toMap(query, getRowBounds(pageable));  
  176.         if (pageable != null && pageable.getSort() != null) {  
  177.             String sorting = pageable.getSort().toString();  
  178.             params.put("sorting", sorting.replace(":"""));  
  179.         }  
  180.         return params;  
  181.     }  
  182.   
  183.     /* (non-Javadoc) 
  184.      * @see com.viathink.core.dao.BaseDao#selectList(com.viathink.core.dao.domain.Identifiable, org.springframework.data.domain.Pageable) 
  185.      */  
  186.     @Override  
  187.     public <V extends T> List<V> selectList(T query, Pageable pageable) {  
  188.         try {  
  189.             return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable));  
  190.         } catch (Exception e) {  
  191.             throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
  192.         }  
  193.     }  
  194.   
  195.     /* (non-Javadoc) 
  196.      * @see com.viathink.core.dao.BaseDao#selectPageList(com.viathink.core.dao.domain.Identifiable, org.springframework.data.domain.Pageable) 
  197.      */  
  198.     @Override  
  199.     public <V extends T> Page<V> selectPageList(T query, Pageable pageable) {  
  200.         try {  
  201.             List<V> contentList = sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT),  
  202.                     getParams(query, pageable));  
  203.             return new PageImpl<V>(contentList, pageable, this.selectCount(query));  
  204.         } catch (Exception e) {  
  205.             throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
  206.         }  
  207.     }  
  208.   
  209.     /* (non-Javadoc) 
  210.      * @see com.viathink.core.dao.BaseDao#selectMap(com.viathink.core.dao.domain.Identifiable, java.lang.String, org.springframework.data.domain.Pageable) 
  211.      */  
  212.     @Override  
  213.     public <K, V extends T> Map<K, V> selectMap(T query, String mapKey, Pageable pageable) {  
  214.         try {  
  215.             return sqlSessionTemplate.selectMap(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable), mapKey);  
  216.         } catch (Exception e) {  
  217.             throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
  218.         }  
  219.     }  
  220.   
  221.     /* (non-Javadoc) 
  222.      * @see com.viathink.core.dao.BaseDao#selectCount() 
  223.      */  
  224.     @Override  
  225.     public Long selectCount() {  
  226.         try {  
  227.             return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT));  
  228.         } catch (Exception e) {  
  229.             throw new DaoException(String.format("查询对象总数出错!语句:%s", getSqlName(SqlId.SQL_SELECT_COUNT)), e);  
  230.         }  
  231.     }  
  232.   
  233.     /* (non-Javadoc) 
  234.      * @see com.viathink.core.dao.BaseDao#selectCount(java.io.Serializable) 
  235.      */  
  236.     @Override  
  237.     public Long selectCount(T query) {  
  238.         try {  
  239.             Map<String, Object> params = BeanUtils.toMap(query);  
  240.             return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT), params);  
  241.         } catch (Exception e) {  
  242.             throw new DaoException(String.format("查询对象总数出错!语句:%s", getSqlName(SqlId.SQL_SELECT_COUNT)), e);  
  243.         }  
  244.     }  
  245.   
  246.     /* (non-Javadoc) 
  247.      * @see com.viathink.core.dao.BaseDao#insert(java.io.Serializable) 
  248.      */  
  249.     @Override  
  250.     public void insert(T entity) {  
  251.         Assert.notNull(entity);  
  252.         try {  
  253.             if (StringUtils.isBlank(entity.getId()))  
  254.                 entity.setId(generateId());  
  255.             sqlSessionTemplate.insert(getSqlName(SqlId.SQL_INSERT), entity);  
  256.         } catch (Exception e) {  
  257.             throw new DaoException(String.format("添加对象出错!语句:%s", getSqlName(SqlId.SQL_INSERT)), e);  
  258.         }  
  259.     }  
  260.   
  261.     /* (non-Javadoc) 
  262.      * @see com.viathink.core.dao.BaseDao#delete(java.io.Serializable) 
  263.      */  
  264.     @Override  
  265.     public int delete(T query) {  
  266.         Assert.notNull(query);  
  267.         try {  
  268.             Map<String, Object> params = BeanUtils.toMap(query);  
  269.             return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE), params);  
  270.         } catch (Exception e) {  
  271.             throw new DaoException(String.format("删除对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE)), e);  
  272.         }  
  273.     }  
  274.   
  275.     /* (non-Javadoc) 
  276.      * @see com.viathink.core.dao.BaseDao#deleteById(java.io.Serializable) 
  277.      */  
  278.     @Override  
  279.     public int deleteById(String id) {  
  280.         Assert.notNull(id);  
  281.         try {  
  282.             return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE_BY_ID), id);  
  283.         } catch (Exception e) {  
  284.             throw new DaoException(String.format("根据ID删除对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE_BY_ID)), e);  
  285.         }  
  286.     }  
  287.   
  288.     /* (non-Javadoc) 
  289.      * @see com.viathink.core.dao.BaseDao#deleteAll() 
  290.      */  
  291.     @Override  
  292.     public int deleteAll() {  
  293.         try {  
  294.             return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE));  
  295.         } catch (Exception e) {  
  296.             throw new DaoException(String.format("删除所有对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE)), e);  
  297.         }  
  298.     }  
  299.   
  300.     /* (non-Javadoc) 
  301.      * @see com.viathink.core.dao.BaseDao#updateById(java.io.Serializable) 
  302.      */  
  303.     @Override  
  304.     public int updateById(T entity) {  
  305.         Assert.notNull(entity);  
  306.         try {  
  307.             return sqlSessionTemplate.update(getSqlName(SqlId.SQL_UPDATE_BY_ID), entity);  
  308.         } catch (Exception e) {  
  309.             throw new DaoException(String.format("根据ID更新对象出错!语句:%s", getSqlName(SqlId.SQL_UPDATE_BY_ID)), e);  
  310.         }  
  311.     }  
  312.   
  313.     /* (non-Javadoc) 
  314.      * @see com.viathink.core.dao.BaseDao#updateByIdSelective(java.io.Serializable) 
  315.      */  
  316.     @Override  
  317.     @Transactional  
  318.     public int updateByIdSelective(T entity) {  
  319.         Assert.notNull(entity);  
  320.         try {  
  321.             return sqlSessionTemplate.update(getSqlName(SqlId.SQL_UPDATE_BY_ID_SELECTIVE), entity);  
  322.         } catch (Exception e) {  
  323.             throw new DaoException(String.format("根据ID更新对象某些属性出错!语句:%s", getSqlName(SqlId.SQL_UPDATE_BY_ID_SELECTIVE)),  
  324.                     e);  
  325.         }  
  326.     }  
  327.   
  328.     /* (non-Javadoc) 
  329.      * @see com.viathink.core.dao.BaseDao#deleteByIdInBatch(java.util.List) 
  330.      */  
  331.     @Override  
  332.     @Transactional  
  333.     public void deleteByIdInBatch(List<String> idList) {  
  334.         if (idList == null || idList.isEmpty())  
  335.             return;  
  336.         for (String id : idList) {  
  337.             this.deleteById(id);  
  338.         }  
  339.     }  
  340.   
  341.     /* (non-Javadoc) 
  342.      * @see com.viathink.core.dao.BaseDao#updateInBatch(java.util.List) 
  343.      */  
  344.     @Override  
  345.     @Transactional  
  346.     public void updateInBatch(List<T> entityList) {  
  347.         if (entityList == null || entityList.isEmpty())  
  348.             return;  
  349.         for (T entity : entityList) {  
  350.             this.updateByIdSelective(entity);  
  351.         }  
  352.     }  
  353.   
  354.     /* (non-Javadoc) 
  355.      * @see com.viathink.core.dao.BaseDao#insertInBatch(java.util.List) 
  356.      */  
  357.     @Override  
  358.     public void insertInBatch(List<T> entityList) {  
  359.         if (entityList == null || entityList.isEmpty())  
  360.             return;  
  361.         for (T entity : entityList) {  
  362.             this.insert(entity);  
  363.         }  
  364.     }  
  365.   
  366. }  

9、通用BaseControllerImpl实现

[java] view plain copy
  1. package com.viathink.frame.core.web.controller;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. import org.apache.commons.lang3.ArrayUtils;  
  6. import org.apache.commons.lang3.StringUtils;  
  7. import org.slf4j.Logger;  
  8. import org.slf4j.LoggerFactory;  
  9. import org.springframework.data.domain.Page;  
  10. import org.springframework.data.domain.Pageable;  
  11. import org.springframework.data.web.PageableDefault;  
  12. import org.springframework.http.MediaType;  
  13. import org.springframework.web.bind.annotation.PathVariable;  
  14. import org.springframework.web.bind.annotation.RequestMapping;  
  15. import org.springframework.web.bind.annotation.RequestMethod;  
  16. import org.springframework.web.bind.annotation.ResponseBody;  
  17. import org.springframework.web.servlet.ModelAndView;  
  18.   
  19. import com.viathink.frame.core.dao.BaseService;  
  20. import com.viathink.frame.core.dao.domain.Identifiable;  
  21. import com.viathink.frame.core.web.domain.ControllerPath;  
  22. import com.viathink.frame.core.web.domain.Result;  
  23. import com.viathink.frame.core.web.domain.Result.Status;  
  24.   
  25. /** 
  26.  * 基础控制器接口实现类 
  27.  * @author LiuJunGuang 
  28.  * @date 2014年3月5日下午12:03:13 
  29.  */  
  30. public abstract class BaseControllerImpl<T extends Identifiable, Q extends T> implements BaseController<T, Q> {  
  31.     private Logger log = LoggerFactory.getLogger(BaseControllerImpl.class);  
  32.     /** 
  33.      * @fields path 页面路径信息 
  34.      */  
  35.     protected ControllerPath path = new ControllerPath(this.getClass());  
  36.   
  37.     /** 
  38.      * 获取基础的服务 
  39.      * @return BaseService 
  40.      */  
  41.     protected abstract BaseService<T> getBaseService();  
  42.   
  43.     @Override  
  44.     @ResponseBody  
  45.     @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)  
  46.     public Result deleteList(String[] ids) {  
  47.         if (ArrayUtils.isEmpty(ids)) {  
  48.             log.error("未设置批量删除对象的ID号!对象:{}", path.getEntityName());  
  49.             return new Result(Status.ERROR, "没有传入要删除的ID号数组!");  
  50.         }  
  51.         try {  
  52.             getBaseService().deleteByIdInBatch(Arrays.asList(ids));  
  53.         } catch (Exception e) {  
  54.             log.error("批量删除对象失败!对象:" + path.getEntityName(), e);  
  55.             return new Result(Status.ERROR, "批量删除失败!");  
  56.         }  
  57.         return new Result(Status.OK, ids.length);  
  58.     }  
  59.   
  60.     @Override  
  61.     @ResponseBody  
  62.     @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)  
  63.     public Result deleteOne(@PathVariable("id") String id) {  
  64.         if (StringUtils.isBlank(id)) {  
  65.             log.error("要删除的ID号为null或空字符串!对象:{}", path.getEntityName());  
  66.             return new Result(Status.ERROR, "没有传入要删除的ID号!");  
  67.         }  
  68.         int count = getBaseService().deleteById(id);  
  69.         if (count == 0)  
  70.             return new Result(Status.ERROR, "要删除的记录不存在!");  
  71.         log.debug("成功删除{}个对象,id:{},对象:{}", count, id, path.getEntityName());  
  72.         return new Result(Status.OK, count);  
  73.     }  
  74.   
  75.     @Override  
  76.     @RequestMapping(method = RequestMethod.POST)  
  77.     public ModelAndView addOne(T entity) {  
  78.         getBaseService().insert(entity);  
  79.         return new ModelAndView(path.getRedirectListPath());  
  80.     }  
  81.   
  82.     @Override  
  83.     @RequestMapping(value = "/add", method = RequestMethod.GET)  
  84.     public ModelAndView addView() {  
  85.         return new ModelAndView(path.getAddViewPath());  
  86.     }  
  87.   
  88.     @Override  
  89.     @RequestMapping(method = RequestMethod.GET)  
  90.     public ModelAndView selectList(Q query, @PageableDefault Pageable pageable) {  
  91.         Page<T> page = getBaseService().queryPageList(query, pageable);  
  92.         ModelAndView mav = new ModelAndView(path.getListViewPath(), "page", page);  
  93.         mav.addObject("query", query);  
  94.         return mav;  
  95.     }  
  96.   
  97.     @Override  
  98.     @RequestMapping(value = "/{id}", method = RequestMethod.GET)  
  99.     public ModelAndView viewOne(@PathVariable("id") String id) {  
  100.         Object obj = getBaseService().queryById(id);  
  101.         return new ModelAndView(path.getOneViewPath(), path.getEntityName(), obj);  
  102.     }  
  103.   
  104.     @Override  
  105.     @ResponseBody  
  106.     @RequestMapping(method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)  
  107.     public Result editOne(T entity) {  
  108.         getBaseService().updateById(entity);  
  109.         return new Result(Status.OK, entity);  
  110.     }  
  111.   
  112.     @Override  
  113.     @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)  
  114.     public ModelAndView editView(@PathVariable("id") String id) {  
  115.         Object obj = getBaseService().queryById(id);  
  116.         return new ModelAndView(path.getEditViewPath(), path.getEntityName(), obj);  
  117.     }  
  118.   
  119. }  

10、列表结果页面:

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html lang="zh-cn">  
  3. <head>  
  4. <#include "/common/common.ftl">  
  5. <@pnotify/>  
  6. <title>字典列表</title>  
  7. </head>  
  8. <body>  
  9.     <#include "/common/navbar.ftl">   
  10.     <#-- 内容开始 -->  
  11.     <div class="warp container">  
  12.         <form action="${base}/sys/dictionary">  
  13.   
  14.             <#-- 查询条件 -->  
  15.             <div class="panel panel-default">  
  16.                 <div class="panel-heading">  
  17.                     <span>查询条件</span>  
  18.                     <ul class="option-group">  
  19.                         <li><a href="javascript:void(0)" ssr-show-detail="detail" class="glyphicon glyphicon-chevron-down" title="高级查询"></a></li>  
  20.                     </ul>  
  21.                 </div>  
  22.                 <table class="table table-striped table-bordered table-hover">  
  23.                     <tbody>  
  24.                         <tr>  
  25.                             <th>名称</th>  
  26.                             <td><input type="text" class="form-control" name="dicNameLike" value="${(query.dicNameLike)!}"></td>  
  27.                             <th></th>  
  28.                             <td><input type="text" class="form-control" name="dicGroupLike" value="${(query.dicGroupLike)!}"></td>  
  29.                             <td rowspan="10">  
  30.                                 <button type="submit" class="btn btn-default">  
  31.                                     <span class="glyphicon glyphicon-search"></span> 查询  
  32.                                 </button>  
  33.                             </td>  
  34.                         </tr>  
  35.                         <tr class="detail hidden">  
  36.                             <th></th>  
  37.                             <td><input type="text" class="form-control"></td>  
  38.                             <th>类型</th>  
  39.                             <td><input type="text" class="form-control"></td>  
  40.                         </tr>  
  41.                         <tr class="detail hidden">  
  42.                             <th></th>  
  43.                             <td><input type="text" class="form-control"></td>  
  44.                             <th>类型</th>  
  45.                             <td><input type="text" class="form-control"></td>  
  46.                         </tr>  
  47.                     </tbody>  
  48.                 </table>  
  49.             </div>  
  50.   
  51.             <#-- 结果列表 -->  
  52.             <div class="panel panel-default">  
  53.                 <div class="panel-heading">  
  54.                     <span>字典列表</span>  
  55.                     <ul class="option-group">  
  56.                         <li><a data-toggle="modal" data-backdrop="static" data-target="#myModal" href="${base}/sys/dictionary/add" class="glyphicon glyphicon-plus" title="添加"></a></li>  
  57.                         <li><a href="${base}/sys/dictionary/delete" ssr-delete-all="checkboxItem" class="glyphicon glyphicon-trash" title="批量删除"></a></li>  
  58.                     </ul>  
  59.                 </div>  
  60.                 <table class="table table-striped table-bordered table-hover">  
  61.                     <thead>  
  62.                         <tr>  
  63.                             <th><input type="checkbox" ssr-select-all="checkboxItem"> 序号</th>  
  64.                             <th>名称</th>  
  65.                             <th></th>  
  66.                             <th></th>  
  67.                             <th>类型</th>  
  68.                             <th>排序</th>  
  69.                             <th>状态</th>  
  70.                             <th>操作</th>  
  71.                         </tr>  
  72.                     </thead>  
  73.                     <#list page.content as result>  
  74.                     <tr>  
  75.                         <td>  
  76.                             <input type="checkbox" class="checkboxItem" value="${result.dicId}">  
  77.                             <a href="${base}/sys/dictionary/${result.dicId}" data-toggle="modal" data-backdrop="static" data-target="#myModal">${result_index+1}</a>  
  78.                         </td>  
  79.                         <td>${result.dicName}</td>  
  80.                         <td>${result.dicValue}</td>  
  81.                         <td>${result.dicGroup}</td>  
  82.                         <td>${result.dicType.label}</td>  
  83.                         <td>${result.dicOrder}</td>  
  84.                         <td>${result.dicStatus.label}</td>  
  85.                         <td>  
  86.                             <ul class="option-group">  
  87.                                 <li><a data-toggle="modal" data-backdrop="static" data-target="#myModal" href="${base}/sys/dictionary/edit/${result.dicId}" title="编辑"  
  88.                                     class="glyphicon glyphicon-edit"></a></li>  
  89.                                 <li><a href="${base}/sys/dictionary/${result.dicId}" ssr-delete-one title="删除" class="glyphicon glyphicon-trash"></a>  
  90.                                 </li>  
  91.                             </ul>  
  92.                         </td>  
  93.                     </tr>  
  94.                     </#list>  
  95.                     <tr>  
  96.                         <td colspan="100"><@tablePage/></td>  
  97.                     </tr>  
  98.                 </table>  
  99.             </div>  
  100.         </form>  
  101.     </div>  
  102.     <#-- 内容结束 -->   
  103.     <#include "/common/footer.ftl">   
  104.     <@modal/>  
  105. </body>  
  106. </html>  

11、编辑页面:

[html] view plain copy
  1. <#assign base=request.contextPath />  
  2. <!DOCTYPE html>  
  3. <html lang="zh-cn">  
  4. <head>  
  5. </head>  
  6. <body>  
  7.     <form class="form-horizontal" role="form" method="post" action="${base}/sys/dictionary">  
  8.         <div class="modal-header">  
  9.             <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>  
  10.             <h4 class="modal-title" id="myModalLabel">编辑</h4>  
  11.         </div>  
  12.         <div class="modal-body">  
  13.             <input type="hidden" name="_method" value="put"> <input type="hidden" name="dicId" value="${dictionary.dicId}">  
  14.             <div class="form-group">  
  15.                 <label for="dicName" class="col-md-2 control-label">名称</label>  
  16.                 <div class="col-md-5">  
  17.                     <input type="text" name="dicName" class="form-control" value="${dictionary.dicName}" id="dicName" placeholder="名称">  
  18.                 </div>  
  19.             </div>  
  20.             <div class="form-group">  
  21.                 <label for="dicValue" class="col-md-2 control-label"></label>  
  22.                 <div class="col-md-5">  
  23.                     <input type="text" name="dicValue" class="form-control" value="${dictionary.dicValue}" id="dicValue" placeholder="值">  
  24.                 </div>  
  25.             </div>  
  26.             <div class="form-group">  
  27.                 <label for="dicGroup" class="col-md-2 control-label"></label>  
  28.                 <div class="col-md-5">  
  29.                     <input type="text" name="dicGroup" class="form-control" value="${dictionary.dicGroup}" id="dicGroup" placeholder="值">  
  30.                 </div>  
  31.             </div>  
  32.             <div class="form-group">  
  33.                 <label for="dicType" class="col-md-2 control-label">类型</label>  
  34.                 <div class="col-md-5">  
  35.                     <input type="text" name="dicType" class="form-control" value="${dictionary.dicType}" id="dicType" placeholder="值">  
  36.                 </div>  
  37.             </div>  
  38.             <div class="form-group">  
  39.                 <label for="dicOrder" class="col-md-2 control-label">排序</label>  
  40.                 <div class="col-md-5">  
  41.                     <input type="text" name="dicOrder" class="form-control" value="${dictionary.dicOrder}" id="dicOrder" placeholder="值">  
  42.                 </div>  
  43.             </div>  
  44.             <div class="form-group">  
  45.                 <label for="dicStatus" class="col-md-2 control-label">状态</label>  
  46.                 <div class="col-md-5">  
  47.                     <input type="text" name="dicStatus" class="form-control" value="${dictionary.dicStatus}" id="dicStatus" placeholder="值">  
  48.                 </div>  
  49.             </div>  
  50.         </div>  
  51.         <div class="modal-footer">  
  52.             <button type="button" id="saveBtn" class="btn btn-primary">保存</button>  
  53.             <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>  
  54.         </div>  
  55.     </form>  
  56. </body>  
  57. <script type="text/javascript">  
  58.     $(function() {  
  59.         $("#saveBtn").click(function() {  
  60.             $.post($('form').attr('action'), $('form').serialize(), function(data) {  
  61.                 $('[data-dismiss]').click();  
  62.             }, 'json');  
  63.         });  
  64.     });  
  65. </script>  
  66. </html>  

三、运行结果图:


spring4+mybaits3整合—项目Demo_第1张图片spring4+mybaits3整合—项目Demo_第2张图片spring4+mybaits3整合—项目Demo_第3张图片spring4+mybaits3整合—项目Demo_第4张图片



项目下载地址:http://pan.baidu.com/s/1o6v0UyI

项目下载地址2:http://download.csdn.net/detail/afgasdg/7019001



转载地址:http://blog.csdn.net/afgasdg/article/details/20942115

你可能感兴趣的:(spring4+mybaits3整合—项目Demo)