hibernate分页查询的实现 在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() {