利用myeclipse的反向工程导入hibernate后,配置文件hibernate.cfg.xml放在src的根目录下,工程会自己加载:
<?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/shtest
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">fjtianxia</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping resource="com/sh/entity/Manager.hbm.xml" />
</session-factory>
</hibernate-configuration>
<mapping resource="com/sh/entity/Manager.hbm.xml" />是寻找hbm.xml文件的路径映射,可以更改路径。
Manager.hbm.xml配置如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.sh.entity.Manager" table="Manager">
<id name="manaId" type="java.lang.Integer">
<column name="ManaId" />
<generator class="native" />
</id>
<property name="manaName" type="java.lang.String">
<column name="ManaName" length="50" not-null="true" />
</property>
<property name="manaPass" type="java.lang.String">
<column name="ManaPass" length="50" not-null="true" />
</property>
<property name="manaEmail" type="java.lang.String">
<column name="ManaEmail" length="50" />
</property>
</class>
</hibernate-mapping>
实体类Manager.java:
package com.sh.entity;
public class Manager {
private Integer manaId;
private String manaName;
private String manaPass;
private String manaEmail;
public Integer getManaId() {
return manaId;
}
public void setManaId(Integer manaId) {
this.manaId = manaId;
}
public String getManaName() {
return manaName;
}
public void setManaName(String manaName) {
this.manaName = manaName;
}
public String getManaPass() {
return manaPass;
}
public void setManaPass(String manaPass) {
this.manaPass = manaPass;
}
public String getManaEmail() {
return manaEmail;
}
public void setManaEmail(String manaEmail) {
this.manaEmail = manaEmail;
}
}
下面就是我们执行的代码了,今天之弄过简单的获取数据列表的例子:
package com.sh.test;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.sh.entity.Manager;
public class test1 {
SessionFactory factory;
@SuppressWarnings("unchecked")
public List getAllManager(){
factory = new Configuration().configure().buildSessionFactory();
Session session=factory.openSession();
List<Manager> l=new ArrayList<Manager>();
l= session.createCriteria(Manager.class).list();
// session.save(m);
return l;
}
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
test1 t=new test1();
t.getAllManager();
List<Manager> l=new ArrayList<Manager>();
l=t.getAllManager();
for(Manager m:l){
System.out.println(m.getManaName()+"..."+m.getManaPass()+"..."+m.getManaEmail());
}
}
}
第一天就这样了,做个准备工作,搭建个成功的环境。
posted on 2008-10-25 18:59
jestane 阅读(137)
评论(0) 编辑 收藏