spring 配置文件(主要整合的是spring 和 mybatis 的配置文件)

问题: 两者之间没有整合在一起的时候是怎么样的

spring配置文件:    Spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Java EE程序员必须学会并灵活应用这份"图纸"准确地表达自己的"生产意图"。Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="duke" class="com.springinaction.springidol.Juggler" >
<!-- 通过构造方法设置属性值 -->
<constructor-arg value="15"></constructor-arg>
</bean> <bean id="sonnect29" class="com.springinaction.springidol.Sonnet29"></bean> <bean id="poeticPoem" class="com.springinaction.springidol.PoeticJuggler">
<constructor-arg value="15"></constructor-arg>
<constructor-arg ref="sonnect29"></constructor-arg>
</bean> <bean id="saxphone" class="com.springinaction.springidol.saxphone"></bean>
<bean id="piano" class="com.springinaction.springidol.piano"></bean> <!-- p命名空间用法 -->
<bean id="Kenny2" class="com.springinaction.springidol.Instrumentalist"
p:song="Lemon Tree" p:age="30" p:instrument-ref="saxphone" >
</bean> <!-- 为集合配置bean -->
<bean id="hank" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<list>
<ref bean="piano" />
<ref bean="saxphone" />
</list>
</property>
<property name="instruments2">
<map>
<entry key="piano" value-ref="piano"></entry>
<entry key="saxphone" value-ref="saxphone"></entry>
</map>
</property>
</bean> <!-- properties的写法 -->
<bean id="hank2" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<props>
<!-- key和value都为String -->
<prop key="piano">la la la</prop>
<prop key="saxphone">ta ta ta</prop>
</props>
</property>
</bean> <!-- 赋null值 -->
<!--
...
<property name="xxx"><null/></property>
...
-->
</beans> 个人理解:在最基础的spring配置文件就是做控制反转的功能,就是将实体类的创建放在spring beanfactory中
     spring里面配置,就是围绕着各种bean,有bean的id, bean对应的class, 以及实现这个class里面
     的一些注入property, 就像上面红色的所示。 把一个个需要beanfactory工厂创建的bean 告诉beanfactory
     工厂。
     mybatis 配置文件:
<?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> <!-- 加载类路径下的属性文件 -->
<properties resource="db.properties"/> <!-- 设置一个默认的连接环境信息 -->
<environments default="mysql_developer">
<environment id="mysql_developer">
<!-- mybatis使用jdbc事务管理方式 -->
<transactionManager type="JDBC"></transactionManager>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="POOLED">
<!-- 配置与数据库交互的4个必要属性,不要直接写,单独写在一个配置文件中 -->
<property name="driver" value="${sqlserver.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
<!-- 连接环境信息,取一个任意唯一的名字 -->
</environments> <!-- 加载映射文件-->
<mappers>
<mapper resource="com/winner/entity/StudentMapper.xml"/>
</mappers>

</configuration> 对应的sql xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace可以写类的全限定名,这样做的好处是
  sqlSession.insert(Student.class.getName()+".addStudent");
-->
<mapper namespace="com.winner.entity.StudentMapper">   <!--实体与表的映射,type是类名,但是没有表名,可以理解表名在下面的sql语句中-->
<resultMap id="studentMap" type="com.winner.entity.Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap>
<insert id="addStudent" parameterType="com.winner.entity.Student">
<![CDATA[
INSERT INTO student(id,name,sal) VALUES (#{id},#{name},#{sal})
]]>
</insert>
</mapper>
个人理解: mybatis配置文件,一个是配置环境(连接数据库的配置, 以及mapper文件的配置), 一个是用来编写
sql语句的xml文件(用于实体类中与其相互匹配), 然后编写对应的dao类来执行操作。这个部分介绍  
    有待深入 spring 和 mybatis 整合配置:

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  <!--读取db.properties -->
  <!-- source 文件夹下面存放的就是classpath 文件, 系统可以找到的 -->
  <!-- 这个文件里面主要就是配置bean工厂,因为spring就是一个bean工厂,然后将数据库mybatis整合在里面 -->
  <!-- 一个是获取jdbc需要的一些东西, 然后就是配置mybatis里面需要的东西-->
  

  <context:property-placeholder location="classpath:db.properties"/>
  <!-- 配置数据源 -->
  <bean id="dataSource"
    class="org.apache.commons.dbcp2.BasicDataSource">
    <!--数据库驱动 -->
    <property name="driverClassName" value="${jdbc.driver}" />
    <!--连接数据库的url -->
    <property name="url" value="${jdbc.url}" />
    <!--连接数据库的用户名 -->
    <property name="username" value="${jdbc.username}" />
    <!--连接数据库的密码 -->
    <property name="password" value="${jdbc.password}" />
    <!--最大连接数 -->
    <property name="maxTotal" value="${jdbc.maxTotal}" />
    <!--最大空闲连接 -->
    <property name="maxIdle" value="${jdbc.maxIdle}" />
    <!--初始化连接数 -->
    <property name="initialSize" value="${jdbc.initialSize}" />
  </bean>

  <!-- 事务管理器 -->
  <bean id="transactionManager" class=
  "org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!-- 数据源 -->
  <property name="dataSource" ref="dataSource" />
  </bean>
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- 传播行为 -->
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="insert*" propagation="REQUIRED" />
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="create*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
    <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
    <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
    </tx:advice>
    <!-- 切面 -->
    <aop:config>
  <aop:advisor advice-ref="txAdvice"
  pointcut="execution(* com.qf.core.service.*.*(..))" />
  </aop:config>

  <!-- 配置 MyBatis的工厂 -->
  <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 数据源 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 配置MyBatis的核心配置文件所在位置 -->
    <property name="configLocation" value="classpath:mybatis-config.xml" />
  </bean>

  < !-- the below configuration told the things that the sql sentence and java function -->
  <!-- the function for below configuration is down for mybatis -->
  <!-- 配置mapper 扫描器 接口开发,扫描 com.qf.core.dao包 ,写在此包下的接口即可被扫描到 --

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

  <!-- 配置扫描@Service注解 -->
    <context:component-scan base-package="com.qf.core.service"/>
 </beans>

个人理解: 整合之后将原来在mybatis 配置文件搭建环境的部分,连接数据库,mapper 部分(整合之后的mapper

      不需要像之前写每一个mappper, 而是直接扫描存放mapper 文件(sql.xml文件)的包就可以了,包里面                    存 放对应的dao就可以了)都在bean 中完成了。原来需要一个个放入bean factory的bean 现在可以直接通过

    扫描一个包就可以了, 就像上面配置扫描@service注解 一样

整合后的mybatis配置文件:

<?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>
<!-- 原来是将数据库连接放在这里,然后就是对应的po 实体类对应建立映射 -->
<!-- 将实体类和数据库里面的表格之间构建连接 -->
<!-- 别名定义 -->
<typeAliases>
<package name="com.qf.core.po" />
</typeAliases>
</configuration>

个人理解:活基本上让spring干完了,现在mybatis 配置文件里面做的就 比较少了


SSM 配置文件 分析的更多相关文章

  1. SSH配置文件和SSM配置文件的写法

    一.SSH配置文件的写法(XML版本) <util:properties id="jdbc" location="classpath:db.properties&q ...

  2. RobotFramework 官方demo Quick Start Guide rst配置文件分析

    RobotFramework官方demo Quick Start Guide rst配置文件分析   by:授客 QQ:1033553122     博客:http://blog.sina.com.c ...

  3. Spring,SpringMVC,MyBatis,SSM配置文件比较

    Spring配置文件: applicationContext.xml applicationContext.xml是Spring的核心配置文件 IOC/DI,AOP相关配置都是在这个文件中 Sprin ...

  4. Nginx源码研究六:NGINX的配置文件分析

    上一篇写到nginx的各个模块的配置信息的存储结构,大体描述了对配置信息的配置项生成,定制,初始化过程.这里重点研究实现定制的过程,所谓实现定制,这里指的是,nginx系统提供使用者定义nginx的配 ...

  5. vue-cli的webpack模板项目配置文件分析

    由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西,开发过程中需要改动到build和config里面一些相关的配置,所以刚好趁此机会将所有配置文件看一遍,理一理思路,也便于以后修 ...

  6. ssm配置文件叙述

    spring+springmvc+mybatis框架中用到了三个XML配置文件:web.xml,spring-mvc.xml,spring-mybatis.xml.第一个不用说,每个web项目都会有的 ...

  7. vue-cli项目配置文件分析

    最近在vue-cli生成的webpack模板项目的基础上开发了一些项目,开发过程中遇到很多坑,并且需要改动build和config里面一些相关的配置,查阅,学习,总结,分享. 一.配置文件结构 本文主 ...

  8. Java初转型-SSM配置文件

    文章来源:http://www.cnblogs.com/wxisme/p/4924561.html web.xml的配置                                        ...

  9. 转:vue-cli的webpack模板项目配置文件分析

    转载地址:http://blog.csdn.net/hongchh/article/details/55113751 一.文件结构 本文主要分析开发(dev)和构建(build)两个过程涉及到的文件, ...

随机推荐

  1. $NOIp$提高组历年题目复习

    写在前面 一个简略的\(NOIp\)题高组历年题目复习记录.大部分都有单独写题解,但懒得放\(link\)了\(QwQ\).对于想的时候兜了圈子的题打上\(*\). \(NOIp2018\ [4/6] ...

  2. asp.net core 3.x 通用主机原理及使用

    一.前言 只是讲asp.net core 3.x通用主机的大致原理,这些东西是通过查看源码以及自己根据经验总结得来的,在文章中不会深入源码,因为个人觉得懂原理就晓得扩展点,后期碰到有需求的时候再仔细去 ...

  3. 【一起学源码-微服务】Ribbon 源码二:通过Debug找出Ribbon初始化流程及ILoadBalancer原理分析

    前言 前情回顾 上一讲讲了Ribbon的基础知识,通过一个简单的demo看了下Ribbon的负载均衡,我们在RestTemplate上加了@LoadBalanced注解后,就能够自动的负载均衡了. 本 ...

  4. 06_URL参数截取

    1:如何获取URL传给子页面的参数: //获得参数(只对字母数字等有效,参数值为中文则不能传) function getQueryString(name) { var reg = new RegExp ...

  5. 小白学 Python 爬虫(35):爬虫框架 Scrapy 入门基础(三) Selector 选择器

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  6. linux下安装OpenCV-2.4

    OpenCV(Open Source Computer Vision Library),是一个跨平台计算机视觉库,实现了图像处理和计算机视觉方面的很多通用算法. OpenCV由一系列 C 函数和少量 ...

  7. nor flash之写保护

    背景 没有电池的嵌入式设备,很容易发生随机掉电.因此要让产品可靠稳定,就必须保证各种场景下的掉电安全. 例如系统更新过程随机掉电,不能导致系统无法启动.例如正常读写flash过程中掉电,最多正在传输的 ...

  8. Linux 学习笔记 6 搭建nginx 实现二级域名访问

    前言 在前一节的内容里面,我们学习了如何使用yum 包管理工具来安装我们需要的软件,这节内容,通过搭建Nginx 反向代理服务器,以及学习服务的配置等内容. NGINX Nginx是一款轻量级的Web ...

  9. ArcEngine 数据编辑(IWorkspaceFactory)

    数据编辑做过很多次,没怎么出现问题,今天出现了问题,浪费了大半天,记录一下. 问题:修改Featrue的属性,修改后停止编辑,但是没有提示是否保存修改 原因:在编辑数据的时候没有加StartEditO ...

  10. TensorFlow——学习率衰减的使用方法

    在TensorFlow的优化器中, 都要设置学习率.学习率是在精度和速度之间找到一个平衡: 学习率太大,训练的速度会有提升,但是结果的精度不够,而且还可能导致不能收敛出现震荡的情况. 学习率太小,精度 ...