• ExtJs、Struts2、Hibernate3.2的Grid后台分页


    1、大致思想模型

    2、基本配置

    2.1、创建数据库test,表student、mark

    DROPTABLEIFEXISTS `test`.`student`;
    CREATETABLE `test`.`student` (
    `stuid`
    varchar(20) NOTNULL,
    `stuname`
    varchar(20) NOTNULL,
    `stusex`
    varchar(20) NOTNULL,
    `stuaddress`
    varchar(20) NOTNULL,
    PRIMARYKEY (`stuid`)
    ) ENGINE
    =InnoDB DEFAULT CHARSET=utf8;

    DROPTABLEIFEXISTS `test`.`mark`;
    CREATETABLE `test`.`mark` (
    `stuid`
    varchar(20) DEFAULTNULL,
    `stumath`
    int(11) NOTNULL,
    `stuchinese`
    int(11) NOTNULL,
    `stuenglish`
    int(11) NOTNULL,
    KEY `stuid` (`stuid`),
    CONSTRAINT `stuid` FOREIGNKEY (`stuid`) REFERENCES `student` (`stuid`)
    ) ENGINE
    =InnoDB DEFAULT CHARSET=utf8;

     

    2.2、配置hibernate

    项目->点击右键->MyEclipse->Add Hibernate Capabilities...

     

    2.3、配置hibernate.cfg.xml文件(如编码方式、sql在tomcat运行的显示)

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
    >

    <!-- Generated by MyEclipse Hibernate Tools. -->
    <hibernate-configuration>

    <session-factory>
    <property name="connection.username">root</property>
    <property name="connection.url">
    jdbc:mysql://localhost:3306
    </property>
    <property name="dialect">
    org.hibernate.dialect.MySQLDialect
    </property>
    <property name="myeclipse.connection.profile">mysql</property>
    <property name="connection.password">123</property>
    <property name="connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <!-- 数据库方面的sql语句的显示,可省略-->
    <property name="current_session_context_class">thread</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <!-- 数据库存储方面的字符串格式,不可省略,使传送到数据库的编码是utf-8格式 -->
    <property name="connection.useUnicode">true</property>
    <property name="connection.characterEncoding">UTF-8</property>
    </session-factory>

    </hibernate-configuration>

     

    2.4、配置struts2,加入基本包和sturts.xml

    commons-fileupload-1.2.1.jar

    commons-io-1.3.2.jar

    freemarker-2.3.16.jar

    javassist-3.7.ga.jar

    ognl-3.0.jar

    struts2-core-2.2.1.jar

    xwork-core-2.2.1.jar

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
    >

    <struts>
    <!-- Add packages here -->
    </struts>

     

    2.5、配置web.xml(与struts2的交互)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
    ="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    >
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

     

    2.6、配置ExtJs

    官网下载:http://www.sencha.com/products/js/

     

    3、创建数据库持久层PO

    (见源码)

     

    4、创建数据库访问层DAO

    4.1、创建DAO接口

    package DAO;

    import java.util.List;

    publicinterface IStudentmarkDAO {
    public List QueryAll(int start, int limit);
    publicint FindAllRows();
    }

    4.2、创建DAO实现类

    package DAO;

    import java.util.List;

    import org.hibernate.Query;
    import org.hibernate.Session;

    import HbmFactory.HibernateSessionFactory;

    publicclass StudentmarkDAOImpl implements IStudentmarkDAO {
    public List QueryAll(int start, int limit) {
    Session session
    = HibernateSessionFactory.getSession();
    session.beginTransaction();
    String sql
    ="select new map(p.stuid as stuid,p.stuname as stuname,p.stusex as stusex,p.stuaddress as stuaddress,"
    +"q.stumath as stumath,q.stuchinese as stuchinese,q.stuenglish as stuenglish) from Student p,Mark q where p.stuid=q.stuid";

    Query query
    = session.createQuery(sql);
    session.getTransaction().commit();
    if (query !=null) {
    return query.setFirstResult(start).setMaxResults(limit).list();
    }
    returnnull;
    }

    publicint FindAllRows() {
    Session session
    = HibernateSessionFactory.getSession();
    session.beginTransaction();
    String sql
    ="select new map(p.stuid as stuid,p.stuname as stuname,p.stusex as stusex,p.stuaddress as stuaddress,"
    +"q.stumath as stumath,q.stuchinese as stuchinese,q.stuenglish as stuenglish) from Student p,Mark q where p.stuid=q.stuid";

    Query query
    = session.createQuery(sql);
    session.getTransaction().commit();
    if (query !=null) {
    return query.list().size();
    }
    return0;
    }
    }

    5、创建业务服务层Service

    5.1、创建Service的Action控制器接口

    package Service;

    publicinterface IActionService {
    public String execute() throws Exception;
    }

    5.2、创建Service实现类

    package Service;

    import javax.servlet.http.HttpServletResponse;

    import net.sf.json.JSONArray;

    import org.apache.struts2.ServletActionContext;

    import DAO.*;

    publicclass StudentmarkServiceImpl implements IActionService {
    private IStudentmarkDAO studentmark =new StudentmarkDAOImpl();
    privateint start;
    privateint limit;

    publicint getStart() {
    return start;
    }

    publicvoid setStart(int start) {
    this.start = start;
    }

    publicint getLimit() {
    return limit;
    }

    publicvoid setLimit(int limit) {
    this.limit = limit;
    }

    public String execute() throws Exception {
    HttpServletResponse response
    = ServletActionContext.getResponse();

    // 编制响应的格式
    response.setContentType("text/html;charset=UTF-8");

    JSONArray json
    = JSONArray.fromObject(studentmark.QueryAll(getStart(),
    getLimit()));
    response.getWriter().write(
    "{\"totalCount\":"+ studentmark.FindAllRows() +",\"data\":"
    + json.toString() +"}");
    returnnull;
    }

    }

    6、创建Action业务控制器

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
    >

    <struts>

    <package name="Service" extends="struts-default">
    <!-- 配置Action -->
    <action name="showdata" class="Service.StudentmarkServiceImpl"></action>
    </package>

    <!-- Add packages here -->
    </struts>

    7、运行效果图

     

    8、总结

    这里用了json-lib进行json序列化,hibernate中外键用了map的映射方法,即此处HQL是“select new map(...) ...”,对map有个初步的了解;

    大致了解了sturts和hibernate的模型,为之后课程设计做好了一定的准备工作;

     

    9、以下是相关配置和使用环境

    win7系统

    myeclipse6.5

    tomcat6.0版本

    extjs3.2版本

    struts2.2.1

    hibernate3.2

    mysql5.1

    json-lib

     

    10、下载相关

    因为源代码没有加入Extjs,所以这部分添加麻烦读者自己到它们的官方网站下载了。

    Extjs官方下载包:http://www.sencha.com/products/js/

    struts配置:https://files.cnblogs.com/yongfeng/Struts.rar

    json-lib包:https://files.cnblogs.com/yongfeng/json-lib.rar

    MyEclipse配置手册(包含tomcat6.0):https://files.cnblogs.com/yongfeng/%e6%93%8d%e4%bd%9c%e6%89%8b%e5%86%8c_MyEclipse.rar

    源代码下载:http://dl.dbank.com/c09kdeh84y(缺少数据库和ExtJs)

  • 相关阅读:
    Laravel中如何将单个routes.php分割成多个子文件
    PHP中“==”运算符的安全问题
    一些需要禁用的PHP危险函数
    使用PHP QR Code生成二维码
    gulp详细入门教程
    h5+分享到微信、朋友圈代码示例
    学习地址
    关于this 方法中套方法中找 立即执行的问题 等问题
    判断条件问题是否存在
    jsp 基础语法
  • 原文地址:https://www.cnblogs.com/yongfeng/p/1864785.html
Copyright © 2020-2023  润新知