spring和mybatis的整合我们有两个配置文件要添加,分别是spring的配置文件和mybatis的配置文件。但是这两个配置文件放在哪儿呢?因为logistics-manager-dao和logistics-manager-service都是jar工程,最终会被打成jar包,配置文件也会被打包在jar包里面,我们调用起来比较麻烦,建议放到logistics-manager-web工程中,因为logistics-manager-web是war工程,logistics-manager聚合工程最终会打包成一个war包,war包整合了聚合工程的所有内容。因此更适合进行框架整合。
2.1mybatis的配置文件
在src/main/resource目录下新建一个mybatis文件夹,然后在该文件夹下新建一个Mybatis的配置文件SqlMapConfig.xml,如下图所示
schema代码
<?xml version="1.0" encoding="UTF-8"?>2.2Spring的配置文件
在src/main/resource目录下新建一个spring文件夹,然后在该文件夹下新建一个Mybatis的配置文件applicationContext-dao.xml,如下图所示
我们在applicationContext-dao.xml文件当中配置数据库连接池、SqlSessionFactory(Mybatis的连接工厂)、Mybatis映射文件的包扫描器,配置内容如下
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context” xmlns:p=“http://www.springframework.org/schema/p”
xmlns:aop=“http://www.springframework.org/schema/aop” xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:property-placeholder location=“classpath:properties/db.properties” />
<bean id=“dataSource” class=“com.alibaba.druid.pool.DruidDataSource”
destroy-method=“close”>