jsp文件:<%@page contentType="text/html;charset=GBK" %>
<%@page import="net.sf.hibernate.*,net.sf.hibernate.cfg.*,java.util.*,org.zzd.db.*"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Hibernate</title>
</head>
<body> <h1>Hibernate</h1>
<a href="index.jsp">首页</a> <%
String query = "from Gjb as gjb";
%>
<jsp:useBean id="listbean" class="org.zzd.listBean" scope="request"/>
<jsp:setProperty name="listbean" property="s_query" value="<%= query %>" />
<%
List list = listbean.getList();
out.print(list);%> </body>
</html>
==================================
Gjb.java:package org.zzd.db;import java.io.Serializable;
//import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Gjb implements Serializable { /** identifier field */
private Long id; /** persistent field */
private String jc; /** nullable persistent field */
private String qc; /** persistent field */
private int lb; /** full constructor */
public Gjb(String jc, String qc, int lb) {
this.jc = jc;
this.qc = qc;
this.lb = lb;
} /** default constructor */
public Gjb() {
} /** minimal constructor */
public Gjb(String jc, int lb) {
this.jc = jc;
this.lb = lb;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getJc() {
return this.jc;
} public void setJc(String jc) {
this.jc = jc;
} public String getQc() {
return this.qc;
} public void setQc(String qc) {
this.qc = qc;
} public int getLb() {
return this.lb;
} public void setLb(int lb) {
this.lb = lb;
} public String toString() {
return this.toString();
// return new ToStringBuilder(this)
// .append("id", getId())
// .toString();
}}
====================================
Gjb.hbm.xml:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="org.zzd.db.Gjb" table="gjb">
<id column="xh" name="id" type="long">
<generator class="sequence"/>
</id>
<property column="jc" length="50" name="jc" not-null="true" type="string"/>
<property column="qc" length="150" name="qc" type="string"/>
<property column="lb" length="11" name="lb" not-null="true" type="integer"/>
</class>
</hibernate-mapping>
===================================
hibernate.cfg.xml:<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/mysql</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <mapping resource="org/zzd/db/Gjb.hbm.xml"/>
<mapping resource="org/zzd/db/Gjlbb.hbm.xml"/>
<mapping resource="org/zzd/db/Jzb.hbm.xml"/>
<mapping resource="org/zzd/db/Xiaoxib.hbm.xml"/>
<mapping resource="org/zzd/db/Xxlbb.hbm.xml"/>
</session-factory>
</hibernate-configuration>
=====================================
请大家指教!
<%@page import="net.sf.hibernate.*,net.sf.hibernate.cfg.*,java.util.*,org.zzd.db.*"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Hibernate</title>
</head>
<body> <h1>Hibernate</h1>
<a href="index.jsp">首页</a> <%
String query = "from Gjb as gjb";
%>
<jsp:useBean id="listbean" class="org.zzd.listBean" scope="request"/>
<jsp:setProperty name="listbean" property="s_query" value="<%= query %>" />
<%
List list = listbean.getList();
out.print(list);%> </body>
</html>
==================================
Gjb.java:package org.zzd.db;import java.io.Serializable;
//import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Gjb implements Serializable { /** identifier field */
private Long id; /** persistent field */
private String jc; /** nullable persistent field */
private String qc; /** persistent field */
private int lb; /** full constructor */
public Gjb(String jc, String qc, int lb) {
this.jc = jc;
this.qc = qc;
this.lb = lb;
} /** default constructor */
public Gjb() {
} /** minimal constructor */
public Gjb(String jc, int lb) {
this.jc = jc;
this.lb = lb;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getJc() {
return this.jc;
} public void setJc(String jc) {
this.jc = jc;
} public String getQc() {
return this.qc;
} public void setQc(String qc) {
this.qc = qc;
} public int getLb() {
return this.lb;
} public void setLb(int lb) {
this.lb = lb;
} public String toString() {
return this.toString();
// return new ToStringBuilder(this)
// .append("id", getId())
// .toString();
}}
====================================
Gjb.hbm.xml:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="org.zzd.db.Gjb" table="gjb">
<id column="xh" name="id" type="long">
<generator class="sequence"/>
</id>
<property column="jc" length="50" name="jc" not-null="true" type="string"/>
<property column="qc" length="150" name="qc" type="string"/>
<property column="lb" length="11" name="lb" not-null="true" type="integer"/>
</class>
</hibernate-mapping>
===================================
hibernate.cfg.xml:<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/mysql</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <mapping resource="org/zzd/db/Gjb.hbm.xml"/>
<mapping resource="org/zzd/db/Gjlbb.hbm.xml"/>
<mapping resource="org/zzd/db/Jzb.hbm.xml"/>
<mapping resource="org/zzd/db/Xiaoxib.hbm.xml"/>
<mapping resource="org/zzd/db/Xxlbb.hbm.xml"/>
</session-factory>
</hibernate-configuration>
=====================================
请大家指教!
如果你使用SessionFactory sessions = new Configuration().configure().buildSessionFactory();的话,hibernate默认你的文件放在该目录下了,否则你需要指定该文件的具体位置,当然这个位置是以/WEB-INF/classes为基准的。如果你放在/WEB-INF/classes/hibernate/hibernate.cfg.xml。
则上面语句应写为:
SessionFactory sessions = new Configuration().configure("/hibernate/hibernate.cfg.xml").buildSessionFactory();
平台是myeclipse 4.0.0GA
关注ING
http://community.csdn.net/Expert/topic/4886/4886204.xml?temp=.6343195