spring+sturts2+ibatis环境搭建。仅献给新手们

来源:互联网 发布:mac 开机 重设密码 编辑:程序博客网 时间:2024/05/16 06:32

实习也有一段时间了,之前多spirng+struts2+ibatis完全不懂的我,现在可以试着开发了。尤其是ibatis以前在学校都没听过,也许这就是学习与公司的区别吧。好吧,这里就不多说了。下面开始讲解怎么搭建,我是新手可能好多地方考虑的不周全,但供新手应该还是可以的

sping 我用的是spirng3.2.先去官网下载好spring3.2所需的jar包,下载后应该是这样的struts2去官网下载好所需的jar包,下载后应该是这样的,下面就直接上图了


用到的jar包为


可能有些用不到,但我是新手,全部用上肯定不会错。好了,项目全部结束了,接下来将配置文件


1  先讲配置文件,第一个是applicationContext.xml主要是配置spring将数据源加载进来,将dao层加载进来

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">




<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
   <property name="url" value="jdbc:mysql://localhost:3306/xinbei"></property>
   <property name="username" value="xinbei"></property>
   <property name="password" value="xinbei"></property>
</bean>


<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
  <property name="configLocation" value="classpath:sqlMapConfig.xml">
  </property>


</bean>

<bean id="testDAO" class="com.baokang.ServiceImpl.TestDAO">
   <property name="dataSource">
   <ref bean="dataSource"/>
 </property>
  <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
  </property>
</bean>
<bean id="userDAO" class="com.baokang.ServiceImpl.UserServiceImpl">
   <property name="dataSource">
   <ref bean="dataSource"/>
 </property>
  <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
  </property>
</bean>


</beans>

2  sqlMapConfig.xml配置数据库的映射,在applicationContext.xml中最后出映射到该文件,然后通过sqlMap找到对应的sql语句

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"> 


<sqlMapConfig> 


<sqlMap resource="com/baokang/sql/ibatis.xml" /> 
<sqlMap resource="com/baokang/sql/user.xml" /> 


</sqlMapConfig>


3 配置struts.xml主要是前台与后台之间的映射,

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>    
        <package name="user" namespace="/" extends="struts-default">
              <action name="user.*" class="com.baokang.action.UserAction" method="{1}">  
                <result name="login">/index.jsp</result>
                <result name="error">/faild.jsp </result>
                
        </action>
        </package>
        
</struts>


4 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>ibatis</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>
    
    <!-- 配置spring配置文件加载的位置 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext.xml</param-value>  
    </context-param>
    
    <!-- 配置struts2 -->  
    <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>  
         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
       </filter>  
    
     <filter>
    <description>字符集过滤器</description>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <description>字符集编码</description>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  
  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>*.action</url-pattern>  
    </filter-mapping> 
    
</web-app>



上面主要是action层,也就是通常所说的控制层,这里我就只列出userAction类了,。另外一个是相同的

action

package com.baokang.action;


import javax.servlet.http.HttpServletRequest;


import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.baokang.dao.UserDao;
import com.baokang.domain.User;


public class UserAction {
private User user;
private UserDao userDao;
private String msg;


public String login() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
userDao = (UserDao) context.getBean("userDAO");
User u = null;
u = this.userDao.queryByUser(this.user);
HttpServletRequest request = ServletActionContext.getRequest();
if (null != u && user.getMm().equals(u.getMm())) {
return "login";
} else 
return "error";


}


public User getUser() {
return user;
}


public void setUser(User user) {
this.user = user;
}

}


下面是dao层,即与数据库打交道的


package com.baokang.dao;


import com.baokang.domain.User;


public interface UserDao {
public User queryByUser(User user);


}

这里我只写了一个查询的方法


下面是domain类。即实体类,

package com.baokang.domain;


public class User {
private String yhm;
private String mm;
public String getYhm() {
return yhm;
}
public void setYhm(String yhm) {
this.yhm = yhm;
}
public String getMm() {
return mm;
}
public void setMm(String mm) {
this.mm = mm;
}

}


接下来是实现层,主要是对数据的增删查改

package com.baokang.ServiceImpl;


import com.baokang.dao.UserDao;
import com.baokang.domain.User;


import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class UserServiceImpl extends SqlMapClientDaoSupport implements UserDao {

public User queryByUser(User user){
return (User)getSqlMapClientTemplate().queryForObject("getUsersByYhm",user);
}

}


下面是sql语句,由于搭建的是ibatis,所以我用的是ibatis

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap >
  <typeAlias type="com.baokang.domain.User" alias="login"/>
  <resultMap id="loginTest" class="login" >
    <result column="yhm" property="yhm" jdbcType="VARCHAR" />
    <result column="mm" property="mm" jdbcType="VARCHAR" />
  </resultMap>


   <!-- 根据用户名获得用户对象 -->
   <select id="getUsersByYhm" resultMap="loginTest">
    SELECT
      YHM as "yhm",  <!-- 用户名 -->
MM as "mm"  <!-- 密码-->
from manager WHERE 1=1
     <isNotEmpty prepend=" AND " property="yhm">
yhm = #yhm#
</isNotEmpty>
<isNotEmpty prepend=" AND " property="mm">
mm = #mm#
</isNotEmpty>
  </select> 
 
</sqlMap>

以上主要是后台的程序,每个类都单独放在一个包中,每个类已经写的很清楚了,开头的package都是不一样的。

接下来讲前台,在这里我只做一个简单的登录界面,名称为web.xml中的第一个jsp.longin.jsp

<body onkeydown="loginClick();">
<table class="pagebg" cellspacing="0">
<tr>
<td align="center" valign="middle">
<div class="loginbg">
<form action="user.login.action" method="post">
<table>
<tr>
<td><span class="ans">用户名:</span></td>
<td><input id="yw_dlm"  class="login-input" name="user.dlm" maxlength="20"/></td>
</tr>
<tr>
<td><span class="ans">密码:</span></td>
<td><input id="yw_mm" type="password" name="user.mm" class="login-input" maxlength="8"/></td>
</tr>
</table>
</form>
<button class="login-button" style="left: 300px; top: 80px" onclick="checkSubmit()">登录</button>
<button class="login-button" style="left: 400px; top: 80px" >重置</button>
</div>
</td>
</tr>
</table>
<!-- 确认对话框 -->
<div id="dialogConfirm" style="display: none"><p>确定删除这些设备信息吗?</p></div>
<!-- 提示信息 -->
<%@ include file="dialogInfo.jsp" %>
</body>


js为login.js

function checkSubmit()
{
if($("#yw_dlm").val()==""){
openDialogInfo("用户名不能为空!");
return false ;
}
if($("#yw_mm").val()==""){
openDialogInfo("密码不能为空!");
return false;
}else{
document.forms[0].submit();
}
}


最后数据库的字段为

数据库的名称,用户名,密码在applicationContext。xml中自己修改,,至此基本结束,谨献给新手的,在这里我也没有做过多的界面,相信你们一定看得懂哦,当初也是这么过来的,

0 0