Hibernate的配置
建立web project工程ShoppingHibernate,工程文件结构图如下所示:
1,将Hibernate的JAR包和MySQL数据库的驱动程序JAR包导入工程,具体包如下图所示:
2,建立数据库的配置文件如下:(连接的数据库schema为fcshopping,表为uservo)
View Code
<?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> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/fcshopping </property> <property name="connection.username">root</property> <property name="connection.password">lpshou</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <mapping resource="domain/UserVo.hbm.xml" /> </session-factory> </hibernate-configuration>
3,建立用户信息持久化类domain.UserVo.java,具体代码如下:
View Code
package domain;
// default package
@SuppressWarnings("serial")
public class UserVo implements java.io.Serializable {
private Integer userId;
private String userName;
private String password;
// Constructors
/** default constructor */
public UserVo() {
}
/** full constructor */
public UserVo(String userName, String password) {
this.userName = userName;
this.password = password;
}
// Property accessors
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
4,建立持久化类对应的映射文件,代码如下:
View Code
<?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="domain.UserVo" table="uservo" catalog="fcshopping"> <id name="userId" type="java.lang.Integer"> <column name="userID" /> <generator class="native"></generator> </id> <property name="userName" type="java.lang.String"> <column name="userName" length="20" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="20" not-null="true" /> </property> </class> </hibernate-mapping>
5,建立商品信息管理的测试类:
View Code
package action; import java.util.ArrayList; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import domain.UserVo; public class AddProduct { public static SessionFactory sessionFactory; static{ try{ Configuration config=new Configuration().configure(); //config.addClass(UserVo.class); sessionFactory=config.buildSessionFactory(); }catch(MappingException e){ e.printStackTrace(); }catch(HibernateException e){ e.printStackTrace(); } } public void save(){ Session session=sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); tx.begin(); UserVo userVo=new UserVo(); userVo.setUserName("test"); userVo.setPassword("123"); session.save(userVo); tx.commit(); }catch(Exception e){ tx.rollback(); e.printStackTrace(); }finally{ session.close(); } } @SuppressWarnings("unchecked") public void findAll(){ List list=new ArrayList(); Session session=sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); tx.begin(); String sql="from UserVo"; Query query=session.createQuery(sql); list=query.list(); tx.commit(); }catch(HibernateException e){ e.printStackTrace(); } System.out.println("编号 姓名 密码 "); System.out.println("---------------------------------"); for(int i=0;i<list.size();i++){ UserVo userVo=(UserVo)list.get(i ); System.out.print(" "+userVo.getUserId()+" "); System.out.print(userVo.getUserName()+" "); System.out.println(userVo.getPassword()+" "); } } public static void main(String[] args) { // TODO Auto-generated method stub AddProduct ap=new AddProduct(); ap.save(); ap.findAll(); } }
6,执行java程序,结果如下图:
内容参考:javaEE框架技术进阶式教程,赵彦