struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo

近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!
主要实现用户的增删改查操作

1、导入相应的jar包
struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo_第1张图片

2、配置web.xml主要是配置struts2和spring

web.xml文件内容如下:

 
 
   
    index.jsp 
   
 
   
   
    org.springframework.web.context.ContextLoaderListener 
   
   
   
    contextConfigLocation 
    classpath:beans.xml 
   
 
 
   
   
    struts2 
     
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 
   
   
    struts2 
    /* 
   
 
 
 

3、配置spring配置文件,主要包括配置数据源、事务、mybaits等
beans.xml配置文件详细如下:

 
 
   
   
   
   
   
   
   
   
   
   
   
   
   
   
     
     
     
     
     
     
     
     
     
     
     
     
     
   
   
   
   
     
     
   
   
   
   
     
   
   
   
   
     
   
   
   
   
 
 

4.JDBC配置文件详细

 jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/operationLog 
jdbc.username=root 
jdbc.password= 
jdbc.maxActive = 2 
jdbc.maxIdle =5 
jdbc.minIdle=1 
jdbc.initialSize =3 
jdbc.maxWait =3000 

5、配置mybatis主配置文件:

 
 
 
   
     
   
   
     
   
 

6、配置user.xml文件

 
 
 
   
   
     
     
     
   
   
 
   
 
   
 
   
    
   
 
   
    update user set 
    username=#{username},password=#{password} where id=#{id} 
   
   
   
    delete from user where 
    id=#{id} 
   
 
 

7、User实体的写法

public class User implements Serializable { 
  private static final long serialVersionUID = -4415990281535582814L; 
  private Integer id; 
  private String username; 
  private String password; 
 
  public Integer getId() { 
    return id; 
  } 
 
  public void setId(Integer id) { 
    this.id = id; 
  } 
 
  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; 
  } 
 
  @Override 
  public String toString() { 
    return "User [id=" + id + ", password=" + password + ", username=" + username + "]"; 
  } 
 
  @Override 
  public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + ((id == null) ? 0 : id.hashCode()); 
    return result; 
  } 
 
  @Override 
  public boolean equals(Object obj) { 
    if (this == obj) 
      return true; 
    if (obj == null) 
      return false; 
    if (getClass() != obj.getClass()) 
      return false; 
    User other = (User) obj; 
    if (id == null) { 
      if (other.id != null) 
        return false; 
    } else if (!id.equals(other.id)) 
      return false; 
    return true; 
  } 
} 

8、UserDao的写法

public interface UserDao { 
 
  public abstract void insertUser(User user); 
 
  public abstract void updateUser(User user); 
 
  public abstract void deleteUser(Integer userId); 
 
  public abstract User findUserByid(Integer userId); 
 
  public abstract List findAll(); 
 
  public abstract User userLogin(User user); 
 
} 

9、UserDao的实现

@Repository 
public class UserDaoImpl implements UserDao { 
  private final String INSERT_USER = "insertUser"; 
  private final String UPDATE_USER = "updateUser"; 
  private final String DELETE_USER = "deleteUser"; 
  private final String FIND_USER_BYID = "findUserById"; 
  private final String SELECT_ALL_USER = "selectAllUser"; 
  private final String USER_LOGIN = "userLogin"; 
  @Autowired 
  private SqlSessionTemplate sqlSessionTemplate; 
 
  public void insertUser(User user) { 
    sqlSessionTemplate.insert(INSERT_USER, user); 
  } 
 
  public void updateUser(User user) { 
    sqlSessionTemplate.update(UPDATE_USER, user); 
  } 
 
  public void deleteUser(Integer userId) { 
    sqlSessionTemplate.delete(DELETE_USER, userId); 
  } 
 
  public User findUserByid(Integer userId) { 
    return sqlSessionTemplate.selectOne(FIND_USER_BYID, userId); 
  } 
 
  public List findAll() { 
    return sqlSessionTemplate.selectList(SELECT_ALL_USER); 
  } 
 
  public User userLogin(User user) { 
    return sqlSessionTemplate.selectOne(USER_LOGIN, user); 
  } 
} 

10、UserService接口

public interface UserService { 
 
  // 添加用户 
  public abstract void addUser(User user); 
 
  public abstract void updateUser(User user); 
 
  public abstract void deleteUser(Integer userId); 
 
  public abstract User findUserById(Integer userId); 
 
  public abstract List findAllUser(); 
 
  public abstract User login(User user); 
 
} 

11、UserService接口的实现

@Service 
@Transactional 
public class UserServiceImpl implements UserService { 
 
  @Autowired 
  private UserDao userDao; 
 
  // 添加用户 
  public void addUser(User user) { 
    userDao.insertUser(user); 
  } 
 
  // 更新用户 
  public void updateUser(User user) { 
    userDao.updateUser(user); 
  } 
 
  public void deleteUser(Integer userId) { 
    userDao.deleteUser(userId); 
  } 
 
  public User findUserById(Integer userId) { 
    return userDao.findUserByid(userId); 
  } 
 
  public List findAllUser() { 
    return userDao.findAll(); 
  } 
 
  public User login(User user) { 
    return userDao.userLogin(user); 
  } 
} 

12、配置Struts2

 
 
  
    
       
   
       
   
       
   
   
   
     
      user_queryAllUser.action 
      /index.jsp 
      /userList.jsp 
      /userAdd.jsp 
      /userUpdate.jsp 
     
   
   

13、UserAction具体实现

@Controller 
@Scope("prototype") 
public class UserAction extends ActionSupport { 
  @Autowired 
  private UserService userService; 
  private User user; 
  private List userList; 
 
  public String execute() throws Exception { 
    return null; 
  } 
 
  public String login() { 
    if (user != null) { 
      User user2 = userService.login(user); 
      if (user2 != null) { 
        return SUCCESS; 
      } 
    } 
    this.addFieldError("user.username", "用户名或密码错误!"); 
    return INPUT; 
  } 
 
  public String addUI() { 
    return "addUser"; 
  } 
 
  public String updateUI() { 
    user = userService.findUserById(user.getId()); 
    return "updateUser"; 
  } 
 
  public String add() { 
    userService.addUser(user); 
    return SUCCESS; 
  } 
 
  public String delete() { 
    userService.deleteUser(user.getId()); 
    return SUCCESS; 
  } 
 
  public String update() { 
    userService.updateUser(user); 
    return SUCCESS; 
  } 
 
  public User getUser() { 
    return user; 
  } 
 
  public void setUser(User user) { 
    this.user = user; 
  } 
 
  public String queryAllUser() { 
    userList = userService.findAllUser(); 
    return "userList"; 
  } 
 
  public List getUserList() { 
    return userList; 
  } 
 
  public void setUserList(List userList) { 
    this.userList = userList; 
  } 
 
} 

14、登录页面的实现

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
 
 
  
   
   
  用户登录 
   
   
     
   
   
   
   
  
  
  
  

用户登录

添加新用户

15、添加页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
 
 
  
   
  添加新用户 
   
   
   
     
 
  
  
  
  

添加新用户

16、修改页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
 
 
  
   
  修改用户 
   
   
   
     
 
  
  
  
  

修改用户

17、列表页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
 
 
  
   
  用户列表 
   
   
   
     
 
  
  
  
  

用户列表

添加新用户

用户id 用户名称 用户密码 操作
${id}修改 ${id}删除

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo)