github地址:https://github.com/durcframework/durcframework
demo:https://github.com/durcframework/durcframework.test
框架介绍:
durcframework是一个基于SpringMVC + Mybatis的框架。其设计目的在于提高开发效率,避免做重复的工作。尤其是在做管理后台时,能减少许多代码量。
框架用到的技术点:
1. 采用泛型设计,对数据库的增删改查做了适度的封装。只需少量代码就能完成一个模块的CRUD操作
2. 使用注解来生成查询条件,mybatis文件不需要额外配置,减少了mybatis的代码量。
3. 使用Java代码动态生成查询条件,可以根据不同场景,不同业务来组装查询条件。
4. 可以配合前台做Ajax开发,传输JSON格式数据,也可以使用传统SpringMVC到jsp页面。
5. 后台自动验证功能,支持JSR-303。
使用本框架可以完成的事:
1. 少量代码完成对一张表的增删改查。
2. 数据导出
3. 数据校验
下面是一个学生表的Controller类,这个类完成基本的增删改查操作
- @Controller
- public class StudentCrudController extends CrudController<Student, StudentService> {
- // 模拟表单提交
- // http://localhost/durcframeworkTest/addStudent.do?name=Lucy&stuNo=No0000197&address=USA&mobile=13345678951&birthday=1987-07-06
- @RequestMapping("/addStudent.do")
- public ModelAndView addStudent(Student student) {
- ModelAndView mav = this.save(student);
- System.out.println("添加后的主键ID:"+ student.getId());
- return mav;
- }
- // http://localhost/durcframeworkTest/listStudent.do
- @RequestMapping("/listStudent.do")
- public ModelAndView listStudent(SearchStudentEntity searchStudentEntity) {
- return this.queryByEntity(searchStudentEntity);
- }
- // 模拟表单提交,修改姓名为Lily
- // http://localhost/durcframeworkTest/updateStudent.do?id=39&name=Lily
- @RequestMapping("/updateStudent.do")
- public ModelAndView updateStudent(Student student) {
- return this.update(student);
- }
- // 传一个id值即可,根据主键删除
- // http://localhost/durcframeworkTest/delStudent.do?id=39
- @RequestMapping("/delStudent.do")
- public ModelAndView delStudent(Student student) {
- // 通过主键查询某一条记录
- System.out.println(this.getService().getById(student.getPk()));
- return this.delete(student);
- }
- }
StudentCrudController类中的四个方法分别完成了增删改查操作,并且自带分页查询。再来看看Service类和Dao类是怎么样的
- @Service
- public class StudentService extends CrudService { }
- public interface StudentDao extends BaseDao { }
Service类和Dao类只需做简单继承即可,应有的功能都封装在父类中了。
本框架采用Maven构建,您需要了解一些Maven知识,在这里就不多提了。
github地址:https://github.com/durcframework/durcframework
demo:https://github.com/durcframework/durcframework.test
-----------------------------
durcframework实质上是对SpringMVC数据操作的一种简易封装.如果您了解SpringMVC,那么您便能快速上手此框架.
下面我们就从每个文件的角度来分析.
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="starter" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <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*:/spring/servlet/servlet-config.xml
- </param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcherServlet</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath*:/applicationContext.xml
- </param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.durcframework.common.RequestContextListener</listener-class>
- </listener>
- <!-- 统一编码 -->
- <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>
- <session-config>
- <session-timeout>20</session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
web.xml文件并没有特别的地方,都是SpringMVC的一些基本配置.
<listener>
<listener-class>org.durcframework.common.RequestContextListener</listener-class>
</listener>
这个监听器是个辅助配置,不是必须的.
SpringMVC的servlet配置文件我把他放在了*:/spring/servlet/servlet-config.xml里面
SpringMVC配置文件:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/tx <a target=_blank href="http://www.springframework.org/schema/tx/spring-tx-3.0.xsd'> ">http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- </a> <!-- 支持spring mvc新的注解类型 详细spring3.0手册 15.12.1 mvc:annotation-driven -->
- <mvc:annotation-driven />
- <!-- 扫描controller
- use-default-filters="false"只扫描@Controller
- -->
- <context:component-scan base-package="durcframework.test"
- use-default-filters="false">
- <context:include-filter expression="org.springframework.stereotype.Controller"
- type="annotation" />
- </context:component-scan>
- <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <bean class="org.durcframework.common.SpringContext" />
- <bean class="org.durcframework.common.WebExceptionResolver" />
- </beans>
可以看见这里也没有特殊的配置.
顺便提一下<context:component-scan>中加了<context:include-filter expression="org.springframework.stereotype.Controller"type="annotation" />这样启动的时候Spring会只扫描有@Controller注解的java文件,而不是去扫描所有java文件.这样走可以适当的加快启动速度.
<bean class="org.durcframework.common.SpringContext" />加上这句话就可以在开发中通过SpringContext.getBean('')获取注入的类
<bean class="org.durcframework.common.WebExceptionResolver" />这个是处理异常信息的
数据源
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <!--
- 通常来说,只需要修改initialSize、minIdle、maxActive。
- 如果用Oracle,则把poolPreparedStatements配置为true,
- mysql可以配置为false。分库分表较多的数据库,建议配置为false。
- -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
- <!-- 基本属性 url、user、password -->
- <property name="url" value="${jdbc.jdbcUrl}" />
- <property name="username" value="${jdbc.user}" />
- <property name="password" value="${jdbc.password}" />
- <!-- 配置初始化大小、最小、最大 -->
- <property name="initialSize" value="${jdbc.initialPoolSize}" />
- <property name="minIdle" value="${jdbc.miniPoolSize}" />
- <property name="maxActive" value="${jdbc.maxPoolSize}" />
- <!-- 配置获取连接等待超时的时间 -->
- <property name="maxWait" value="60000" />
- <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
- <property name="timeBetweenEvictionRunsMillis" value="60000" />
- <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="300000" />
- <property name="validationQuery" value="SELECT 'x'" />
- <property name="testWhileIdle" value="true" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
- <property name="poolPreparedStatements" value="false" />
- <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
- <!-- 配置监控统计拦截的filters -->
- <property name="filters" value="stat" />
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation">
- <value>classpath:mybatis/mybatisConfig.xml</value>
- </property>
- <property name="mapperLocations">
- <list>
- <value>classpath:durcframework_mybatis/expressionBlock.xml</value>
- <value>classpath:mybatis/mapper/*.xml</value>
- </list>
- </property>
- </bean>
- <!-- 事务 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
- <property name="transactionManager" ref="transactionManager"/>
- </bean>
- <!-- mybatis的spring插件 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
- <property name="basePackage" value="durcframework.test.*.dao" />
- </bean>
- <!-- 事务配置 -->
- <!-- 打开注释开启声明式事务,并不是所有方法都需要事务,具体要看项目需求以及团队规范
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="save*" propagation="REQUIRED"/>
- <tx:method name="del*" propagation="REQUIRED"/>
- <tx:method name="update*" propagation="REQUIRED"/>
- <tx:method name="add*" propagation="REQUIRED"/>
- <tx:method name="query*" propagation="REQUIRED" read-only="true"/>
- <tx:method name="*Tran" propagation="REQUIRED"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="interceptorPointCuts" expression="execution(* *..service.*.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
- </aop:config>
- -->
- </beans>
这是一个数据源配置,包括事务处理,没有其它特殊配置.
applicationContext.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <context:property-placeholder location="classpath:/server.properties" />
- <!-- spring context配置文件 -->
- <!-- 不要扫描durcframework.test包下的Controller类 -->
- <context:component-scan base-package="durcframework.test">
- <context:exclude-filter expression="org.springframework.stereotype.Controller"
- type="annotation" />
- </context:component-scan>
- <import resource="classpath*:/spring/data/*.xml" />
- </beans>
<context:component-scan>加了<context:exclude-filter expression="org.springframework.stereotype.Controller"type="annotation"/>这样表示不扫描有@Controller注解的文件
mybatis配置
这里拿student表来说明
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="durcframework.test.student.dao.StudentDao">
- <resultMap id="queryResultMap" type="durcframework.test.student.entity.Student">
- <result column="id" property="id" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- <result column="politicsStatus" property="politicsStatus"
- jdbcType="INTEGER" />
- <result column="nationality" property="nationality" jdbcType="VARCHAR" />
- <result column="stuNo" property="stuNo" jdbcType="VARCHAR" />
- <result column="gender" property="gender" jdbcType="TINYINT" />
- <result column="department" property="department" jdbcType="INTEGER" />
- <result column="address" property="address" jdbcType="VARCHAR" />
- <result column="mobile" property="mobile" jdbcType="VARCHAR" />
- <result column="registDate" property="registDate" jdbcType="TIMESTAMP" />
- <result column="birthday" property="birthday" jdbcType="TIMESTAMP" />
- </resultMap>
- <select id="find" parameterType="org.durcframework.expression.ExpressionQuery"
- resultMap="queryResultMap">
- SELECT
- t.ID AS id
- ,t.NAME AS name
- ,t.POLITICS_STATUS AS politicsStatus
- ,t.NATIONALITY AS nationality
- ,t.STU_NO AS stuNo
- ,t.GENDER AS gender
- ,t.DEPARTMENT AS department
- ,t.ADDRESS AS address
- ,t.MOBILE AS mobile
- ,t.REGIST_DATE AS registDate
- ,t.BIRTHDAY AS birthday
- FROM student t
- <include refid="expressionBlock.where" />
- <if test="paramMap.mobile != null">
- AND t.mobile = #{paramMap.mobile,jdbcType=VARCHAR}
- </if>
- <choose>
- <when test="sortname == null">
- ORDER BY ID desc
- </when>
- <otherwise>
- ORDER BY #{sortname,jdbcType=VARCHAR} ${sortorder}
- </otherwise>
- </choose>
- <if test="!isQueryAll">
- LIMIT
- #{firstResult,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
- </if>
- </select>
- <select id="findTotalCount" parameterType="org.durcframework.expression.ExpressionQuery"
- resultType="java.lang.Integer">
- SELECT count(*) FROM student t
- <include refid="expressionBlock.where" />
- </select>
- <insert id="save" parameterType="durcframework.test.student.entity.Student" keyProperty="id" keyColumn="id" useGeneratedKeys="true">
- INSERT INTO student
- (NAME,POLITICS_STATUS,NATIONALITY,STU_NO,GENDER,DEPARTMENT,ADDRESS,MOBILE,REGIST_DATE,BIRTHDAY)
- VALUES(
- #{name,jdbcType=VARCHAR}
- ,#{politicsStatus,jdbcType=INTEGER}
- ,#{nationality,jdbcType=VARCHAR}
- ,#{stuNo,jdbcType=VARCHAR}
- ,#{gender,jdbcType=TINYINT}
- ,#{department,jdbcType=INTEGER}
- ,#{address,jdbcType=VARCHAR}
- ,#{mobile,jdbcType=VARCHAR}
- ,#{registDate,jdbcType=TIMESTAMP}
- ,#{birthday,jdbcType=TIMESTAMP}
- )
- </insert>
- <update id="update" parameterType="durcframework.test.student.entity.Student">
- UPDATE student SET
- NAME = #{name,jdbcType=VARCHAR}
- ,POLITICS_STATUS = #{politicsStatus,jdbcType=INTEGER}
- ,NATIONALITY = #{nationality,jdbcType=VARCHAR}
- ,STU_NO = #{stuNo,jdbcType=VARCHAR}
- ,GENDER = #{gender,jdbcType=TINYINT}
- ,DEPARTMENT = #{department,jdbcType=INTEGER}
- ,ADDRESS = #{address,jdbcType=VARCHAR}
- ,MOBILE = #{mobile,jdbcType=VARCHAR}
- ,REGIST_DATE = #{registDate,jdbcType=TIMESTAMP}
- ,BIRTHDAY = #{birthday,jdbcType=TIMESTAMP}
- WHERE ID = #{id,jdbcType=INTEGER}
- </update>
- <select id="get" resultMap="queryResultMap" parameterType="java.io.Serializable">
- SELECT t.ID AS id
- ,t.NAME AS name
- ,t.POLITICS_STATUS AS politicsStatus
- ,t.NATIONALITY AS nationality
- ,t.STU_NO AS stuNo
- ,t.GENDER AS gender
- ,t.DEPARTMENT AS department
- ,t.ADDRESS AS address
- ,t.MOBILE AS mobile
- ,t.REGIST_DATE AS registDate
- ,t.BIRTHDAY AS birthday
- FROM student t
- WHERE ID = #{id,jdbcType=INTEGER}
- </select>
- <delete id="del" parameterType="durcframework.test.student.entity.Student">
- DELETE FROM student
- WHERE ID = #{id,jdbcType=INTEGER}
- </delete>
- </mapper>
四条语句对应的增删改查,不难理解
其中<include refid="expressionBlock.where" />定义在框架里面,用处是拼接条件.具体代码可以在durcframework项目中查看
Entity文件
- public class Student extends BaseEntity {
- @Min(1)
- private Integer id;
- @Size(message="学生姓名",max=50)
- @NotEmpty(message="学生姓名不能为空")
- private String name;
- private Integer politicsStatus = 1;
- private String nationality = "汉族";
- @Size(message="学号长度不能超过20",max=20)
- @NotEmpty(message="学号不能为空")
- private String stuNo;
- private Byte gender = 1;
- private Integer department = 16;
- @Size(message="请输入正确的地址",max=200)
- private String address;
- @Pattern(message="请输入正确的手机号",regexp="^[1]\\d{10}$")
- private String mobile;
- private Date registDate = new Date();
- private Date birthday;
- @Override
- protected boolean isNeedValidate() {
- // 开启验证
- return true;
- // 关闭验证
- // return false;
- }
- ...get/set
- }
是student表对应的实体类,注解是用来验证的支持JSR-303
DAO文件
- /**
- * DAO类,只需简单继承父类即可
- */
- public interface StudentDao extends BaseDao<Student> {
- }
注意DAO类是一个接口而不是class,继承BaseDao<Student>其中带有泛型参数Student,表示对Student表操作
BaseDao中增删改查已经封装好了,因此子类也具有同样功能
Service
- @Service
- public class StudentService extends CrudService<Student, StudentDao> {
- }
Service层继承了CrudService<Student, StudentDao>,表示这个Service具有增删改查功能
其中两个泛型参数,Student表示操作的表,StudentDao表示把这个类注入到Service中
启动时会自动把StudentDao注入到StudentService中
Controller
- @Controller
- public class StudentCrudController extends CrudController<Student, StudentService> {
- // 模拟表单提交
- // http://localhost/durcframeworkTest/addStudent.do?name=Lucy&stuNo=No0000197&address=USA&mobile=13345678951&birthday=1987-07-06
- @RequestMapping("/addStudent.do")
- public ModelAndView addStudent(Student student) {
- ModelAndView mav = this.save(student);
- System.out.println("添加后的主键ID:"+ student.getId());
- return mav;
- }
- // http://localhost/durcframeworkTest/listStudent.do
- @RequestMapping("/listStudent.do")
- public ModelAndView listStudent(SearchStudentEntity searchStudentEntity) {
- return this.queryByEntity(searchStudentEntity);
- }
- // 模拟表单提交,修改姓名为Lily
- // http://localhost/durcframeworkTest/updateStudent.do?id=39&name=Lily
- @RequestMapping("/updateStudent.do")
- public ModelAndView updateStudent(Student student) {
- return this.update(student);
- }
- // 传一个id值即可,根据主键删除
- // http://localhost/durcframeworkTest/delStudent.do?id=39
- @RequestMapping("/delStudent.do")
- public ModelAndView delStudent(Student student) {
- // 通过主键查询某一条记录
- System.out.println(this.getService().get(student.getId()));
- return this.delete(student);
- }
- }
类似Service,Controller也只需要做简单继承即可,Service会自动注入到Controller中.这四个方法对应了增删改查.
查询类
- public class SearchStudentEntity extends SearchEasyUI{
- private Integer schId;
- private String schName;
- private Integer schPoliticsStatus;
- private String schNationality;
- private String schStuNo;
- private Integer[] schGender;
- private Integer schDepartment;
- private String schAddress;
- private String schMobile;
- private Date schRegistDateStart;
- private Date schRegistDateEnd;
- private Date schBirthday;
- public void setSchId(Integer schId) {
- this.schId = schId;
- }
- @ValueField(column = "id")
- public Integer getSchId() {
- return this.schId;
- }
- public void setSchName(String schName) {
- this.schName = schName;
- }
- @ValueField(column = "name",equal="like")
- public String getSchName() {
- return this.schName;
- }
- public void setSchPoliticsStatus(Integer schPoliticsStatus) {
- this.schPoliticsStatus = schPoliticsStatus;
- }
- @ValueField(column = "politicsStatus")
- public Integer getSchPoliticsStatus() {
- return this.schPoliticsStatus;
- }
- public void setSchNationality(String schNationality) {
- this.schNationality = schNationality;
- }
- @ValueField(column = "nationality")
- public String getSchNationality() {
- return this.schNationality;
- }
- public void setSchStuNo(String schStuNo) {
- this.schStuNo = schStuNo;
- }
- @ValueField(column = "stu_No")
- public String getSchStuNo() {
- return this.schStuNo;
- }
- @ListField(column = "gender")
- public Integer[] getSchGender() {
- return schGender;
- }
- public void setSchGender(Integer[] schGender) {
- this.schGender = schGender;
- }
- public void setSchDepartment(Integer schDepartment) {
- this.schDepartment = schDepartment;
- }
- @ValueField(column = "department")
- public Integer getSchDepartment() {
- return this.schDepartment;
- }
- public void setSchAddress(String schAddress) {
- this.schAddress = schAddress;
- }
- @ValueField(column = "address")
- public String getSchAddress() {
- return this.schAddress;
- }
- public void setSchMobile(String schMobile) {
- this.schMobile = schMobile;
- }
- @ValueField(column = "mobile")
- public String getSchMobile() {
- return this.schMobile;
- }
- @ValueField(column="regist_date",equal=">=")
- public Date getSchRegistDateStart() {
- return schRegistDateStart;
- }
- public void setSchRegistDateStart(Date schRegistDateStart) {
- this.schRegistDateStart = schRegistDateStart;
- }
- @ValueField(column="regist_date",equal="<")
- public Date getSchRegistDateEnd() {
- if(schRegistDateEnd != null){
- return DateUtil.getDateAfterDay(schRegistDateEnd, 1);
- }
- return schRegistDateEnd;
- }
- public void setSchRegistDateEnd(Date schRegistDateEnd) {
- this.schRegistDateEnd = schRegistDateEnd;
- }
- public void setSchBirthday(Date schBirthday) {
- this.schBirthday = schBirthday;
- }
- @ValueField(column = "birthday")
- public Date getSchBirthday() {
- return this.schBirthday;
- }
- }
@ValueField注解表示单值查询,支持like模糊查询,即name = 'jim'
@ListField表示多值查询,即xxx in (y,z)
项目整体结构图:
后台准备工作,请查看:http://blog.csdn.net/thc1987/article/details/33721833
前台页面:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ include file="../../taglib.jsp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>学生管理</title>
- <style type="text/css">
- .stu_cont{padding: 1px;}
- .fitem{margin: 4px;}
- </style>
- </head>
- <body>
- <div class="stu_cont">
- <div id="toolbar">
- <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">添加学生</a>
- </div>
- <table id="dg" title="学生信息" class="easyui-datagrid"
- url="${ctx}listStudent.do"
- toolbar="#toolbar" pagination="true"
- rownumbers="false" fitColumns="true" singleSelect="true">
- <thead>
- <tr>
- <th data-options="field:'stuNo',sortable:true,width:20">学号</th>
- <th data-options="field:'name',width:20">姓名</th>
- <th data-options="field:'gender',width:20,formatter:formatGender">性别</th>
- <th data-options="field:'nationality',width:20">名族</th>
- <th data-options="field:'address',width:50,formatter:formatAddr">家庭地址</th>
- <th data-options="field:'mobile',width:20">手机号</th>
- <th data-options="field:'birthday',width:20">出生日期</th>
- <th data-options="field:'registDate',sortable:true,width:20">入学时间</th>
- <th data-options="field:'_operate',width:80,align:'center',formatter:formatOper">操作</th>
- </tr>
- </thead>
- </table>
- <div id="dlg" class="easyui-dialog" style="width:500px;height:380px;padding:10px 20px"
- closed="true" modal="true" buttons="#dlg-buttons">
- <form id="fm" method="post" novalidate>
- <div class="fitem">
- <label>学号:</label>
- <input name="stuNo" class="easyui-validatebox" required="true">
- </div>
- <div class="fitem">
- <label>姓名:</label>
- <input name="name" class="easyui-validatebox" required="true">
- </div>
- <div class="fitem">
- <label>性别:</label>
- <select class="easyui-combobox" name="gender" style="width:100px;" required="true">
- <option value="1" selected>男</option>
- <option value="0">女</option>
- </select>
- </div>
- <div class="fitem">
- <label>名族:</label>
- <input name="nationality">
- </div>
- <div class="fitem">
- <label>政治面貌:</label>
- <select class="easyui-combobox" name="politicsStatus" style="width:100px;" required="true">
- <option value="0" selected>群众</option>
- <option value="1">共青团员</option>
- <option value="2">党员</option>
- </select>
- </div>
- <div class="fitem">
- <label>手机号:</label>
- <input name="mobile">
- </div>
- <div class="fitem">
- <label>联系地址:</label>
- <input name="address">
- </div>
- <div class="fitem">
- <label>出生日期:</label>
- <input name="birthday" class="easyui-datebox">
- </div>
- <div class="fitem">
- <label>入学时间:</label>
- <input name="registDate" class="easyui-datebox" required="true">
- </div>
- </form>
- </div>
- <div id="dlg-buttons">
- <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">保存</a>
- <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
- </div>
- </div>
- <jsp:include page="../../easyui_lib.jsp"></jsp:include>
- <script type="text/javascript">
- var url;
- function newUser(){
- $('#dlg').dialog('open').dialog('setTitle','添加学生');
- $('#fm').form('clear');
- url = '${ctx}addStudent.do';
- }
- function editUser(index){
- $('#dg').datagrid('selectRow',index);
- var row = $('#dg').datagrid('getSelected');
- if (row){
- $('#dlg').dialog('open').dialog('setTitle','修改学生信息');
- $('#fm').form('clear').form('load',row);
- url = '${ctx}updateStudent.do?id='+row.id;
- }
- }
- function saveUser(){
- $('#fm').form('submit',{
- url: url,
- onSubmit: function(){
- return $(this).form('validate');
- },
- success: function(result){
- var result = eval('('+result+')');
- if (result.success){
- $('#dlg').dialog('close'); // close the dialog
- $('#dg').datagrid('reload'); // reload the user data
- } else {
- var errorMsg = result.errorMsg;
- errorMsg = errorMsg || buildValidateError(result)
- $.messager.show({
- title: 'Error',
- msg: errorMsg,
- style:{
- right:'',
- top:document.body.scrollTop+document.documentElement.scrollTop,
- bottom:''
- }
- });
- }
- }
- });
- }
- function buildValidateError(result){
- var validateErrors = result.validateErrors;
- return validateErrors.join('<br>')
- }
- function removeUser(index){
- $('#dg').datagrid('selectRow',index);
- var row = $('#dg').datagrid('getSelected');
- if (row){
- $.messager.confirm('Confirm','确定要删除该学生('+row.name+')的信息吗?',function(r){
- if (r){
- $.post('${ctx}delStudent.do',{id:row.id},function(result){
- if (result.success){
- $('#dg').datagrid('reload'); // reload the user data
- } else {
- $.messager.show({ // show error message
- title: 'Error',
- msg: result.errorMsg
- });
- }
- },'json');
- }
- });
- }
- }
- function formatOper(val,row,index){
- return '<a href="#" onclick="editUser('+index+')">修改</a>'+
- ' | <a href="#" onclick="removeUser('+index+')">删除</a>';
- }
- function formatGender(val,row){
- return val == 1 ? '男' : '女';
- }
- function formatAddr(val,row){
- val = val || "";
- if(val.length>20) {
- var showAddr = (val||'').substring(0,10) + "...";
- return '<div title="'+val+'">'+showAddr+'</div>';
- }else{
- return val;
- }
- }
- </script>
- </body>
- </html>
代码不是很多,效果图:
源代码以及更多例子请访问github:https://github.com/durcframework/durcframework.test
框架源代码:https://github.com/durcframework/durcframework