锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
]]>
1.eclipse 3.2.2
2.myeclipse 5.1.1
3.jdk 1.5
浜屻佺畝瑕佽鏄?br>
鏁版嵁搴撲負mysql
鍦╩ysql涓緩绔嬩竴涓猼est鏁版嵁搴?寤虹珛cat琛?br>CREATE TABLE `cat` (
`cat_id` varchar(32) NOT NULL,
`name` varchar(16) NOT NULL,
`sex` varchar(1) default NULL,
`weight` float(9,3) default NULL,
PRIMARY KEY (`cat_id`)
)
涓夈佹楠?o:p>
1.瀵煎叆鍖呯殑鍑嗗宸ヤ綔
a.鏂板緩java project.寤虹珛鍖卐xample
鍦ㄥ畠涓嬮潰緙栧啓綾籆at.java
package example;
// Fields
private String catId;
private String name;
private String sex;
private Float weight;
// Constructors
/** default constructor */
public Cat() {
}
/** minimal constructor */
public Cat(String name) {
this.name = name;
}
/** full constructor */
public Cat(String name, String sex, Float weight) {
this.name = name;
this.sex = sex;
this.weight = weight;
}
// Property accessors
public String getCatId() {
return this.catId;
}
public void setCatId(String catId) {
this.catId = catId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Float getWeight() {
return this.weight;
}
public void setWeight(Float weight) {
this.weight = weight;
}
}
鍚屾牱鍦ㄦ鍖呬笅闈㈢紪鍐機at.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="example.Cat" table="cat" catalog="testhibernate">
<id name="catId" type="java.lang.String">
<column name="cat_id" length="32" />
<generator class="uuid.hex"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="16" not-null="true" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="1" />
</property>
<property name="weight" type="java.lang.Float">
<column name="weight" precision="9" scale="3" />
</property>
</class>
</hibernate-mapping>
b.鍦ㄥ伐紼嬬殑src閲岄潰鍔犲叆涓涓寘錛岀敤鏉ュ瓨鏀懼皢瑕佺敓鎴愮殑HibernateSessionFactory銆傚寘鍚嶅錛坋xample.util錛夈?br>
package example.util;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
}
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
session.close();
}
}
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
瀵瑰伐紼嬪悕鐐歸紶鏍囧彸閿傞夋嫨myeclipse->add
hibernate capabicities銆?o:p>
鍦ㄥ脊鍑虹殑紿楀彛閫夋嫨涓璈ibernate 3.0 Core Libraries鍜孒ibernate 3.0 Advanced Support Libraries
涓嬮潰閫変腑Copy checked Library Jars to project folder and add to build-path銆傜偣鍑諱笅涓姝ャ?o:p>
c.榛樿錛坔ibernate cofig file錛夛紝涓嬩竴姝ャ?o:p>
d.閫変腑User JDBC driver
connect url: jdbc:mysql://localhost:3306/test
Driver class: org.gjt.mm.mysql.Driver
username: root
password: ******
Dialect: mysql
e.鍦ㄧ涓琛屽寘閫夋嫨閲岄潰錛岄夋嫨鍦ㄥ墠闈㈢浜屽ぇ姝ュ緩鐨勫寘濡傦紙example錛夈傜偣鍑誨畬鎴愩?o:p>
f.寮瑰嚭鐨勭敾闈腑 閫夋嫨properties鐨刟dd鎸夐挳銆傚湪Property涓姞鍏how_sql錛孷alue涓姞鍏rue銆傜偣紜畾
淇濆瓨璁劇疆銆傚湪mappings涓偣add鍔犲叆鍓嶉潰寤虹珛鐨凜at.hbm.xml銆傛渶鍚庣敓鎴愮殑hibernate.cfg.xml鏂囦歡濡備笅 <!-- Generated by MyEclipse Hibernate Tools. --> <session-factory> </session-factory> </hibernate-configuration>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"
<hibernate-configuration>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/testhibernate
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="example/Cat.hbm.xml" />
package test;
import java.util.List;
import example.*;
import example.util.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
Session session=null;
Transaction tx=null;
public static void main(String[] args) {
TestHibernate th=new TestHibernate();
List cl=th.getAllCats();
if(cl!=null){
Iterator it=cl.iterator();
while(it.hasNext()){
Cat cat=(Cat)it.next();
System.out.println("catID:"+cat.getCatId()+"name:"+cat.getName()+"sex:"+cat.getSex());
}
}
public List getAllCats(){
session=HibernateSessionFactory.getSession();
List catlist=null;
try{
tx=session.beginTransaction();
catlist=session.createQuery("from Cat").list();
return catlist;
}catch(Exception ex){
System.err.println(ex.getMessage());
return null;
}finally{
HibernateSessionFactory.closeSession();
}
}
]]>
主站蜘蛛池模板:
亚洲国产日韩在线一区|
亚洲国产成人一区二区三区|
亚洲免费在线视频播放|
69视频在线观看免费|
亚洲AV成人片色在线观看高潮
|
亚洲人精品午夜射精日韩|
一区二区免费在线观看|
亚洲精品无码永久在线观看|
一区二区三区视频免费观看|
亚洲国产人成中文幕一级二级|
深夜免费在线视频|
337p日本欧洲亚洲大胆裸体艺术
|
精品视频一区二区三区免费|
亚洲精品无码久久久久|
毛片在线播放免费观看|
久久久亚洲裙底偷窥综合|
7723日本高清完整版免费|
亚洲一级特黄特黄的大片|
精品久久洲久久久久护士免费|
日本亚洲欧美色视频在线播放|
国产免费变态视频网址网站|
特级毛片免费播放|
亚洲AV无码专区亚洲AV伊甸园|
曰批全过程免费视频网址|
亚洲色丰满少妇高潮18p|
免费人成年激情视频在线观看|
国产精品免费看久久久香蕉
|
亚洲综合中文字幕无线码|
麻豆精品国产免费观看|
一级特黄a免费大片|
亚洲gv猛男gv无码男同短文|
91久久成人免费|
亚洲hairy多毛pics大全|
亚洲熟妇av一区二区三区|
2015日韩永久免费视频播放|
亚洲精品无码久久久久APP|
精品亚洲成α人无码成α在线观看
|
国产成人免费一区二区三区|
中国毛片免费观看|
一本天堂ⅴ无码亚洲道久久|
久久亚洲精品无码观看不卡|