好了,我们接着上篇继续我们的服务层及Struts的实现:
服务层这里我就不用做介绍了,这里的实现和以前Hibernate的例子基本类同,唯一不同的是使用了Spring2.5的标注方式实现类的注入。
IUserService.java
服务层实现:UserService.java
Struts层的实现也是比较简单了,与上述的区别都一样,在这里我就不太多说了。
Login.java
到现在为止,我们的底层的类已经全部实现了,下来我们看其配置文件:
struts.xml
Struts.properties
接下来也是与之前例子不同的地方,就是ibatis的配置文件:
config.xml
服务层这里我就不用做介绍了,这里的实现和以前Hibernate的例子基本类同,唯一不同的是使用了Spring2.5的标注方式实现类的注入。
IUserService.java
package
org.login.service;
import java.util.List;
import org.login.vo.Users;
public interface IUserService {
List < Users > getAllUser();
Users getUserById(Integer id);
boolean isLogin(Users user);
boolean insertUser(Users user);
boolean updateUser(Users user);
boolean deleteUser(Integer id);
}
import java.util.List;
import org.login.vo.Users;
public interface IUserService {
List < Users > getAllUser();
Users getUserById(Integer id);
boolean isLogin(Users user);
boolean insertUser(Users user);
boolean updateUser(Users user);
boolean deleteUser(Integer id);
}
package
org.login.service.Impl;
import java.util.List;
import org.login.dao.IUserDAO;
import org.login.service.IUserService;
import org.login.vo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service( " userService " )
public class UserService implements IUserService {
@Autowired(required = false )
@Qualifier( " userDAO " )
private IUserDAO userDAO = null ;
public IUserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) {
this .userDAO = userDAO;
}
@Override
public boolean deleteUser(Integer id) {
return userDAO.deleteUser(id);
}
@Override
public List < Users > getAllUser() {
return userDAO.getAllUser();
}
@Override
public boolean insertUser(Users user) {
return userDAO.insertUser(user);
}
@Override
public boolean isLogin(Users user) {
return userDAO.isLogin(user);
}
@Override
public boolean updateUser(Users user) {
return userDAO.updateUser(user);
}
@Override
public Users getUserById(Integer id) {
return userDAO.getUserById(id);
}
}
import java.util.List;
import org.login.dao.IUserDAO;
import org.login.service.IUserService;
import org.login.vo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service( " userService " )
public class UserService implements IUserService {
@Autowired(required = false )
@Qualifier( " userDAO " )
private IUserDAO userDAO = null ;
public IUserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) {
this .userDAO = userDAO;
}
@Override
public boolean deleteUser(Integer id) {
return userDAO.deleteUser(id);
}
@Override
public List < Users > getAllUser() {
return userDAO.getAllUser();
}
@Override
public boolean insertUser(Users user) {
return userDAO.insertUser(user);
}
@Override
public boolean isLogin(Users user) {
return userDAO.isLogin(user);
}
@Override
public boolean updateUser(Users user) {
return userDAO.updateUser(user);
}
@Override
public Users getUserById(Integer id) {
return userDAO.getUserById(id);
}
}
Login.java
package
org.login.actions;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.login.service.IUserService;
import org.login.vo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings({ " serial " , " unchecked " })
@Service( " login " )
public class Login extends ActionSupport implements ServletRequestAware {
private static final String UPDATE = " update " ;
private HttpServletRequest request = null ;
@Autowired(required = false )
@Qualifier( " userService " )
private IUserService userService = null ;
private String id = null ;
private String name = null ;
private String password = null ;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this .id = id;
}
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this .userService = userService;
}
@Override
public String execute() throws Exception {
Users user = new Users();
user.setUserName(name);
user.setPassword(password);
if (getUserService().isLogin(user)) {
return SUCCESS;
}
return INPUT;
}
public String save() throws Exception {
Users user = new Users();
user.setUserName(name);
user.setPassword(password);
if (getUserService().insertUser(user)) {
return SUCCESS;
}
return ERROR;
}
public String update() throws Exception {
Users user = null ;
String action = request.getParameter( " action " );
String id = request.getParameter( " id " );
if ( " loading " .equals(action)) {
user = new Users();
user = getUserService().getUserById(Integer.valueOf(id));
if ( null != user) {
request.setAttribute( " user " , user);
}
return UPDATE;
} else {
user = new Users();
user.setUserName(name);
user.setPassword(password);
user.setId(Integer.valueOf(id));
if (getUserService().updateUser(user)) {
return SUCCESS;
}
}
return ERROR;
}
public String delete() throws Exception {
String id = request.getParameter( " id " );
if (getUserService().deleteUser(Integer.valueOf(id))) {
return SUCCESS;
}
return ERROR;
}
public String findAllUsers() throws Exception {
List < Users > list = getUserService().getAllUser();
if (list.size() > 0 ) {
request.setAttribute( " list " , list);
return SUCCESS;
}
return ERROR;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this .request = request;
}
}
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.login.service.IUserService;
import org.login.vo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings({ " serial " , " unchecked " })
@Service( " login " )
public class Login extends ActionSupport implements ServletRequestAware {
private static final String UPDATE = " update " ;
private HttpServletRequest request = null ;
@Autowired(required = false )
@Qualifier( " userService " )
private IUserService userService = null ;
private String id = null ;
private String name = null ;
private String password = null ;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this .id = id;
}
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this .userService = userService;
}
@Override
public String execute() throws Exception {
Users user = new Users();
user.setUserName(name);
user.setPassword(password);
if (getUserService().isLogin(user)) {
return SUCCESS;
}
return INPUT;
}
public String save() throws Exception {
Users user = new Users();
user.setUserName(name);
user.setPassword(password);
if (getUserService().insertUser(user)) {
return SUCCESS;
}
return ERROR;
}
public String update() throws Exception {
Users user = null ;
String action = request.getParameter( " action " );
String id = request.getParameter( " id " );
if ( " loading " .equals(action)) {
user = new Users();
user = getUserService().getUserById(Integer.valueOf(id));
if ( null != user) {
request.setAttribute( " user " , user);
}
return UPDATE;
} else {
user = new Users();
user.setUserName(name);
user.setPassword(password);
user.setId(Integer.valueOf(id));
if (getUserService().updateUser(user)) {
return SUCCESS;
}
}
return ERROR;
}
public String delete() throws Exception {
String id = request.getParameter( " id " );
if (getUserService().deleteUser(Integer.valueOf(id))) {
return SUCCESS;
}
return ERROR;
}
public String findAllUsers() throws Exception {
List < Users > list = getUserService().getAllUser();
if (list.size() > 0 ) {
request.setAttribute( " list " , list);
return SUCCESS;
}
return ERROR;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this .request = request;
}
}
struts.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 >
< include file ="struts-default.xml" />
< package name ="struts2" extends ="struts-default" >
< action name ="logon" class ="login" >
< result name ="success" > /success.jsp </ result >
< result name ="input" > /login.jsp </ result >
</ action >
< action name ="save" class ="login" method ="save" >
< result name ="success" > /success.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
< action name ="update" class ="login" method ="update" >
< result name ="success" > /success.jsp </ result >
< result name ="update" > /update.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
< action name ="delete" class ="login" method ="delete" >
< result name ="success" > /success.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
< action name ="show" class ="login" method ="findAllUsers" >
< result name ="success" > /list.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
</ package >
</ struts >
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< include file ="struts-default.xml" />
< package name ="struts2" extends ="struts-default" >
< action name ="logon" class ="login" >
< result name ="success" > /success.jsp </ result >
< result name ="input" > /login.jsp </ result >
</ action >
< action name ="save" class ="login" method ="save" >
< result name ="success" > /success.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
< action name ="update" class ="login" method ="update" >
< result name ="success" > /success.jsp </ result >
< result name ="update" > /update.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
< action name ="delete" class ="login" method ="delete" >
< result name ="success" > /success.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
< action name ="show" class ="login" method ="findAllUsers" >
< result name ="success" > /list.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
</ package >
</ struts >
struts.locale=zh_CN
struts.i18n.encoding=UTF-8
struts.objectFactory=spring
struts.i18n.encoding=UTF-8
struts.objectFactory=spring
config.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >
< sqlMapConfig >
< sqlMap resource ="org/login/vo/Users.xml" />
</ sqlMapConfig >
<! DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >
< sqlMapConfig >
< sqlMap resource ="org/login/vo/Users.xml" />
</ sqlMapConfig >
这个文件比较简单,只配置了bean的映射文件。
最后一个配置文件就是Spring的配置文件了,这个也是最关键的,与以前的配置是截然不同,因为在Spring2.5使用了标注方式注入,同时她了具有了自动的搜索bean的能力,所以配置就相当的简单,如果有不明白的地方,可以写到评论里,或者给我留言,这也是Spring2.5最难理解的地方了。
applicationContext.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:context ="http://www.springframework.org/schema/context"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
< context:annotation-config />
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" >
< property name ="driverClassName" value ="net.sourceforge.jtds.jdbc.Driver" />
< property name ="url" value ="jdbc:jtds:sqlserver://localhost:1433/pubs" />
< property name ="username" value ="sa" />
< property name ="password" value ="aa" />
</ bean >
< bean id ="sqlClient" class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="configLocation" >
< value > classpath:config.xml </ value >
</ property >
</ bean >
< context:component-scan base-package ="org.login" />
</ beans >
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
< context:annotation-config />
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" >
< property name ="driverClassName" value ="net.sourceforge.jtds.jdbc.Driver" />
< property name ="url" value ="jdbc:jtds:sqlserver://localhost:1433/pubs" />
< property name ="username" value ="sa" />
< property name ="password" value ="aa" />
</ bean >
< bean id ="sqlClient" class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="configLocation" >
< value > classpath:config.xml </ value >
</ property >
</ bean >
< context:component-scan base-package ="org.login" />
</ beans >
这篇就写到这里吧,下篇我们将说明页面层的代码,至于说明我想也不用了,如果有兴趣研究的朋友可以先下载示例代码先看下,具体的下载地址如下:
演示示例下载地址:Struts2.0+Spring2.5.1+ibatis2.3地址:http://download.csdn.net/source/331581
如果有朋友比较喜欢Hibernate的话,过些时候我会再写个关于Hibernate的集成,不过大家可以先看一下我以前的两个关于Hibernate方法的集成示例,下载地址如下:
Struts1.2+Spring1.2+HIbernate3.1整合用户登录下载地址:
http://download.csdn.net/source/323772
Struts2.0+Spring2.0+HIbernate3.1整合用户登录以及增删改查完整示例:http://download.csdn.net/source/326113