- package domain;
- import java.io.Serializable;
- import java.sql.Date;
- public class TbUser implements Serializable {
- private Integer id;
- private String name;
- private String gender;
- private Integer age;
- private Date birthday;
- private String phone;
- private String address;
- private String username;
- private String password;
- /*省略get(),set()*/
- }
TbUser.hbm.xml
- <?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">
- <hibernate-mapping package="domain">
- <class name="TbUser" table="tb_user">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="name" length="20"/>
- </property>
- <property name="gender" type="java.lang.String">
- <column name="gender" length="6"/>
- </property>
- <property name="age" type="java.lang.Integer">
- <column name="age"/>
- </property>
- <property name="birthday" type="java.sql.Date">
- <column name="birthday"/>
- </property>
- <property name="phone" type="java.lang.String">
- <column name="phone" length="11"/>
- </property>
- <property name="address" type="java.lang.String">
- <column name="address" length="50"/>
- </property>
- <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>
- package dao;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.cfg.Configuration;
- public class HibernateSessionFactory {
- private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
- private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
- private static Configuration configuration = new Configuration();
- private static org.hibernate.SessionFactory sessionFactory;
- private static String configFile = CONFIG_FILE_LOCATION;
- static {
- try {
- configuration.configure(configFile);
- sessionFactory = configuration.buildSessionFactory();
- } catch (Exception e) {
- System.err
- .println("%%%% Error Creating SessionFactory %%%%");
- e.printStackTrace();
- }
- }
- private HibernateSessionFactory() {
- }
- public static Session getSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- if (session == null || !session.isOpen()) {
- if (sessionFactory == null) {
- rebuildSessionFactory();
- }
- session = (sessionFactory != null) ? sessionFactory.openSession()
- : null;
- threadLocal.set(session);
- }
- return session;
- }
- public static void rebuildSessionFactory() {
- try {
- configuration.configure(configFile);
- sessionFactory = configuration.buildSessionFactory();
- } catch (Exception e) {
- System.err
- .println("%%%% Error Creating SessionFactory %%%%");
- e.printStackTrace();
- }
- }
- public static void closeSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- threadLocal.set(null);
- if (session != null) {
- session.close();
- }
- }
- public static org.hibernate.SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public static void setConfigFile(String configFile) {
- HibernateSessionFactory.configFile = configFile;
- sessionFactory = null;
- }
- public static Configuration getConfiguration() {
- return configuration;
- }
- }
- package dao;
- import util.Page;
- import domain.TbUser;
- public interface UserDAO {
- public TbUser findByUsernamePassword(String username,String password);
- public Page findByPageNo(int pageNo,int pageSize);
- }
- package dao;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import util.Page;
- import domain.TbUser;
- public class UserDAOImpl implements UserDAO {
- private Session session = HibernateSessionFactory.getSession();
- public Page findByPageNo(int pageNo, int pageSize) {
- Page page = new Page();
- Query query = session.createQuery("select count(*) from TbUser");
- int count = Integer.parseInt(query.uniqueResult().toString());
- query = session.createQuery("from TbUser");
- query.setMaxResults(pageSize);
- query.setFirstResult((pageNo-1)*pageSize);
- page.setList(query.list());
- page.setTotal(count);
- page.setPageNo(pageNo);
- page.setPageSize(pageSize);
- return page;
- }
- public TbUser findByUsernamePassword(String username, String password) {
- String hql = "from TbUser where username=? and password=?";
- Query query = session.createQuery(hql);
- query.setParameter(0, username);
- query.setParameter(1, password);
- return (TbUser) query.uniqueResult();
- }
- }
- package service;
- import util.Page;
- import domain.TbUser;
- public interface UserService {
- public TbUser findByUsernamePassword(String username,String password);
- public Page findByPageNo(int pageNo,int pageSize);
- }
- package service;
- import util.Page;
- import dao.UserDAO;
- import domain.TbUser;
- public class UserServiceImpl implements UserService {
- private UserDAO userDAO;
- public Page findByPageNo(int pageNo, int pageSize) {
- return userDAO.findByPageNo(pageNo, pageSize);
- }
- public TbUser findByUsernamePassword(String username, String password) {
- return userDAO.findByUsernamePassword(username, password);
- }
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- public UserDAO getUserDAO() {
- return userDAO;
- }
- }
- package util;
- import java.util.List;
- public class Page {
- private int total;
- private int pageSize;
- private int totalPage;
- private int pageNo;
- private int prePage;
- private int nextPage;
- private List list;
- public int getTotal() {
- return total;
- }
- public void setTotal(int total) {
- this.total = total;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getTotalPage() {
- if(total%pageSize == 0){
- totalPage = total/pageSize;
- }else{
- totalPage = total/pageSize + 1;
- }
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- public int getPageNo() {
- return pageNo;
- }
- public void setPageNo(int pageNo) {
- this.pageNo = pageNo;
- }
- public int getPrePage() {
- if(pageNo == 1){
- prePage = pageNo;
- }else{
- prePage = pageNo - 1;
- }
- return prePage;
- }
- public void setPrePage(int prePage) {
- this.prePage = prePage;
- }
- public int getNextPage() {
- if(pageNo == totalPage){
- nextPage = pageNo;
- }else{
- nextPage = pageNo + 1;
- }
- return nextPage;
- }
- public void setNextPage(int nextPage) {
- this.nextPage = nextPage;
- }
- public List getList() {
- return list;
- }
- public void setList(List list) {
- this.list = list;
- }
- }
applicationContext.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation"
- value="classpath:hibernate.cfg.xml" />
- </bean>
- <bean id="userDAO" class="dao.UserDAOImpl"/>
- <bean id="userService" class="service.UserServiceImpl">
- <property name="userDAO">
- <ref local="userDAO"/>
- </property>
- </bean>
- <bean id="loginAction" class="action.UserAction">
- <property name="userService">
- <ref local="userService"/>
- </property>
- </bean>
- </beans>
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">
- <hibernate-configuration>
- <session-factory>
- <property name="connection.username">root</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
- <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.password">1101</property>
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
- <property name="hbm2ddl.auto">update</property>
- <mapping resource="domain/TbUser.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
native_zh_CN.properties
- user.username=/u7528/u6237/u540d
- user.password=/u5bc6/u7801
- login=/u767b/u5f55
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.ui.theme" value="simple"/>
- <constant name="struts.custom.i18n.resources" value="native"/>
- <constant name="struts.objectFactory" value="spring"/>
- <package name="user" namespace="/user" extends="struts-default">
- <action name="*" class="loginAction" method="{1}">
- <result name="success">/index.jsp</result>
- <result name="input">/login.jsp</result>
- <result name="browse">/index.jsp</result>
- </action>
- </package>
- </struts>
login.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登录</title>
- </head>
- <body>
- <fieldset>
- <legend>系统登录</legend>
- <s:form action="user/login" method="post">
- <s:text name="user.username"/><s:textfield name="username" key="user.username" label="%{getText('user.username')}"/>
- <s:text name="user.password"/><s:password name="password" key="user.password" label="%{getText('user.password')}"/>
- <s:submit key="login"/>
- </s:form>
- </fieldset>
- </body>
- </html>
index.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>首页</title>
- </head>
- <body>
- ${message }<hr/>
- <a href="user/browse" mce_href="user/browse">用户列表</a><br/>
- <table>
- <tr>
- <th>ID</th>
- <th>姓名</th>
- <th>性别</th>
- <th>年龄</th>
- <th>生日</th>
- <th>电话</th>
- <th>地址</th>
- </tr>
- <c:forEach items="${userList}" var="user">
- <tr>
- <td>${user.id }</td>
- <td>${user.name }</td>
- <td>${user.gender }</td>
- <td>${user.age }</td>
- <td>${user.phone }</td>
- <td>${user.address }</td>
- </tr>
- </c:forEach>
- </table>
- 共${page.totalPage }页|当前第${page.pageNo }页|共${page.total }条记录<br/>
- <!-- 分页列表 -->
- <c:if test="${page.totalPage != 1}">
- <!-- 首页情况 -->
- <c:if test="${page.pageNo == 1}">
- <font color="gray">首页 上一页</font>
- <a href="user/browse?pageNo=${page.nextPage }" mce_href="user/browse?pageNo=${page.nextPage }">下一页</a>
- <a href="user/browse?pageNo=${page.totalPage }" mce_href="user/browse?pageNo=${page.totalPage }">尾页</a>
- </c:if>
- <!-- 尾页情况 -->
- <c:if test="${page.pageNo == page.totalPage}">
- <a href="user/browse?pageNo=1" mce_href="user/browse?pageNo=1">首页</a>
- <a href="user/browse?pageNo=${page.prePage }" mce_href="user/browse?pageNo=${page.prePage }">上一页</a>
- <font color="gray">下一页 尾页</font>
- </c:if>
- <!-- 普通情况 -->
- <c:if test="${page.pageNo!=1 and page.pageNo!=page.totalPage}">
- <a href="user/browse?pageNo=1" mce_href="user/browse?pageNo=1">首页</a>
- <a href="user/browse?pageNo=${page.prePage }" mce_href="user/browse?pageNo=${page.prePage }">上一页</a>
- <a href="user/browse?pageNo=${page.nextPage }" mce_href="user/browse?pageNo=${page.nextPage }">下一页</a>
- <a href="user/browse?pageNo=${page.totalPage }" mce_href="user/browse?pageNo=${page.totalPage }">尾页</a>
- </c:if>
- </c:if>
- <!-- 分页列表 -->
- </body>
- </html>