ssh2

application文件

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
 
 <!-- 配置属性文件 -->
 <context:property-placeholder location="classpath:database.properties"/>




<!--spring管理bean -->
<bean id="userDao" class="cn.bdqn.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="userService" class="cn.bdqn.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean> 


<bean id="userAction" class="cn.bdqn.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>



<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置会话工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 基本属性 -->
<property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
   </props>
</property>
<!-- 引入映射文件 -->
<property name="mappingResources">
<list>
<value>cn/bdqn/entity/hbm/User.hbm.xml</value>
</list>
</property>
</bean>

<!-- 配置事务管理器  -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <!-- 注入会话工厂 -->
     <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 注解管理事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>


struts文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "struts-2.3.dtd" >
<struts>
<!--  action由spring管理  -->
<constant name="struts.objectFactory" value="spring"></constant>

<package name="default" extends="struts-default" namespace="/">
<action name="login" class="userAction" method="login">
<result name="success">/successPage.jsp</result>
</action>
</package>

</struts>


database文件

jdbc.driver=org.gjt.mm.mysql.Driver
jdbc.url=jdbc:mysql://localhost:3306/hibernate_clazz
jdbc.username=root

jdbc.password=root



分页语句

 

 

<!--表格内容代码-->

<tbody>

<s:iterator value="pageUtil.list">

<tr id="row_1">

<td><s:property value="userId" /></td>

<td name="name"><s:property value="userName" /></td>

<td><a href="#"> <i class="fa fa-pencil-square-o"

title="编辑" aria-hidden="true"></i>

</a> <a href="#"> <i class="fa fa-trash" title="删除"

aria-hidden="true"></i>

</a></td>

</tr>

</s:iterator>

</tbody>

</table>

<!-- 分页器 start -->

<div class="pageStyle">

<span><b> <s:property value="pageUtil.totalCount" />

</b>  第 <s:property value="pageUtil.pageIndex" /><s:property

value="pageUtil.totalPageCount" />  

</span>

<ul class="pagination">

<li><a href="javascript:gotoPage(<s:property value='1'/>)">首页</a></li>

<li><a

href="javascript:gotoPage(<s:property value='pageUtil.prePageIndex'/>)">上页</a></li>

<s:bean name="org.apache.struts2.util.Counter" var="counter">

<s:param name="first" value="1" />

<s:param name="last" value="pageUtil.totalPageCount" />

<s:iterator>

<li

<s:if test="pageUtil.pageIndex==#counter.current-1">

   class="active"

   </s:if>><a

href="javascript:gotoPage(<s:property value='#counter.current-1'/>)">

<s:property value="#counter.current-1" />

</a></li>

</s:iterator>

</s:bean>

<li><a

href="javascript:gotoPage(<s:property value='pageUtil.nextPageIndex'/>)">下页</a></li>

<li><a

href="javascript:gotoPage(<s:property value='pageUtil.totalPageCount'/>)">尾页</a></li>

</ul>

</div>

<!-- 分页器 end -->

</div>

</body>

<script>

   function gotoPage(pageNum){

   var pageIndex =  document.getElementById("pageIndex");

   pageIndex.value = pageNum;

   //js提交表单

   document.getElementById("searchForm").submit();

   }

</script>

@Resource

private UserService userService;

private Integer pageIndex;

private Integer pageSize;

//搜索数据

private String username;

private PageUtil<User> pageUtil;

public PageUtil<User> getPageUtil() {

return pageUtil;

}

public void setPageIndex(Integer pageIndex) {

this.pageIndex = pageIndex;

}

public void setPageSize(Integer pageSize) {

this.pageSize = pageSize;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

@Action(value = "login", results = { @Result(name = ActionSupport.SUCCESS, location = "/successPage.jsp") })

public String login() {

System.out.println("UserAction-----------login");

userService.updateUser();

return ActionSupport.SUCCESS;

}

@Action(value = "indexSupper", results = { @Result(name = ActionSupport.SUCCESS, location = "/index-supper.jsp") })

public String indexSupper() {

if(username==null){

username = "";

}

         //统计总条数

  int totalCount = userService.countUser(username);

  if(pageIndex==null){

  pageIndex = 1;

  }

   pageUtil = new PageUtil<User>(totalCount,pageIndex);

  List<User> userList = userService.findUserListByPage(username,pageUtil.getDatabaseOffset(),pageUtil.getPageSize());

  pageUtil.setList(userList);  

return ActionSupport.SUCCESS;

}

 

 

 

 

 

6.1. Dao

public int countUser(String username) {

int count = hibernateTemplate.execute(new HibernateCallback<Integer>() {

@Override

public Integer doInHibernate(Session sessionthrows HibernateException, SQLException {

Query query = session.createQuery("select count(userId) from User where userName like concat('%',?,'%')");

query.setString(0, username);

Integer count = ((Long)query.uniqueResult()).intValue();

return count;

}

});

return count;

}

public List<User> findUserListByPage(String usernameint databaseOffsetint pageSize) {

List<User> userList = hibernateTemplate.execute(new HibernateCallback<List<User>>(){

@Override

public List<User> doInHibernate(Session sessionthrows HibernateException, SQLException {

 Criteria criteria = session.createCriteria(User.class);

 criteria.add(Restrictions.like("userName""%"+username+"%"));//模糊查询

 criteria.setFirstResult(databaseOffset);

 criteria.setMaxResults(pageSize);

return criteria.list();

}});

return userList;

}

 

 

 

 

 

public class PageUtil<T> {

//默认每页显示的数量为5

private final static int DEFAULT_PAGESIZE =  5;

private int   totalCount;

private int   totalPageCount;

private int   pageIndex;

private int   pageSize;

private List<T>  list;

private int prePageIndex;

private int nextPageIndex;

public PageUtil(int totalCountint pageIndex) {

this.totalCount = totalCount;

this.pageIndex = pageIndex;

this.pageSize = DEFAULT_PAGESIZE;

}

public PageUtil(int totalCountint pageIndexint pageSize) {

this(totalCount,pageIndex);

this.totalCount = totalCount;

this.pageIndex = pageIndex;

this.pageSize = pageSize;

}

public int getTotalCount() {

return totalCount;

}

public void setTotalCount(int totalCount) {

this.totalCount = totalCount;

}

public int getTotalPageCount() {

//计算

return totalCount%pageSize==0?(totalCount/pageSize):(totalCount/pageSize+1);

}

public void setTotalPageCount(int totalPageCount) {

this.totalPageCount = totalPageCount;

}

public int getPageIndex() {

return pageIndex;

}

public void setPageIndex(int pageIndex) {

this.pageIndex = pageIndex;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public int getPrePageIndex() {

if(pageIndex<=1){

return 1;

}else{

return  pageIndex-1;

}

}

public void setPrePageIndex(int prePageIndex) {

this.prePageIndex = prePageIndex;

}

public int getNextPageIndex() {

if(pageIndex>=getTotalPageCount()){

return pageIndex;

}else{

return  pageIndex+1;

}

}

public void setNextPageIndex(int nextPageIndex) {

this.nextPageIndex = nextPageIndex;

}

public List<T> getList() {

return list;

}

public void setList(List<T> list) {

this.list = list;

}

//数据库的下标

public int getDatabaseOffset(){

return (pageIndex-1)*pageSize;

}


猜你喜欢

转载自blog.csdn.net/yzqtjgb/article/details/80636220