SSM整合之applicationContext.xml(也叫Mybatis-config.xml)文件的详细配置加解释说明,web.xml文件的配置

简介: SSM整合之applicationContext.xml(也叫Mybatis-config.xml)文件的详细配置加解释说明,web.xml文件的配置

ssm整合,mybatis-config.xml文件的配置信息:

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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
  <!-- 启动自动扫描该包下所有的Bean(例如@Controller) -->
  <context:component-scan base-package="com.macw" />
    <!--
   把小配置文件 jdbc.properties引入到spring配置文件中
     location:引入的小配置文件的路径
     system-properties-mode:表示不从系统属性里面获取变量,否则会跟我们的小配置文件冲突
    -->
    <context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"></context:property-placeholder>
    <!-- 数据源(数据库连接池),jdbc连接数据库需要的数据库连接都从这个类中获取 -->
    <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="password" value="${password}"/>
        <property name="username" value="${username}"/>
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="url" value="${url}"/>
        <property name="maxWait" value="${maxWait}"/>
        <property name="maxActive" value="${maxActive}"/>
        <property name="initialSize" value="${initialSize}"/>
    </bean>
    <!-- 配置sqlSessionFactoy的bean对象,配置好后,这个bean就可以帮助创建sqlSession对象了 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="ds"/>
        <!--  注册映射文件  -->
        <property name="mapperLocations">
            <list>
                <!-- 把所有的dao包里面的mapper文件引入进来 -->
                <value>classpath:com/macw*.xml</value>
            </list>
        </property>
        <!-- 还可以设置其他配置 -->
        <property name="typeAliasesPackage" value="com.macw.entity"/>
        <property name="configurationProperties">
            <props>
                <prop key="logImpl">LOG4J</prop>
            </props>
        </property>
    </bean>
    <!-- 让spring帮我生成dao接口的实现类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 告知spring生成哪些接口的实现类 -->
        <property name="basePackage" value="com.macw.dao"/>
        <!-- dao操作数据库时需要sqlSessonfactory,spring会自动找一个id="sqlSessionFactrory"的bean标签 -->
    </bean>
    <!-- 相当于配置了一个 bean标签 id="bookDao" -->
    <!-- 扫描业务包里面的注解 -->
    <context:component-scan base-package="com.macw.service.impl"/>
    <!-- 事务管理器类 -->
    <bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"/>
    </bean>
    <!--
    配置事务增强类
        在这个里面我们使用了特殊的标签tx:advice,spring会自动判断出使用哪个增强类
        id:这个增强类的标记
        transaction-manager是使用哪个事务管理器。要跟上面的bean的id一致
     -->
    <tx:advice id="ct" transaction-manager="tm">
        <!-- 事务的使用策略,哪些方法需要使用事务,哪些不需要使用事务 -->
        <tx:attributes>
            <!-- 方法名是select开头的,以后我们定义业务类的方法名不能随意写了。 -->
            <tx:method name="select*" read-only="true"/>
            <!-- 其余的方法要开启事务 -->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--  配置切入点和织入  -->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.macw.service..*.*(..))"/>
        <aop:advisor advice-ref="ct" pointcut-ref="pc"/>
    </aop:config>
</beans>

小配置文件jdbc.properties

username=hr
password=hr
url=jdbc:oracle:thin:@localhost:1521:xe
driverClassName=oracle.jdbc.OracleDriver
initialSize=10
maxActive=15
maxWait=10000

web.xml文件配置信息:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <!-- 1.配置监听,spring-mybatis.xml文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mybatis-config.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 2.配置spring-mvc.xml,加载spring容器文件 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <display-name>Archetype Created Web Application</display-name>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 3.过滤器,字符编码,p125 -->
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 4.静态资源访问 -->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.PNG</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  <!-- 5.jsp配置 -->
  <error-page>
    <error-code>404</error-code>
    <location>/404.html</location>
  </error-page>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
目录
相关文章
|
3月前
|
XML 前端开发 Java
讲解SSM的xml文件
本文详细介绍了SSM框架中的xml配置文件,包括springMVC.xml和applicationContext.xml,涉及组件扫描、数据源配置、事务管理、MyBatis集成以及Spring MVC的视图解析器配置。
99 1
|
13天前
|
缓存 NoSQL Java
Mybatis学习:Mybatis缓存配置
MyBatis缓存配置包括一级缓存(事务级)、二级缓存(应用级)和三级缓存(如Redis,跨JVM)。一级缓存自动启用,二级缓存需在`mybatis-config.xml`中开启并配置映射文件或注解。集成Redis缓存时,需添加依赖、配置Redis参数并在映射文件中指定缓存类型。适用于查询为主的场景,减少增删改操作,适合单表操作且表间关联较少的业务。
|
2月前
|
SQL 缓存 Java
MyBatis如何关闭一级缓存(分注解和xml两种方式)
MyBatis如何关闭一级缓存(分注解和xml两种方式)
99 5
|
4月前
|
SQL XML Java
mybatis复习01,简单配置让mybatis跑起来
文章介绍了MyBatis的基本概念、历史和特点,并详细指导了如何配置MyBatis环境,包括创建Maven项目、添加依赖、编写核心配置文件、创建数据表和实体类、编写Mapper接口和XML配置文件,以及如何编写工具类和测试用例。
mybatis复习01,简单配置让mybatis跑起来
|
3月前
|
JavaScript 前端开发 应用服务中间件
vue前端开发中,通过vue.config.js配置和nginx配置,实现多个入口文件的实现方法
vue前端开发中,通过vue.config.js配置和nginx配置,实现多个入口文件的实现方法
243 0
|
3月前
|
前端开发 Java Spring
SSM:拦截器&model&文件的上传和下载
本文档介绍了Spring MVC中拦截器的实现、Model对象的使用、文件上传下载的配置及实现,以及项目依赖管理文件pom.xml的配置。拦截器通过继承HandlerInterceptor接口实现请求的预处理、后处理和清理工作。Model对象用于数据传递,支持视图解析器的直接返回。文件上传下载涉及配置multipartResolver及编写控制器方法处理文件操作。pom.xml列出了项目所需的各种依赖库。
|
5月前
|
安全 Java 数据库连接
后端框架的学习----mybatis框架(3、配置解析)
这篇文章详细介绍了MyBatis框架的核心配置文件解析,包括环境配置、属性配置、类型别名设置、映射器注册以及SqlSessionFactory和SqlSession的生命周期和作用域管理。
后端框架的学习----mybatis框架(3、配置解析)
|
4月前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
82 1
|
5月前
|
缓存 Java 数据库连接
mybatis1.常见配置
本文介绍了MyBatis框架中的常见配置及其加载顺序。配置可通过`properties`元素、资源文件或方法参数传递,其中方法参数传递的属性具有最高优先级。文章列举了几个重要的配置项,如`cacheEnabled`用于全局开启或关闭缓存功能;`lazyLoadingEnabled`控制对象的延迟加载行为;`useGeneratedKeys`允许JDBC支持自动生成主键;`defaultExecutorType`设定默认执行器类型等。此外,还介绍了多环境配置方法,通过`environments`元素可定义不同环境下的数据库连接信息,并可根据需求动态选择加载特定环境
|
6月前
|
SQL Java 数据库连接
idea中配置mybatis 映射文件模版及 mybatis plus 自定义sql
idea中配置mybatis 映射文件模版及 mybatis plus 自定义sql
154 3