五、整合struts2和mybatis和spring

1、导入jar包,及mysql驱动包5.1.20(后来删除了commons-logging.jar)

五、整合struts2和mybatis和spring_第1张图片

 2、web.xml配置文件(重要)

  对应用上下文配置文件改名为默认名:applicationContext


 
    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">
    
    index.jsp
  
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
      
          class>org.springframework.web.context.ContextLoaderListenerclass>
      
      
      
          struts2
          class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.classclass>
      
      
          struts2
          *.action
      
五、整合struts2和mybatis和spring_第2张图片

3、mybatis配置文件mybatis.cfg.xml


DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
        <package name="cn.sxt.vo"/>
    
    
    
        <package name="cn.sxt.dao.UserMapper"/>
    

4、编码  整个目录结构

五、整合struts2和mybatis和spring_第3张图片

UserDao接口

 UserDao实现

 UserService接口

public interface UserService {
    public List getAll();
}

UserService实现

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public List getAll() {
        return userDao.getAll();
    }
}

UserAction

public class UserAction {
    private List list;
    private UserService userService;
    public String list(){
        list = userService.getAll();
        return "success";
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

Spring配置文件

applicationContext.xml



    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">
    
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
        
        
        
      
    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    
    
    
        
        
            
            
            
            
            
            
            
        
    
    
        
        
    
    
    
    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
     

   <import resource="config/spring/user.xml"/>

User.xml



    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">
        
   class="cn.sxt.dao.impl.UserDaoImpl">
           
   
   class="cn.sxt.service.impl.UserService">
           
   
   class="cn.sxt.action.UserAction">
           
   

struts.xml


DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">



 

转载于:https://www.cnblogs.com/djlindex/p/11396338.html

你可能感兴趣的:(五、整合struts2和mybatis和spring)