实体类如下: package com.mys.model; // default package import java.util.Date; /** * User entity. @author MyEclipse Persistence Tools */ public class User implements java.io.Serializable { // Fields private String id; private String userName; private String userPwd; private String sex; private Integer age; private String jiguan; private Date userRegisterTime; // Constructors /** default constructor */ public User() { } /** minimal constructor */ public User(String id, String userName, String userPwd, Date userRegisterTime) { this.id = id; this.userName = userName; this.userPwd = userPwd; this.userRegisterTime = userRegisterTime; } /** full constructor */ public User(String id, String userName, String userPwd, String sex, Integer age, String jiguan, Date userRegisterTime) { this.id = id; this.userName = userName; this.userPwd = userPwd; this.sex = sex; this.age = age; this.jiguan = jiguan; this.userRegisterTime = userRegisterTime; } // Property accessors public String getId() { return this.id; } public void setId(String id) { this.id = id; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return this.userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } public String getJiguan() { return this.jiguan; } public void setJiguan(String jiguan) { this.jiguan = jiguan; } public Date getUserRegisterTime() { return this.userRegisterTime; } public void setUserRegisterTime(Date userRegisterTime) { this.userRegisterTime = userRegisterTime; } } 业务类如下: package com.mys.util; /** * 我们必须启动Hibernate,此过程包括创建一个全局的SessoinFactory,并把它储存在应用程序代码容易访问的地方。 */ import java.util.List; import com.mys.model.*; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class HibernateUtil{ private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml //以前hibernate文档里的: sessionFactory = new Configuration().configure().buildSessionFactory(); Configuration config=new Configuration(); config.configure(); config.addClass(User.class);//完整类名? sessionFactory=config.buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } // 查询用户信息 public void showUsers() { Session session = sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); Query query=session.createQuery("from tb_User as u"); List list=query.list();//List是util.*类库里的接口 System.out.println("id"+"用户名"+"用户密码"+"性别"+"年龄"+"籍贯"+"注册时间"); for(int i=0;i<list.size();i++) { User u=(User)list.get(i); System.out.print(u.getId()); // 省略部分 System.out.print(u.getUserName()); System.out.print(u.getUserPwd()); System.out.print(u.getSex()); System.out.print(u.getAge()); System.out.print(u.getJiguan()); System.out.print(u.getUserRegisterTime()); System.out.println(""); } tx.commit(); } catch(Exception e){ e.printStackTrace(); }finally{ session.close(); } } public static void main(String[] args) { // TODO Auto-generated method stub HibernateUtil h=new HibernateUtil(); h.showUsers(); } } 配置文件hibernate.cfg.xml如下: <?xml version=""1.0"" encoding=""UTF-8""?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/db_test </property> <property name="connection.username">root</property> <property name="connection.password">425175</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile"> com.mysql.jdbc.Driver </property> <mapping resource="./User.hbm.xml" /> </session-factory> </hibernate-configuration> 映射文件在src目录下User.hbm.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd "> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="User" table="db_user" catalog="db_test"> <id name="id" type="java.lang.String"> <column name="id" length="15" /> <generator class="assigned"></generator> </id> <property name="userName" type="java.lang.String"> <column name="userName" length="20" not-null="true" /> </property> <property name="userPwd" type="java.lang.String"> <column name="userPwd" length="20" not-null="true" /> </property> <property name="sex" type="java.lang.String"> <column name="sex" length="4" /> </property> <property name="age" type="java.lang.Integer"> <column name="age" /> </property> <property name="jiguan" type="java.lang.String"> <column name="jiguan" length="20" /> </property> <property name="userRegisterTime" type="java.util.Date"> <column name="userRegisterTime" length="10" not-null="true" /> </property> </class> </hibernate-mapping> 我想用Hibernate实现一个查询数据库里所有数据的功能,映射文件和实体类都是自动生成的,业务类是自己参照着数编写的。 |
|
25分 |
异常信息中没有:could not find the main class
|