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. 16python的map函数,filter函数,reduce函数

    map num_l = [1,6,8,9] def map_test(func,array): ret = [] for i in array: res = func(i) ret.append(re ...

  2. 优雅的使用BeanUtils对List集合的操作

    摘要 在业务员流程的时候,我们在Entity.Bo.Vo层数据间可能经常转换数据,Entity对应的是持久层数据结构(一般是数据库表的映射模型).Bo对应的是业务层操作的数据结构.Vo就是Contro ...

  3. java的package和import机制

    在说package.import机制前我们先来了解下java的CLASSPATH. CLASSPATH顾名思义就是class的路径,当我们在系统中运行某个java程序时,它就会告诉系统在这些地方寻找这 ...

  4. 机器学习之路--seaborn

    seaborn是基于plt的封装好的库.有很强的作图功能. 1.布局风格设置(图形的style)and 细节设置 用matplotlib作图: import numpy as np import ma ...

  5. .NET BS端和CS端相互压缩发送接收byte对象数据方法

    本文是总结实际项目经验,代码不少是学习别人整合的,效果稳定可靠,有很大参考价值:但是也有不全面的地方,朋友们拿到可以按照自己需要修改. 场景是项目需要在客户端控制台软件和.NET MVC站点间互相传递 ...

  6. php hash比较缺陷

    PHP在处理哈希字符串时,会利用”!=”或”==”来对哈希值进行比较,它把每一个以”0E”开头的哈希值都解释为0,所以如果两个不同的密码经过哈希以后,其哈希值都是以”0E”开头的,那么PHP将会认为他 ...

  7. 【转】iOS 7免费设计资源汇总

    原文链接:http://mobile.51cto.com/hot-406317.htm#585532-tsina-1-28470-7e393678b940a4d55500bf3feae3d2e9 以下 ...

  8. Tensorflow内存暴涨问题

    1.目前只总结出两条 创建saver实例saver = tf.train.Saver()放在循环外面 不循环初始化变量 sess.run(tf.global_variables_initializer ...

  9. python对象的初始化

    效果图: 代码: # 对象的初始化 class Person: # 在类中可以定义一些特殊方法(魔术方法) # 特殊方法都是以__开头,__结尾的方法 前后都是两个下划线 # 特殊方法会在特殊的时刻自 ...

  10. python条件(三元)运算符

    条件运算符  是 三元运算符 语法: 语句1  if   条件表达式  else  语句2 执行流程: 条件运算符在执行时,会先对条件表达式进行求值判断         如果判断结果为True,则执行 ...