package pojo;
/**
* TbUserId entity. @author MyEclipse Persistence Tools
*/
public class TbUserId implements java.io.Serializable {
// Fields
private Integer id;
private String username;
private String password;
private String userLastLogTime;
// Constructors
/** default constructor */
public TbUserId() {
}
/** full constructor */
public TbUserId(Integer id, String username, String password,
String userLastLogTime) {
this.id = id;
this.username = username;
this.password = password;
this.userLastLogTime = userLastLogTime;
}
// Property accessors
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;
}
public String getUserLastLogTime() {
return this.userLastLogTime;
}
public void setUserLastLogTime(String userLastLogTime) {
this.userLastLogTime = userLastLogTime;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof TbUserId))
return false;
TbUserId castOther = (TbUserId) other;
return ((this.getId() == castOther.getId()) || (this.getId() != null
&& castOther.getId() != null && this.getId().equals(
castOther.getId())))
&& ((this.getUsername() == castOther.getUsername()) || (this
.getUsername() != null
&& castOther.getUsername() != null && this
.getUsername().equals(castOther.getUsername())))
&& ((this.getPassword() == castOther.getPassword()) || (this
.getPassword() != null
&& castOther.getPassword() != null && this
.getPassword().equals(castOther.getPassword())))
&& ((this.getUserLastLogTime() == castOther
.getUserLastLogTime()) || (this.getUserLastLogTime() != null
&& castOther.getUserLastLogTime() != null && this
.getUserLastLogTime().equals(
castOther.getUserLastLogTime())));
}
public int hashCode() {
int result = 17;
result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());
result = 37 * result
+ (getUsername() == null ? 0 : this.getUsername().hashCode());
result = 37 * result
+ (getPassword() == null ? 0 : this.getPassword().hashCode());
result = 37
* result
+ (getUserLastLogTime() == null ? 0 : this.getUserLastLogTime()
.hashCode());
return result;
}
}
Action
package action;
import service.MemberService;
import bean.PageBean;
import com.opensymphony.xwork2.ActionSupport;
public class ListMemberAction extends ActionSupport{
//通过applicationContext.xml配置文件注入memberService的值
private MemberService memberService;
public void setMemberService(MemberService memberService) {
this.memberService = memberService;
}
private int page; //第几页
public MemberService getMemberService() {
return memberService;
}
private PageBean pageBean; //包含分布信息的bean
public int getPage() {
return page;
}
public void setPage(int page) { //若URL中无此参数,会默认为第1页
this.page = page;
}
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
public String list() throws Exception{
this.pageBean = memberService.queryForPage(2, page);
//listmember=memberService.list();
return SUCCESS;
}
@Override
public String execute() throws Exception {
//分页的pageBean,参数pageSize表示每页显示记录数,page为当前页
this.pageBean = memberService.queryForPage(2, page);
return SUCCESS;
}
}
DaoImpl
package dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import dao.MemberDao;
public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao {
//省略了其他的代码
/** *//**
* 分页查询
* @param hql 查询的条件
* @param offset 开始记录
* @param length 一次查询几条记录
* @return
*/
public List queryForPage(final String hql,final int offset,final int length){
List list = getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException,SQLException{
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}
/** *//**
* 查询所有记录数
* @return 总记录数
*/
public int getAllRowCount(String hql){
return getHibernateTemplate().find(hql).size();
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<action name="list" class="listmember" method="list">
<result type="success">/listmember.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
spring xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="listmember" class="action.ListMemberAction">
<property name="memberService">
<ref local="memberService"></ref>
</property>
</bean>
<bean id="memberService" class="service.impl.MemberServiceImpl">
<property name="memberDao">
<ref local="memberDao"></ref>
</property>
</bean>
<bean id="memberDao" class="dao.impl.MemberDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
</beans>
hibernate
<?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="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/dbshop
</property>
<property name="connection.username">root</property>
<property name="connection.password">HKEA60828</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
mysqldriver
</property>
<mapping resource="pojo/TbUser.hbm.xml" />
</session-factory>
</hibernate-configuration>
/**
* TbUserId entity. @author MyEclipse Persistence Tools
*/
public class TbUserId implements java.io.Serializable {
// Fields
private Integer id;
private String username;
private String password;
private String userLastLogTime;
// Constructors
/** default constructor */
public TbUserId() {
}
/** full constructor */
public TbUserId(Integer id, String username, String password,
String userLastLogTime) {
this.id = id;
this.username = username;
this.password = password;
this.userLastLogTime = userLastLogTime;
}
// Property accessors
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;
}
public String getUserLastLogTime() {
return this.userLastLogTime;
}
public void setUserLastLogTime(String userLastLogTime) {
this.userLastLogTime = userLastLogTime;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof TbUserId))
return false;
TbUserId castOther = (TbUserId) other;
return ((this.getId() == castOther.getId()) || (this.getId() != null
&& castOther.getId() != null && this.getId().equals(
castOther.getId())))
&& ((this.getUsername() == castOther.getUsername()) || (this
.getUsername() != null
&& castOther.getUsername() != null && this
.getUsername().equals(castOther.getUsername())))
&& ((this.getPassword() == castOther.getPassword()) || (this
.getPassword() != null
&& castOther.getPassword() != null && this
.getPassword().equals(castOther.getPassword())))
&& ((this.getUserLastLogTime() == castOther
.getUserLastLogTime()) || (this.getUserLastLogTime() != null
&& castOther.getUserLastLogTime() != null && this
.getUserLastLogTime().equals(
castOther.getUserLastLogTime())));
}
public int hashCode() {
int result = 17;
result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());
result = 37 * result
+ (getUsername() == null ? 0 : this.getUsername().hashCode());
result = 37 * result
+ (getPassword() == null ? 0 : this.getPassword().hashCode());
result = 37
* result
+ (getUserLastLogTime() == null ? 0 : this.getUserLastLogTime()
.hashCode());
return result;
}
}
Action
package action;
import service.MemberService;
import bean.PageBean;
import com.opensymphony.xwork2.ActionSupport;
public class ListMemberAction extends ActionSupport{
//通过applicationContext.xml配置文件注入memberService的值
private MemberService memberService;
public void setMemberService(MemberService memberService) {
this.memberService = memberService;
}
private int page; //第几页
public MemberService getMemberService() {
return memberService;
}
private PageBean pageBean; //包含分布信息的bean
public int getPage() {
return page;
}
public void setPage(int page) { //若URL中无此参数,会默认为第1页
this.page = page;
}
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
public String list() throws Exception{
this.pageBean = memberService.queryForPage(2, page);
//listmember=memberService.list();
return SUCCESS;
}
@Override
public String execute() throws Exception {
//分页的pageBean,参数pageSize表示每页显示记录数,page为当前页
this.pageBean = memberService.queryForPage(2, page);
return SUCCESS;
}
}
DaoImpl
package dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import dao.MemberDao;
public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao {
//省略了其他的代码
/** *//**
* 分页查询
* @param hql 查询的条件
* @param offset 开始记录
* @param length 一次查询几条记录
* @return
*/
public List queryForPage(final String hql,final int offset,final int length){
List list = getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException,SQLException{
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}
/** *//**
* 查询所有记录数
* @return 总记录数
*/
public int getAllRowCount(String hql){
return getHibernateTemplate().find(hql).size();
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<action name="list" class="listmember" method="list">
<result type="success">/listmember.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
spring xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="listmember" class="action.ListMemberAction">
<property name="memberService">
<ref local="memberService"></ref>
</property>
</bean>
<bean id="memberService" class="service.impl.MemberServiceImpl">
<property name="memberDao">
<ref local="memberDao"></ref>
</property>
</bean>
<bean id="memberDao" class="dao.impl.MemberDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
</beans>
hibernate
<?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="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/dbshop
</property>
<property name="connection.username">root</property>
<property name="connection.password">HKEA60828</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
mysqldriver
</property>
<mapping resource="pojo/TbUser.hbm.xml" />
</session-factory>
</hibernate-configuration>