SSI框架为struts、spring、ibatis,在该框架中,使用ibatis作为数据持久层,其中ibatis使用最新版本mybatis3。
注:本文使用版本:struts-2.3.4、spring-3.2.1、mybatis-3.2.1
aopalliance-1.0.jar、
freemarker-2.3.19.jar,
ognl-3.0.5.jar,
struts2-core-2.3.4.jar,
struts2-spring-plugin-2.3.4.jar,
xwork-core-2.3.4.jar,
commons-fileupload-1.2.2.jar、
commons-logging-api-1.1.jar、
commons-io-2.0.1.jar、
commons-lang-2.4.jar、
commons-lang3-3.1.jar、
commons-logging-api-1.1.jar。
3、添加spring包:
spring-aop-3.2.1.RELEASE.jar、
spring-aspects-3.2.1.RELEASE.jar、
spring-beans-3.2.1.RELEASE.jar、
spring-context-3.2.1.RELEASE.jar、
spring-context-support-3.2.1.RELEASE.jar、
spring-core-3.2.1.RELEASE.jar、
spring-expression-3.2.1.RELEASE.jar、
spring-jdbc-3.2.1.RELEASE.jar、
spring-orm-3.2.1.RELEASE.jar、
spring-test-3.2.1.RELEASE.jar、
spring-tx-3.2.1.RELEASE.jar、
spring-web-3.2.1.RELEASE.jar。
4、添加ibatis包:
mybatis-3.2.1.jar
mybatis-spring-1.2.0.jar
5、添加其他包:
ojdbc14.jar 【下载地址 http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html 】
aspectjweaver.jar 【下载地址 http://search.maven.org/remotecontent?filepath=org/aspectj/aspectjweaver/1.6.12/aspectjweaver-1.6.12.jar 】
javassist-3.15.0-GA.jar
c3p0-0.9.1.jar 【下载地址:https://code.google.com/p/aicms/downloads/detail?name=c3p0-0.9.1.jar&can=2&q=】
三、配置struts
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
org.apache.struts2.dispatcher.ng.listener.StrutsListener
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
2、建立BaseAction。
package com.hsinghsu.testSSI.action;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 4565394360208096613L;
}
3、编写测试struts的Action。
package com.hsinghsu.testSSI.action;
public class LoginAction extends BaseAction {
/**
*
*/
private static final long serialVersionUID = -6144396976094110475L;
public String execute() throws Exception
{
return "success";
}
}
4、配置action。
/jsp/welcome.jsp
5、建立测试jsp页面
<%@ page language="java" pageEncoding="UTF-8"%>
IWelcome
Welcome HsingHsu
6、配置spring
7、配置Spring监听器
contextConfigLocation
classpath:applicationContext.xml
characterEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
characterEncoding
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
org.springframework.web.context.ContextLoaderListener
org.apache.struts2.dispatcher.ng.listener.StrutsListener
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
8、配置tomcat并启动,在浏览器中输入http://localhost:8686/testSSI/login.action
则页面会显示Welcome HsingHsu。
四、配置ibatis
1、建立数据库与数据表ibatis.connection.driver_class oracle.jdbc.driver.OracleDriver
ibatis.connection.url jdbc:oracle:thin:@10.127.58.1:1521:ora11g
ibatis.connection.username adm
ibatis.connection.password adm
c3p0.acquireIncrement=3
c3p0.idleConnectionTextPeriod=900
c3p0.minPoolSize=2
c3p0.maxPoolSize=50
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=600
c3p0.initialPoolSize=3
3、配置spring,添加ibatis支持。
classpath:jdbc.oracle.properties
4、配置ibatis映射文件
5、创建持久层实体类。
package com.hsinghsu.testSSI.model;
public class User {
private Long id;
private String name;
private String pwd;
private Long age;
private Long sex;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
public Long getSex() {
return sex;
}
public void setSex(Long sex) {
this.sex = sex;
}
}
6、配置User映射文件
insert into
T_TEST_USER(ID,NAME,PWD,AGE,SEX)
values(#{id},#{name},#{pwd},#{age},#{sex})
五、编写java代码
package com.hsinghsu.testSSI.dao;
import com.hsinghsu.testSSI.model.User;
public interface UserDao {
public User getUserByName(String name);
}
2、编写DAO实现
package com.hsinghsu.testSSI.dao.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import com.hsinghsu.testSSI.dao.UserDao;
import com.hsinghsu.testSSI.model.User;
@Repository(value = "userDao")
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override
public User getUserByName(String name) {
User user = new User();
// select语句,传入一个参数,返回User对象 -------------------------------------------------------------------------
user = getSqlSession().selectOne("UserMapper.getUserByName", name);// select语句,传入一个参数,返回User对象
//insert语句,传入一个对象 -------------------------------------------------------------------------------------
User oneUser = new User();
oneUser.setId(Long.parseLong("6"));
oneUser.setName("go07");
oneUser.setPwd("pesd07");
oneUser.setAge(Long.parseLong("25"));
oneUser.setSex(Long.parseLong("1"));
//int result = getSqlSession().insert("UserMapper.insertUser",oneUser);//insert语句,传入一个对象
//System.out.println("insert result:"+result);
return user;
}
}
3、修改action类
package com.hsinghsu.testSSI.action;
import org.springframework.beans.factory.annotation.Autowired;
import com.hsinghsu.testSSI.dao.UserDao;
import com.hsinghsu.testSSI.model.User;
public class LoginAction extends BaseAction {
/**
*
*/
private static final long serialVersionUID = -439437585357651788L;
@Autowired
private UserDao userDao;
private User user;
private String username;
private String password;
public String execute() throws Exception {
user = userDao.getUserByName(username);
if (user != null) {
if (user.getPwd().equals(password)) {
return SUCCESS;
}
}
return ERROR;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4、新建用户登录页面。
<%@ page language="java" pageEncoding="UTF-8"%>
Insert title here
5、修改struts.xml配置文件
/jsp/welcome.jsp
/jsp/welcome.jsp
/jsp/login.jsp
6、运行tomcat,在浏览器中输入http://localhost:8686/testSSI/jsp/login.jsp 会显示登录页面,如果成功会显示welcome 。失败则会继续要求登录。