说明:
1、后台程序是参考本公司“杜师傅”的例子!加上自学了ligerui,整合来使用!
2、最近在做一个项目,使用到了上面的技术,不知道现在ssh发展到了上面地步,是不是已经快淘汰了,发现springMVC+mybatis、hibernate+esayui、juqeryui等是企业级应用比较广泛,本人在大学期间,就是学习ssh整合开发,个人感觉现在ssh在java企业应用中还是普遍存在的吧!
3、重在个人总结,技术积累,有什么不好的地方,还请各位牛人指点!相互学习、相互进步。
话不多说,下面是整合信息
一、项目结构:
二、把需要的jar包添加到lib目录,配置web.xml、applicationContext.xml
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoader</listener-class>
</listener>
<display-name>Struts Blank</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- <filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.filter.CharsetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="TransactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="sum*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* com.dao.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
</aop:config>
<!-- Dao Beans -->
<bean id="UserDao" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Service Beans -->
<bean id="UserService" class="com.service.UserService">
<property name="userdao" ref="UserDao"/>
</bean>
</beans>
三、dao
package com.dao.impl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.common.PageUnit;
import com.dao.UserDao;
import com.tool.DBEncoding;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public List userList(Map data,int pageNow,int pageSize) throws Exception{
Session session=this.getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select " +
"t.ID," +
"t.SFZH," +
"t.YHXM," +
"t.YHXB," +
"decode(t.YHXB,'0','男','1','女','') as STR_YHXB," +
"t.PHONE," +
"t.PASSWORD," +
"t.CSRQ," +
"t.UPDATENUMBER," +
"to_char(t.ADDDATE,'yyyy-MM-dd hh24:mi:ss') as ADDDATE," +
"t.YXBS " +
" from USERS t " +
" where 1=1 ";
//" and t.YXBS=0";
String sql_where="";
List<String> dataList=new ArrayList<String>();
if(data!=null&&data.size()>0)
{
if(data.get("SFZH")!=null)
{
sql_where+=" and t.SFZH like ? ";
dataList.add("%"+data.get("SFZH")+"%");
}
if(data.get("YHXM")!=null)
{
sql_where+=" and t.YHXM like ? ";
dataList.add("%"+data.get("YHXM")+"%");
}
}
sql+=sql_where+"order by t.ADDDATE desc";
String sql_pageHear="select * from ( select zt.*,rownum m from ( ";
String sql_pageFoot=" ) zt where rownum<=? ) where m>?";
String sql_all=sql_pageHear+sql+sql_pageFoot;
PreparedStatement ps=session.connection().prepareStatement(sql_all);
int index=1;
if(dataList!=null&&dataList.size()>0)
{
for(int i=0;i<dataList.size();i++)
{
ps.setString(index++, dataList.get(i));
}
}
ps.setInt(index++, pageNow*pageSize);
ps.setInt(index++, (pageNow-1)*pageSize);
System.out.println("executeSQL:"+sql_all);
ResultSet rs=ps.executeQuery();
List l=null;
try
{
if(rs!=null)
{
int pageCount=PageUnit.getPageCount(session,sql, dataList, pageSize);
int Total = PageUnit.getRowCount(session, sql, dataList);
l=new ArrayList();
while(rs.next())
{
Map m=new HashMap();
for(int i=0;i<rs.getMetaData().getColumnCount();i++)
{
String key=rs.getMetaData().getColumnName(i+1);
m.put(key, DBEncoding.EncodingOutputDB(rs.getString(key)));
if("null".equals(m.get(key)+""))
{
m.put(key,"");
}
}
m.put("pageCount", pageCount);
m.put("Total", Total);
l.add(m);
}
}
releaseSession(session);
}
finally
{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
}
return l;
}
}
dao中的分页类
package com.common;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import org.hibernate.Session;
import com.tool.DBEncoding;
public class PageUnit{
public static int getPageCount(Session session,String sql, List<String> data, int pageSize)
throws Exception {
int pageCount=0;
int rowCount=0;
sql = "select count(*) from (" +sql+")";
PreparedStatement ps=session.connection().prepareStatement(sql);
int index=1;
if(data!=null&&data.size()>0)
{
for(int i=0;i<data.size();i++)
{
ps.setString(index++, DBEncoding.EncodingInputDB(data.get(i)+""));
}
}
System.out.println("executeSQL:"+sql);
ResultSet rs=ps.executeQuery();
try
{
if(rs!=null)
{
if(rs.next())
{
rowCount=Integer.parseInt(rs.getInt(1)+"");
}
}
if(rowCount%pageSize==0)
{
pageCount=rowCount/pageSize;
}
else
{
pageCount=rowCount/pageSize+1;
}
}
finally
{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
}
return pageCount;
}
public static int getRowCount(Session session,String sql,List<String> data) throws Exception{
//int pageCount=0;
int rowCount=0;
sql = "select count(*) from (" +sql+")";
PreparedStatement ps=session.connection().prepareStatement(sql);
int index=1;
if(data!=null&&data.size()>0)
{
for(int i=0;i<data.size();i++)
{
ps.setString(index++, DBEncoding.EncodingInputDB(data.get(i)+""));
}
}
System.out.println("executeSQL:"+sql);
ResultSet rs=ps.executeQuery();
try
{
if(rs!=null)
{
if(rs.next())
{
rowCount=Integer.parseInt(rs.getInt(1)+"");
}
}
}
finally
{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
}
return rowCount;
}
}
service类
package com.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.dao.UserDao;
public class UserService {
private UserDao userdao;
public UserDao getUserdao() {
return userdao;
}
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
/**
* 带分页的查询用户信息
* @param data
* @param pageNow
* @param pageSize
* @return
*/
public List userList(Map data,int pageNow,int pageSize){
List list = new ArrayList();
try {
list = userdao.userList(data, pageNow, pageSize);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
这时候应该写个测试类来测试一下结果
package com.test;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.factory.SpringContextFactory;
import com.service.UserService;
public class UserDaoTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext act=SpringContextFactory.getSpringContext();
UserService us = (UserService)act.getBean("UserService");
List list = us.userList(null, 1, 10);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
package com.factory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringContextFactory {
private static ClassPathXmlApplicationContext act=null;
public static ClassPathXmlApplicationContext getSpringContext()
{
if(act==null)
{
act=new ClassPathXmlApplicationContext("applicationContext.xml");
}
return act;
}
}
action 类
package com.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.factory.SpringContextFactory;
import com.opensymphony.xwork2.ActionSupport;
import com.service.UserService;
public class UserAction extends ActionSupport{
private int pageNow = 1;
private int pageSize = 10;
private JSONObject Rows;
private String action;
@SuppressWarnings("unchecked")
public String execute(){
//HttpServletRequest request=ServletActionContext.getRequest();
ClassPathXmlApplicationContext act=SpringContextFactory.getSpringContext();
UserService us = (UserService)act.getBean("UserService");
if(action.equals("ulist")){
List userList = us.userList(null, pageNow, pageSize);
/**
* json
*/
HashMap maprow = new HashMap();
if(userList!=null&&userList.size()>0){
String Total=(((Map)userList.get(0)).get("Total"))+"";
maprow.put("Rows", userList);
maprow.put("Total", Total);
}
Rows =JSONObject.parseObject(JSON.toJSONString(maprow));
System.out.println(Rows);
return SUCCESS;
}
return ERROR;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public JSONObject getRows() {
return Rows;
}
public void setRows(JSONObject rows) {
Rows = rows;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
}
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>
<package name="struts2" extends="json-default">
<action name="UserAction" class="com.action.UserAction">
<result type="json">
<param name="root">Rows</param>
</result>
</action>
</package>
</struts>
下面来使用ligerui的ligerGrid查询显示列表数据,userlist.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<%@include file="include/head.jsp" %>
<script type="text/javascript">
$(function(){
$("#mainGrid").ligerGrid({
title:"用户列表",
columns:[
{display:"主键",name:"ID",hide:"hidden"},
{display:"身份证号",name:"SFZH",width:200},
{display:"用户姓名",name:"YHXM",width:100},
{display:"用户性别",name:"STR_YHXB"},
{display:"手机号码",name:"PHONE"},
{display:"出生日期",name:"CSRQ"},
{display:"录入时间",name:"ADDDATE"},
{display:"有效标示",name:"YXBS"}
],
//data:docData,
url:'UserAction.action?action=ulist',
sortName: 'au_id',
width:"100%",height:"100%",checkbox:true,
pagesizeParmName:"pageSize",
pageParmName:"pageNow",
pageSize:10,
rowHeight:40,
rownumbers:true,usePager:true
});
})
</script>
</head>
<body>
<div id="mainGrid"></div>
</body>
</html>
显示结果
[img][/img]
只写了个查询的列子,后面的增、删、改都差不多啦!