第6章 三大框架整合之一(3课时)

整合思路

整合分两步:
第一步:在web.xml里配置两个上下文,把spring,springmvc集成
第二步:在spring里配置集成mybatis,让spring管理mybatis连接和mapper

第1-2节 web.xml里配置两个上下文,把spring,springmvc集成到一起。参考spring手册

1)新建一个工程spring-mb
file-new project-dynamic web project-输入工程名字(spring-mb)-next-next-勾选generate web.xml-finish
2)拷贝工程依赖的jar包至WEB-INF/lib
3)web.xml里配置spring,springmvc上下文


  
contextConfigLocation
classpath:applicationContext.xml


org.springframework.web.context.ContextLoaderListener


    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:springmvc.xml
    
  
  
    springmvc
    *.action
  
  
  
 
dispatcher-rest
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:springmvc.xml

1



dispatcher-rest
/

  
    
  
  CharacterEncodingFilter
  org.springframework.web.filter.CharacterEncodingFilter
  
  encoding
  UTF-8
  
  
 
  CharacterEncodingFilter
  /*
  

4)springmvc.xml



 
 
  
 
 





 
 



 
 
 
 
 

spring与mybatis集成---applicationContext.xml(主要参考spring手册和mybatis-spring手册)

头部


注解驱动


扫描model所在的包,这里是service实现类所在的包


与mybatis集成

mybatis集成第一步:spring管理session

1)参考mybatis-spring手册第三章SqlSessionFactoryBean
中间发现dataSource没有配置,所以先配置datasource.参考spring手册7.12.5

 



2)在刚才配置上面增加配置数据源的代码





    
    
    
    

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/scott?characterEncoding=UTF-8&useUnicode=true
jdbc.username=root
jdbc.password=root

第二步 让spring管理mapper

  1. 让spring管理mapper,实现自动扫描 参考mybatis-spring手册



第三步 mybatis配置文件:SqlMapConfig.xml

建resource源文件目录并在下面建config包,将SqlMapConfig.xml拷贝过来,并去除数据库连接和扫描mapper部分的配置,余下部分代码





   
   
   
   
    







完整目录结构

第6章 三大框架整合之一(3课时)_第1张图片
image.png

至此三大框架集成完成,下面开始测试

开始建包

1)com.neuedu.model.service.impl

2)建包:com.neuedu.mapper

3)建包:com.neuedu.controller

4)建文件夹:在WebContent/WEB-INF/jsp/

5)建源文件目录: WebContent/js WebContent/css WebContent/images

  1. 建包:com.neuedu.pojo

1- 整合测试

第3节 异常处理

1)异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' available

分析:缺少 id="dataSource"的bean定义改造applicationCotext,加id="dataSource"







2)异常2

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [config/SqlMapConfig.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation.  Cause: java.lang.NoClassDefFoundError: org/apache/log4j/Priority

分析:log4j错误
解决:

 resource下引入log4j.properties

jar包里加入log4j.jar

示例程序编写

1)controller

@Controller
public class TestController {
  @Autowired
  IUserService userServiceImpl;
  @RequestMapping("/findAll")
  public String findAll(){
      List ulist=userServiceImpl.findAll();
      System.out.println(ulist.size());
      return "findall";
  }
}

2)IUserService

public interface IUserService {
   public List findAll();
}

3)UserServcieImpl

@Service("userServiceImpl")
public class UserServiceImpl implements IUserService {
    @Autowired
IUserMapper userMapper;
    @Override
    public List findAll() {
        // TODO Auto-generated method stub
        return userMapper.findAll();
    }

}

4)IUserMapper

public interface IUserMapper {
@Select("select * from t_user")
    List findAll();
  }

5)findall.jsp(略)

第6章 三大框架整合之一(3课时)_第2张图片
image.png

你可能感兴趣的:(第6章 三大框架整合之一(3课时))