本人刚接触Hibernate,在网上看了些例子,就按着教程用netbean配置Hibernate, :D 为自己做做笔记,我试过用application和web配置,我在这里记得我配置的过程。(netbean version6.1)
1:application project
a:需要下载的东西:mysql database,core包(antlr.jar,asm-attrs.jar,asm.jar,cglic.jar,commons-collections.jar,commons-logging.jar,dom4j.jar,ehcache.jar,jdbc2_0_stdext.jar,jta.jar),连接mysql的driver(mysql-connector-java-5.0.8-bin.jar)。
b: 用mysql 新建一个database :test,建立一个table:user.
Drop table if exits test.user
Create table test.user(
id varchar(32) primary key,
username varchar(32) not null,
password varchar(32) not null
)
Type = InnoDB;
c:设置基本的hibernate.cfg.xml , 我是在SCR里面建立的,也就是<default package>里面,我也试过在自己新建的package建,但不成功,报”hibernate.cfg.xml not found”(还没找到原因).
<?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">
<hibernate-configuration>
<session-factory name="session1">
<!--设置方言为MYSQL-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--JDBC驱动程序-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--JDBC URL-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<!--database 的账号-->
<property name="hibernate.connection.username">root</property>
<!--database 的密码-->
<property name="hibernate.connection.password">root</property>
<!-- 指定对应的mapping 文件 -->
<mapping resource="hibernatemap/user.cfg.xml"/>
</session-factory>
</hibernate-configuration>
d:在 netbean里面,建立一个名为User的java文件:(这里所建的属性,必须一一对应之前在database(test)的user表)
package hibernatemap;
import java.io.Serializable;
public class User implements Serializable {
public int id;
public String username;
public String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
e:设置基本的user.cfg.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">
<hibernate-mapping>
< !-- name 写下指定刚才建的User.java路径,table是对应在database的表名 -->
<class name="hibernatemap.User" table="user">
<id name="id">
<column name="id"/>
</id>
<property name="username"/>
<property name="password"/>
</class>
</hibernate-mapping>
配置完成了,可以写一个测试类了,Test.java
package hibernateconnect;
import hibernatemap.User;
import org.hibernate.cfg.* ;
import org.hibernate.* ;
public class Test {
public static void main (String[] args) throws Exception {
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();/*建立一个sessionfactory,要hibernate.cfg.xml的路径不在默认路径里面,必须在configure()填上具体具体路径(这里我还没尝试过,我是在默认的路径建立的)*/
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
//user.setId(6);
user.setUsername("daniellai");
user.setPassword("11110");
session.save(user);
tx.commit();
} catch (HibernateException e){
e.printStackTrace();
}
}
}
2:web project.
用web 做 project去配置hibernate,你只需有Plugins到Hibernate3.0,就不需要自己动手去下载core包,Hibernate configuration file 和 Hibernate mapping file.只需要下载连接mysql的driver就可以了。方便的很,但感觉还是自己动手配配好点,起码熟悉一点.
1:application project
a:需要下载的东西:mysql database,core包(antlr.jar,asm-attrs.jar,asm.jar,cglic.jar,commons-collections.jar,commons-logging.jar,dom4j.jar,ehcache.jar,jdbc2_0_stdext.jar,jta.jar),连接mysql的driver(mysql-connector-java-5.0.8-bin.jar)。
b: 用mysql 新建一个database :test,建立一个table:user.
Drop table if exits test.user
Create table test.user(
id varchar(32) primary key,
username varchar(32) not null,
password varchar(32) not null
)
Type = InnoDB;
c:设置基本的hibernate.cfg.xml , 我是在SCR里面建立的,也就是<default package>里面,我也试过在自己新建的package建,但不成功,报”hibernate.cfg.xml not found”(还没找到原因).
<?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">
<hibernate-configuration>
<session-factory name="session1">
<!--设置方言为MYSQL-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--JDBC驱动程序-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--JDBC URL-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<!--database 的账号-->
<property name="hibernate.connection.username">root</property>
<!--database 的密码-->
<property name="hibernate.connection.password">root</property>
<!-- 指定对应的mapping 文件 -->
<mapping resource="hibernatemap/user.cfg.xml"/>
</session-factory>
</hibernate-configuration>
d:在 netbean里面,建立一个名为User的java文件:(这里所建的属性,必须一一对应之前在database(test)的user表)
package hibernatemap;
import java.io.Serializable;
public class User implements Serializable {
public int id;
public String username;
public String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
e:设置基本的user.cfg.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">
<hibernate-mapping>
< !-- name 写下指定刚才建的User.java路径,table是对应在database的表名 -->
<class name="hibernatemap.User" table="user">
<id name="id">
<column name="id"/>
</id>
<property name="username"/>
<property name="password"/>
</class>
</hibernate-mapping>
配置完成了,可以写一个测试类了,Test.java
package hibernateconnect;
import hibernatemap.User;
import org.hibernate.cfg.* ;
import org.hibernate.* ;
public class Test {
public static void main (String[] args) throws Exception {
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();/*建立一个sessionfactory,要hibernate.cfg.xml的路径不在默认路径里面,必须在configure()填上具体具体路径(这里我还没尝试过,我是在默认的路径建立的)*/
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
//user.setId(6);
user.setUsername("daniellai");
user.setPassword("11110");
session.save(user);
tx.commit();
} catch (HibernateException e){
e.printStackTrace();
}
}
}
2:web project.
用web 做 project去配置hibernate,你只需有Plugins到Hibernate3.0,就不需要自己动手去下载core包,Hibernate configuration file 和 Hibernate mapping file.只需要下载连接mysql的driver就可以了。方便的很,但感觉还是自己动手配配好点,起码熟悉一点.