1331601238 卢久贵
在mysql中新建数据好USER表,字段有3个,分别是id、username、password,贴上脚本仅供参考
create table `ding`.`user`(
`id` int default '' not null, `username` varchar(30), `password` varchar(30), primary key (`id`) );切换到数据库透视图,新建一个mysql连接(这里就不再多说了)。
新建一个web工程,然后右键选择myeclipse,选择add hibernate........ ,把hibernate的jar加入到工程中,选择的时候注意jar包要放在lib中,可以检查下,其中有一下会选择刚新建的mysql连接,加完以后就会看到SRC下多了一个hibernate.cfg.xml文件,内容包含了配置的连接信息,如下:<?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"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.url"> jdbc:mysql://localhost:3306/ding </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="myeclipse.connection.profile">mysql</property> <property name="connection.password">ding</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> </session-factory></hibernate-configuration>切换到数据库透视图,启动那个连接找到建的USER表,右键选择Hibernate Reverse Engineering...,在弹出的框里面点击 Java src folder 一行最右侧的 Browser.. 按钮来选择源码目录,这里我选择自己工程src 目录,并把下面的 Java package 右侧的输入框中输入自己想要的包名,我这里是test,接着选中下面的复选框,我选前面两个:
[x] Hibernate mapping file (*.hbm.xml) for each database table[x] Java Data Object (POJO <> DB Table)第2项下面的复选create abstract class我这里不选,点击next,再在ID generator选择increment(自增长),点完成。返回j2ee透视图,会看到在test包下生成了User.java和User.hbm.xml文件,如下: public class User implements java.io.Serializable { private Integer id; private String username; private String password; public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } 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; } }
<?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="test.User" table="user"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="30" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="30" /> </property> </class> </hibernate-mapping>
同时在原先的hibernate.cfg.xml中加入了 <mapping resource="test/User.hbm.xml" />,最终的hibernate.cfg.xml文件如下:
<?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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.url"> jdbc:mysql://localhost:3306/ding </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="myeclipse.connection.profile">mysql</property> <property name="connection.password">ding</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <mapping resource="test/User.hbm.xml" /> </session-factory> </hibernate-configuration>
写个Test测试类
public class Test { /** * @Description 方法实现功能描述 * @param args * void * @throws 抛出异常说明 */ public static void main(String[] args) { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session session = sf.openSession(); String hql = "from User"; int curpage = 1;//当前页 int pagesize = 2;//每页显示数 //以上两项有一项为0则查出所有记录 List list = findOnePage(session, hql, curpage, pagesize); for(Iterator i = list.iterator();i.hasNext();){ User user = (User) i.next(); System.out.println("username==="+user.getUsername()); } } public static List findOnePage(Session session,String strHQL, int offset, int pagesize){ List lst = null; try { Query query = session.createQuery(strHQL); if (offset != 0 && pagesize != 0) { query.setFirstResult((offset - 1) * pagesize); query.setMaxResults(pagesize); } lst = query.list(); } catch (Exception ex) { ex.printStackTrace(); } return lst; } }
测试下就可以看到效果了 int curpage = 1;//当前页 int pagesize = 2;//每页显示数 可以改这两个参数试试