java架构搭建(三)--测试模块编写

此篇继续上篇  http://blog.csdn.net/lushuaiyin/article/details/8588420

ssh已经整合完毕,现在做一个功能模块测试是否可用。

这个功能我们就叫first。

java代码结构图:


jsp路径结构图:


下面贴出代码:

FirstIndexAction

package org.first.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.base.MyBaseAction;
import org.first.dao.FirstDao;

import com.opensymphony.xwork2.ActionContext;

public class FirstIndexAction  extends MyBaseAction {
	private static final long serialVersionUID = 1L;
	
	public String execute() throws Exception{
		
		return SUCCESS;
	}	
	public String firstPage() throws Exception{
		//继承了MyBaseAction,以下这些调用就简单很多
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE); 
		HttpServletRequest request  = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		HttpSession  session  = request.getSession();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		return SUCCESS;
	}
	
	public String queryUser() throws Exception{
		String realName="";
		if(this.getValueFromRequest("realName")!=null){
			realName=(String)this.getValueFromRequest("realName");
		}
		List list=firstDao.queryUsers(realName);
		this.setValueToRequest("userList", list);
		return SUCCESS;
	}
	
	private FirstDao firstDao;

	public FirstDao getFirstDao() {
		return firstDao;
	}
	public void setFirstDao(FirstDao firstDao) {
		this.firstDao = firstDao;
	}
}

LsyUser

package org.first.bean;

public class LsyUser {
	
	private String user_id;
	private String user_name;
	private String real_name;
	
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getReal_name() {
		return real_name;
	}
	public void setReal_name(String real_name) {
		this.real_name = real_name;
	}

}

context_first.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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<bean id="firstDao" parent="transactionProxyTemplate">
		<property name="target">
			<bean class=" org.first.dao.impl.FirstDaoImpl">
				<property name="sessionFactory">
					<ref bean="sessionFactory"/>
				</property>
			</bean>
		</property>
		<property name="proxyInterfaces">
			<value> org.first.dao.FirstDao</value>
		</property>
	</bean>
</beans>


LsyUser.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	    <class name="org.first.bean.LsyUser" table="LSY_USER_DEVELOP" >
        <id name="user_id" type="java.lang.String">
            <column name="user_id" length="32" />
            <generator class="assigned" />
        </id>
        <property name="user_name" type="java.lang.String">
            <column name="user_name" length="32" />
        </property>
        <property name="real_name" type="java.lang.String">
            <column name="real_name" length="32" />
        </property>
    </class>
</hibernate-mapping>
数据库表我就不说了,就是很简单的3个字段,都是varcher类型。


struts_first.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="first" extends="base-struts-default" namespace="/first">
	    <!-- 首页 -->	
		<action name="firstPage" class="org.first.action.FirstIndexAction" method="firstPage">
		   <result name="success">/jsp/first/firstPage.jsp</result>
		</action>
		<action name="queryUser" class="org.first.action.FirstIndexAction" method="queryUser">
		   <result name="success">/jsp/first/listUser.jsp</result>
		</action>
	</package>
</struts>

FirstDao

package org.first.dao;

import java.util.List;

public interface FirstDao {
	
	public List queryUsers(String realName);

}

FirstDaoImpl

package org.first.dao.impl;

import java.util.List;

import org.base.MyHibernateDao;
import org.first.dao.FirstDao;

public class FirstDaoImpl extends MyHibernateDao implements FirstDao{
	
	public List queryUsers(String realName){
		List list=null;
		if(realName==null||realName.trim().equals("")){
			System.out.println("参数realName为空,查询所有值。");
			String hql="select u from LsyUser u ";
			list=this.queryListHql(hql);
		}else{
			String hql="select u from LsyUser u where u.real_name like '%"+realName.trim()+"%'";
			list=this.queryListHql(hql);
		}
		return list;
	}
}

index.jsp(项目首页)

<%@ page contentType="text/html; charset=UTF-8"%>
<% 
String path = request.getContextPath();
%>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<script src="<%=path%>/script/jquery-1.7.1.min.js" type="text/javascript"></script>	
</head>
<body style="overflow: scroll; overflow: auto;">

<input type="hidden" name="path" id="path" value='<%=path%>' ></input>

    <center>
     <h1>首页</h1>
    </center>
    <table width="500px" align="center">
       <tr width="100%">
           <td >账号:</td>
           <td ><input type="text" value="" id="username"></input></td>
           
           <td >密码:</td>
           <td ><input type="text" value="" id="password"></input></td>
       </tr>
       
       <tr width="100%">
           <td >查询用户:</td>
           <td ><input type="radio"  checked value="1" name="logintype"></input></td>
           
           <td >查询部门:</td>
          <td ><input type="radio"   value="2" name="logintype"></input></td>
       </tr>
       
       <tr width="100%">
           <td ></td>
           <td ><input type="button" value="登录" οnclick="login()"></input></td>
           
           <td ></td>
           <td ><input type="button" value="重置" οnclick="resetValue()"></input></td>
       </tr>
       
    </table>
</body>
</html> 

<script type="text/javascript">	
//简单的登录
function login(){
	var username=document.getElementById("username").value;
	var password=document.getElementById("password").value;
	if(username!=null&&username!=""){
		var urlpath="<%=path%>"+"/jsp/first/firstPage.jsp?randomStr="+Math.random();
		var urlpath2="<%=path%>"+"/jsp/second/secondPage.jsp?randomStr="+Math.random();
		var type=getRadioGroupValue();
		if(type=="2"){
			window.location.href=urlpath2;
		}else{
			window.location.href=urlpath;
		}
	}else{
		alert("账号密码不正确!");
	}
	
}

function resetValue(){
	document.getElementById("username").value="";
	document.getElementById("password").value="";
}

//js获取单选按钮组的值
function getRadioGroupValue(){
	var result="";
	var logintype=document.getElementsByName("logintype");
	if(logintype!=null&&(typeof logintype !="undifined")){
		for(i=0;i<logintype.length;i++){
			if(logintype[i].checked){
				result=logintype[i].value;
			}
		}
	}
	return result;
}
</script>

firstPage.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<% 
String path = request.getContextPath();
%>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<script src="<%=path%>/script/jquery-1.7.1.min.js" type="text/javascript"></script>	
</head>
<body style="overflow: scroll; overflow: auto;">

<input type="hidden" name="path" id="path" value='<%=path%>' ></input>
<center><h1>查询用户</h1></center>
    <table width="500px"  border="1" align="center">
        <tr>
			 <td>姓名:<input type="text"  id="realName" value='' ></input></td>
		</tr>
		<tr>
			 <td><h1><a href="javascript:void(0)" οnclick="chaxun()">查询</a></h1></td>
		</tr>
	</table>
	
</body>
</html> 
<script type="text/javascript">	
//查询列表
function chaxun(){
	var realName=document.getElementById("realName").value;
	var urlpath="<%=path%>/first/queryUser.action?randomStr="+Math.random()+"&realName="+realName;
	window.location.href=urlpath;
}


  
</script>

listUser.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="java.util.*"%>
<%@page import=" org.first.bean.LsyUser"%>
<% 
String path = request.getContextPath();
%>
<% 
List list=null;
if(request.getAttribute("userList")!=null){
	list=(List)request.getAttribute("userList");
}
%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<script src="<%=path%>/script/jquery-1.7.1.min.js" type="text/javascript"></script>	
</head>
<body style="overflow: scroll; overflow: auto;">

<input type="hidden" name="path" id="path" value='<%=path%>' ></input>

      <table width="500px"  border="1" align="center">
        <tr>
			 <td align="center">姓名:<input type="text"  id="realName" value='' ></input></td>
		</tr>
		<tr>
			 <td align="center"><h1><a href="javascript:void(0)" οnclick="queryuser()">查询</a></h1></td>
		</tr>
	</table>
	
	 <table width="500px"  border="1" align="center">
        <tr>
			 <th align="center">姓名</th><th align="center">账号</th>
		</tr>
		<% 
		if(list!=null){
			for(int i=0;i<list.size();i++){
				LsyUser user=(LsyUser)list.get(i);
				if(user!=null){
					String realname="";
					String user_name="";
					if(user.getReal_name()!=null){
						realname=user.getReal_name();
					}
					if(user.getUser_name()!=null){
						user_name=user.getUser_name();
					}
		%>			
				<tr>
					 <td><%=realname%></td><td><%=user_name%></td>
				</tr>
		<%	
				}
			}
		}
		%>
		
	</table>
</body>
</html> 
<script type="text/javascript">	

function queryuser(){
	var urlpath="<%=path%>"+"/first/queryUser.action";
	var realName=document.getElementById("realName").value;
	$.ajax({
		   type:"POST",
		   url:urlpath,
		   data: "realName="+realName+"&randomStr="+Math.random(),
		   success: function(msg){
		     alert( "Succeed:");
		     window.location.reload();
		   }
		});
}


</script>



此功能我已经测试过,能正常使用。代码就不讲解了,就是很简单的一个查询。目的只是为了测试ssh的整合是否成功。


Hibernate这个框架的好处就是帮我们处理持久层的东西。

如果我想把数据库从orcle移植到mysql,利用hibernate就很简单。

步骤:

1:修改对sessionFactory的配置。

因为属性的值都放到properties中了,所以我只需要修改configure.properties,如下:

# applicationContext.xml

### C3P0 Connection Pool
c3p0.maxPoolSize=3
c3p0.minPoolSize=1
c3p0.maxIdleTime=1800
c3p0.maxStatements=0
c3p0.acquireIncrement=2
c3p0.idleConnectionTestPeriod=600

#oracle  
#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@10.55.15.66:1521:cdbank
#jdbc.username=ccdb
#jdbc.password=ccdb
#hibernate.dialect=org.hibernate.dialect.Oracle9Dialect

#mysql
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mydb
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

2:记得添加驱动包mysql-connector-java-5.1.13-bin.jar,并引入classpath。

3:创建对应的数据库和表。

这里我想说的是Hibernate中的一个属性 <prop key="hibernate.hbm2ddl.auto">update</prop>

这个hibernate.hbm2ddl.auto有一下几个值:

validate               加载hibernate时,验证创建数据库表结构
create                  每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop        加载hibernate时创建,退出是删除表结构
update                 加载hibernate自动更新数据库结构
在此如果我们在移植数据库时(当然只是开发过程,不关心数据的情况下),我们可以配置这个属性帮我们创建表。

等完成后一定要把此属性删掉。为什么呢?因为数据库时很重要的,所以很多大公司才高新聘请DBA,这种用程序操作

数据库本身就很危险。在你对hibernate源码都没怎么看的情况下,这种方式就不自量力了。


下一篇我们要将代码中一个比较重要的内容,那就是spring事务的使用。这里已经用到了,只是内容太多,放到下篇再细说吧。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值