美文网首页
09三大框架整合

09三大框架整合

作者: RobertLiu123 | 来源:发表于2019-08-06 13:10 被阅读0次

整合思路
整合分两步:
第一步:在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上下文

<!-- spring上下文配置,全局上下文。module -->
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener><!-- 支持传统请求的 -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <!-- 支持restful -->
 <servlet>
<servlet-name>dispatcher-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- *.action 由DispatcherServlet来解析
/  rest风格的请求,会匹配所有/开头的 不会匹配*.jsp的。 /js/*.js  /css/*.css这种静态文件需要额外配置
/* 在rest不要配置了。
 -->
<servlet-mapping>
<servlet-name>dispatcher-rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  
    <!-- 配置编码解析器 -->
  <filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
  </init-param>
  </filter>
 <filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

4)springmvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
 <!-- 配置注解驱动(处理器映射器和处理器适配器以及一些常用的转换器) -->
 <mvc:annotation-driven/>
 <!-- 注解扫描的包,springmvc主要用来管理controller层,所以配置扫描controller所在的包即可 --> 
 <context:component-scan base-package="com.neuedu.controller"/>
 <!-- 配置jsp视图解析器参考手册22.5.2 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
 <!-- 配置文件上传解析器 参考手册22.10.2-->
 <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="1024000"/>
</bean>
 <!-- 配置静态资源访问权限 参考手册22.14.2 -->
 <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
 <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
 <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
</beans>

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

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

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/SqlMapConfig.xml"></property>
</bean>

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

<!-- 配置引入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

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

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


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.neuedu.mapper" />
</bean>

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
   <setting name="logImpl" value="LOG4J"/>
   <!-- 是否懒加载(延迟加载)true-懒加载false-即该加载 -->
   <setting name="lazyLoadingEnabled" value="true"/>
   <!-- 积极加载改为消极加载 -->
   <setting name="aggressiveLazyLoading" value="false"/> 
</settings>
<!-- 类型别名 -->
<typeAliases>
<!-- 终级配置 整包扫描 推荐-->
<package name="com.neuedu.pojo"/>
</typeAliases>
</configuration>

3)建包:com.neuedu.mapper
4)建包:com.neuedu.controller
5)建文件夹:在WebContent/WEB-INF/jsp/
6)建源文件目录: WebContent/js WebContent/css WebContent/images

建包: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"

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

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<User> ulist=userServiceImpl.findAll();
      System.out.println(ulist.size());
      return "list";
  }
}

2)IUserService

public interface IUserService {
   public List<User> findAll();
}

3)UserServcieImpl

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

}

4)IUserMapper

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

5)list.jsp(略)

相关文章

网友评论

      本文标题:09三大框架整合

      本文链接:https://www.haomeiwen.com/subject/joubdctx.html