1、大致思想模型
2、基本配置
2.1、创建数据库test,表student、mark
CREATE TABLE `test`.`student` (
`stuid` varchar ( 20 ) NOT NULL ,
`stuname` varchar ( 20 ) NOT NULL ,
`stusex` varchar ( 20 ) NOT NULL ,
`stuaddress` varchar ( 20 ) NOT NULL ,
PRIMARY KEY (`stuid`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
DROP TABLE IF EXISTS `test`.`mark`;
CREATE TABLE `test`.`mark` (
`stuid` varchar ( 20 ) DEFAULT NULL ,
`stumath` int ( 11 ) NOT NULL ,
`stuchinese` int ( 11 ) NOT NULL ,
`stuenglish` int ( 11 ) NOT NULL ,
KEY `stuid` (`stuid`),
CONSTRAINT `stuid` FOREIGN KEY (`stuid`) REFERENCES `student` (`stuid`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
2.2、配置hibernate
项目->点击右键->MyEclipse->Add Hibernate Capabilities...
2.3、配置hibernate.cfg.xml文件(如编码方式、sql在tomcat运行的显示)
DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
< 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 >
< property name ="current_session_context_class" > thread property >
< property name ="show_sql" > true property >
< property name ="format_sql" > true property >
< 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
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
struts >
2.5、配置web.xml(与struts2的交互)
< 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接口
import java.util.List;
public interface IStudentmarkDAO {
public List QueryAll( int start, int limit);
public int FindAllRows();
}
4.2、创建DAO实现类
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import HbmFactory.HibernateSessionFactory;
public class 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();
}
return null ;
}
public int 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();
}
return 0 ;
}
}
5、创建业务服务层Service
5.1、创建Service的Action控制器接口
public interface IActionService {
public String execute() throws Exception;
}
5.2、创建Service实现类
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import DAO. * ;
public class StudentmarkServiceImpl implements IActionService {
private IStudentmarkDAO studentmark = new StudentmarkDAOImpl();
private int start;
private int limit;
public int getStart() {
return start;
}
public void setStart( int start) {
this .start = start;
}
public int getLimit() {
return limit;
}
public void 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() + " } " );
return null ;
}
}
6、创建Action业务控制器
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 name ="showdata" class ="Service.StudentmarkServiceImpl" > action >
package >
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配置:http://files.cnblogs.com/yongfeng/Struts.rar
json-lib包:http://files.cnblogs.com/yongfeng/json-lib.rar
MyEclipse配置手册(包含tomcat6.0):http://files.cnblogs.com/yongfeng/%e6%93%8d%e4%bd%9c%e6%89%8b%e5%86%8c_MyEclipse.rar
源代码下载:http://dl.dbank.com/c09kdeh84y(缺少数据库和ExtJs)