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 配置文件里面做的就 比较少了


最新文章

  1. 使用ANTS Performance Profiler&amp;ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题
  2. 《图解Spark:核心技术与案例实战》介绍及书附资源
  3. JSP隐含变量和Spring中Model在EL表达式中的读取顺序
  4. 关于webdriver中弹出框的定位
  5. Hbase快速开始——shell操作
  6. 【2016-07-11】Qt远程部署失败,提示&quot;没有那个文件或目录&quot;的解决方法
  7. 蓝牙—GAP(Generic Access Profile)
  8. Android开发-API指南-数据存储
  9. winform - BackgroundWorker
  10. oracle时间处理
  11. RPi 2B Raspbian SD卡内部架构
  12. 请大神帮忙解决 jquery 控制 li 标签问题
  13. B - The Accomodation of Students - hdu 2444(最大匹配)
  14. 配置php开发环境
  15. 怎么把自己电脑上开发的项目发布到自己电脑IIS上面?
  16. 《连载 | 物联网框架ServerSuperIO教程》- 17.支持实时数据库,高并发保存测点数据。附:3.4 发布与版本更新说明。
  17. ecshop收货地址货到付款修改
  18. Struts简介、原理及简单实现
  19. 元组Tuple的使用
  20. SpringBoot打包成war

热门文章

  1. pyinstaller打包py脚本Warning:lib not found等相关问题
  2. Python学习3月8号【python编程 从入门到实践】---》笔记(1)
  3. c++ 屏幕截图指定窗口句柄后台截图返回位图句柄
  4. $Luogu4403$ 秦腾与教学评估 二分
  5. SQLServer系统函数之字符串函数
  6. 「每日五分钟,玩转JVM」:指针压缩
  7. MySQL 物理备份工具-xtrabackup
  8. springboot2 + grpc + k8s + istio
  9. Spirng Boot2 系列教程(二十二)| 启动原理
  10. 网络流入门题目 - bzoj 1001