1 hibernate.properties 一定要放到src目录下。
2 User包下面放有测试类HibernateUtil.java User.hbm.xml User.java
3 过程的一个属性:xml文件 设置为copy
BO类:
package User;
public class User {
public User() {
}
get set方法。。。。。。。。。。。。。。。
private int id;
private String userName;
private String userPwd;
}
User.hbm.xml 文件映射:
<?xml version="1.0"?>
<!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 - Hibernate Tools
-->
<hibernate-mapping>
<class name="User.User" table="user">
<id name="id" type="integer">
<column name="id" />
<generator class="increment"></generator>
</id>
<property name="userName" type="string">
<column name="userName" length="100" not-null="true" />
</property>
<property name="userPwd" type="string">
<column name="userPwd" length="100" not-null="true" />
</property>
</class>
</hibernate-mapping>
测试类:
package User;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class HibernateUtil {
public HibernateUtil() {
}
private static SessionFactory sessionFactory;
public static void main(String[] args) {
try {
Configuration config=new Configuration();
config.addClass(User.class);
sessionFactory = config.buildSessionFactory();
Session s = sessionFactory.openSession();
Transaction t = s.beginTransaction();
User user = new User();
// user=(User)s.load(User.class,1);
// List l=(ArrayList) s.find("from User.User");
//
// for(ListIterator iteator=l.listIterator();iteator.hasNext();)
// {
// user=(User)iteator.next();
// System.out.println(user.getUserName());
// }
// user.setId(3);
user.setUserName("8888888");
user.setUserPwd("888888");
s.save(user);
t.commit();
s.close();
System.out.println("congratulation!!!");
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
}
文件:hibernate.properties
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.url=jdbc:mysql://localhost:3307/test
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.show_sql=true