使用架构细则
ibatis-2.3.4.726+spring-framework-3.1.1+struts-2.3.1.2+memcached
框架详解
所包含的jar
antlr.jar, aopalliance.jar, aspectjweaver.jar, bonecp-0.7.1-rc2.jar, cglib-nodep-2.1_3.jar, commons-collections-3.1.jar, commons-fileupload-1.2.2.jar, commons-io-2.0.1.jar, commons-lang-2.5.jar, commons-logging.jar, commons-pool-1.3.jar, dom4j-1.6.1.jar, freemarker-2.3.18.jar, freemarker-2.3.18.jar, guava-11.0.2.jar, ibatis-2.3.4.726.jar, javassist-3.11.0.GA.jar, json-lib-2.1-jdk15.jar, jstl-1.2.jar, jta.jar, jxl.jar, log4j-1.2.16.jar, mysql-connector-java-5.1.7-linux-bin.jar, ognl-3.0.4.jar, org.springframework.aop-3.1.1.RELEASE.jar, org.springframework.asm-3.1.1.RELEASE.jar, org.springframework.beans-3.1.1.RELEASE.jar, org.springframework.context-3.1.1.RELEASE.jar, org.springframework.core-3.1.1.RELEASE.jar, org.springframework.expression-3.1.1.RELEASE.jar, org.springframework.jdbc-3.1.1.RELEASE.jar, org.springframework.orm-3.1.1.RELEASE.jar, org.springframework.transaction-3.1.1.RELEASE.jar, org.springframework.web-3.1.1.RELEASE.jar, slf4j-api-1.6.2.jar, spy-2.4.jar, spymemcached-2.8.1.jar, struts2-core-2.3.1.2.jar, struts2-json-plugin-2.3.1.2.jar, struts2-spring-plugin-2.3.1.2.jar, xwork-core-2.3.1.2.jar
Struts
一:构建框架
1:在web.xml中配置struts的
2:典型的Struts配置文件(struts.xml)
"-//Apache Software Foundation//DTD struts Configuration
2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd" >
--引入其他status文档 --配置userAction,class对应Spring配置的bean的ID,method对应要调用的方法,result声明方法返回值到跳转的页面或者要返回的数据类型
3:与配置信息对应的Action类(UserAction):
package action;
import org.apache.struts2.ServletActionContext;
import iservice.IUserService;
import com.opensymphony.xwork2.ActionSupport;
import com.rdg.model.UserInfo;
public class UserAction extends ActionSupport {
--定义用户Id
private String userId;
--定义用户密码
private String userPass;
--定义新闻排序方式
private String newsOrder;
--声明UserService,具体实现见后续章节中的Spring部分
private IUserService UserService;
public void setUserService(IUserService userService) {
UserService = userService;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public void setNewsOrder(String newsOrder) {
this.newsOrder = newsOrder;
}
public String getNewsOrder() {
return newsOrder;
}
--验证用户登录,方法名为执行成功后的跳转页面
public String index(){
try {
UserInfo user=new UserInfo();
user.setId(userId);
user.setPass(userPass);
UserInfo newUser=UserService.userLogin(user);
ServletActionContext.getRequest().getSession().setAttribute("user", newUser);
if(newUser!=null){
return SUCCESS;
}else{
return ERROR;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return ERROR;
}
}
public String UNOrder(){
UserInfo user=new UserInfo();
user.setId(((UserInfo)ServletActionContext.getRequest().getSession().getAttribute("user")).getId());
user.setNewsOrder(newsOrder);
return UserService.updateUser(user);
}
}
Spring
一:构建框架
1:在web.xml中配置spring的监听和字符集过滤,默认过滤为utf8
org.springframework.web.context.ContextLoaderListener
--字符集过滤
2:典型的spring配置文件配置pool数据源和依赖注入
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-lazy-init="false"> --数据源名称和类 --数据源驱动 --连接URL --数据库用户名 --数据库密码 --设置connection的空闲存活时间 --设置测试connection的间隔时间 --每个分区的最大连接数 --每个分区的最小连接数 --设置分区个数 --设置分区中的connection增长数量 --设置statement缓存个数 --设置connection助手线程个数 --将数据源注入dao的属性中 --数据库交互类集成dao类,则可直接使用数据源 --将dao类注入到service的属性中 --将service注入到Action中
3: spring配置文件中用到的类
--userDao类(扩展自UserInfoDAOImpl,UserInfoDAOImpl中定义基本的方法,扩展中定义扩展方法,在扩展中注入dao进行操作)
package com.rdg.dao.impl;
import com.rdg.dao.base.UserInfoDAOImpl;
public class UserInfoDAO extends UserInfoDAOImpl {
--调用父类的构造方法
public UserInfoDAO() {
// TODO Auto-generated constructor stub
super();
}
}
--userService类
package service;
import iservice.IUserService;
import java.util.List;
import net.spy.memcached.MemcachedClient;
import com.rdg.dao.base.UserInfoDAO;
import com.rdg.model.UserInfo;
import com.rdg.model.UserInfoExample;
public class UserInfoManager implements IUserService {
private UserInfoDAO userDao;
private MemcachedClient memcachedClient;
public void setMemcachedClient(MemcachedClient memcachedClient) {
this.memcachedClient = memcachedClient;
}
public void setUserDao(UserInfoDAO userDao) {
this.userDao = userDao;
}
--验证用户登录
public UserInfo userLogin(UserInfo user) {
UserInfo users=userDao.selectByPrimaryKey(user.getId());
if(user!=null){
return users;
}else{
return null;
}
}
--更新用户信息
public String updateUser(UserInfo user) {
try{
UserInfoExample ue=new UserInfoExample();
ue.createCriteria().andIdEqualTo(user.getId());
int i=userDao.updateByExampleSelective(user, ue);
userDao.updateByPrimaryKeySelective(user);
return "list";
}catch(Exception e){
e.printStackTrace();
return "error";
}
}
}
-- userAction类
package action;
import org.apache.struts2.ServletActionContext;
import iservice.IUserService;
import com.opensymphony.xwork2.ActionSupport;
import com.rdg.model.UserInfo;
public class UserAction extends ActionSupport {
private String userId;
private String userPass;
private String newsOrder;
private IUserService UserService;
public void setUserService(IUserService userService) {
UserService = userService;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public void setNewsOrder(String newsOrder) {
this.newsOrder = newsOrder;
}
public String getNewsOrder() {
return newsOrder;
}
--验证用户登录,方法名为登录后的跳转页面
public String index(){
try {
UserInfo user=new UserInfo();
user.setId(userId);
user.setPass(userPass);
UserInfo newUser=UserService.userLogin(user);
ServletActionContext.getRequest().getSession().setAttribute("user", newUser);
if(newUser!=null){
return SUCCESS;
}else{
return ERROR;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return ERROR;
}
}
--修改用户信息
public String UNOrder(){
UserInfo user=new UserInfo();
--从用户登录时存入session中的用户信息中取出用户Id
user.setId(((UserInfo)ServletActionContext.getRequest().getSession().getAttribute("user")).getId());
--修改用户实体信息
user.setNewsOrder(newsOrder);
--调用修改方法
return UserService.updateUser(user);
}
}
Ibatis
一:构建框架
1:Ibatis基础配置信息(pool方式)
(1)配置数据源bean(这里仍以与spring IOC集成方式为例)
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-lazy-init="false"> --数据源名称和类,此处为使用连接池连接,class使用的是连接池数据源 --数据源驱动,此处使用的是mysql数据库,加载mysql的数据库驱动包 --连接URL,这里使用的是mysql的连接字符串,主机地址为localhost,数据库名为test --数据库用户名 --数据库密码 --设置connection的空闲存活时间 --设置测试connection的间隔时间 --每个分区的最大连接数 --每个分区的最小连接数 --设置分区个数 --设置分区中的connection增长数量 --设置statement缓存个数 --设置connection助手线程个数 --将数据源注入dao的属性中 --数据库交互类集成dao类,则可直接使用数据源
以上就是Ibatis使用连接池连接MySql数据库的配置信息,将配置好的数据源注入dao类则可使用,Ibatis使用方法常用的分为两种,一种是使用Ibatis官方提供的程序自动生成类,只需在service中调用生成好的dao类的各个方法即可实现数据库的增删改查等常用操作,另一种是手动创建各个配置文件与编写映射等SQL语句,此处以手动创建为例做说明
(2):创建核心配置文件(sql-map-config.xml)
--配置实体类与数据库的映射XML路径
(3):创建和数据库对应的实体类
package com.rdg.model;
import com.rdg.model.base.UserInfoBase;
import java.io.Serializable;
public class UserInfo extends UserInfoBase implements Serializable {
private String id;
private String pass;
private String newsOrder;
private static final long serialVersionUID = 1L;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getNewsOrder() {
return newsOrder;
}
public void setNewsOrder(String newsOrder) {
this.newsOrder = newsOrder;
}
}
(4):创建实体类与数据库的映射XML(user_info_SqlMap.xml)
--命名空间
--配置实体类与数据库字段映射map --查询数据库的SQL语句,返回值为User实体 --更新用户newsorder的SQL语句,返回值为int类型 update userinfo set newsorder = ’123456’ ]]>
(5):在dao层中的应用,调用父类的方法和配置文件中配置的方法名来调用进行各种数据库的操作,这是基础操作的类,注入dao的类是继承自这个类的,具体说明见Spring部分
package com.rdg.dao.base;
import com.rdg.model.UserInfo;
import com.rdg.model.UserInfoExample;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class UserInfoDAOImpl extends SqlMapClientDaoSupport implements UserInfoDAO {
public UserInfoDAOImpl() {
super();
}
public int countByExample(UserInfoExample example) {
Integer count = (Integer) getSqlMapClientTemplate().queryForObject("user_info.countByExample", example);
return count;
}
public int deleteByExample(UserInfoExample example) {
int rows = getSqlMapClientTemplate().delete("user_info.deleteByExample", example);
return rows;
}
public int deleteByPrimaryKey(String id) {
UserInfo _key = new UserInfo();
_key.setId(id);
int rows = getSqlMapClientTemplate().delete("user_info.deleteByPrimaryKey", _key);
return rows;
}
public String insert(UserInfo record) {
Object newKey = getSqlMapClientTemplate().insert("user_info.insert", record);
return (String) newKey;
}
public String insertSelective(UserInfo record) {
Object newKey = getSqlMapClientTemplate().insert("user_info.insertSelective", record);
return (String) newKey;
}
@SuppressWarnings("unchecked")
public List List return list; } public UserInfo selectByPrimaryKey(String id) { UserInfo _key = new UserInfo(); _key.setId(id); UserInfo record = (UserInfo) getSqlMapClientTemplate().queryForObject("user_info.selectByPrimaryKey", _key); return record; } public int updateByExampleSelective(UserInfo record, UserInfoExample example) { UpdateByExampleParms parms = new UpdateByExampleParms(record, example); int rows = getSqlMapClientTemplate().update("user_info.updateByExampleSelective", parms); return rows; } public int updateByExample(UserInfo record, UserInfoExample example) { UpdateByExampleParms parms = new UpdateByExampleParms(record, example); int rows = getSqlMapClientTemplate().update("user_info.updateByExample", parms); return rows; } public int updateByPrimaryKeySelective(UserInfo record) { int rows = getSqlMapClientTemplate().update("user_info.updateByPrimaryKeySelective", record); return rows; } public int updateByPrimaryKey(UserInfo record) { int rows = getSqlMapClientTemplate().update("user_info.updateByPrimaryKey", record); return rows; } protected static class UpdateByExampleParms extends UserInfoExample { private Object record; public UpdateByExampleParms(Object record, UserInfoExample example) { super(example); this.record = record; } public Object getRecord() { return record; } } } memcached (1)准备工作 a)下载安装并启动memcached-win32-1.4.4-14.zip b)加入对memcached支持的spy-2.4.jar, spymemcached-2.8.1.jar包 c)在spring中声明并注入memcached操作类 d)在方法用调用memcached --将数据存入memcached中 memcachedClient.set("user", 72000, li); --从memcached中取出数据 memcachedClient.get("user"); 框架流程详解 程序在JSP或者HTML页面提交请求,请求提交到在struts配置文件中配置的Action名称所对应的类中,Action类接受请求信息后调用service类中完成指定功能的方法,有service调用dao层与数据库交互,dao层交互完毕以后将返回的数据传递给service类,service类中进行判断或者直接传递至action类中。将数据封装传递给请求端 以用户登录为例说明SSI数据流的流向: 1:JSP页面提交数据至Name属性为*user的Action中,user是Action的标识。*号代表要调用的这个Action中的方法名,Action中的class属性为Spring中配置bean的Id值,有Spring配置信息中可以看到Action对应的类为action包下的UserAction类,数据在传递到UserAction后由Action将数据传递至下一层的Service类,在Spring的配置文件中也注入了UserService类,ref=“UserService”对应的也是spring中bean的Id值,由此类推找到UserService在Spring中的配置,UserService对应的类为service.UserInfoManager,在Service接受到数据后执行特定的方法与dao层的ibatis交互,Ibatis使用连接池进行数据库连接,执行从xml文件中读取的sql语句转化为的对象,如果没有取出信息则说明该用户不存在,反之,登陆成功,值得注意的是Srtuts配置的Action必须是在spring中配置的Action,否则会造成spring无法注入,运行时导致空指针异常