實習也有一段時間了,之前多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中自己修改,,至此基本結束,謹獻給新手的,在這裏我也沒有做過多的界面,相信你們一定看得懂哦,當初也是這麼過來的,