文章目录
Dao层整合
1.项目结构
2.添加配置文件
2.1mybatis的配置文件
2.2Spring的配置文件
2.3数据库属性文件
上篇文章介绍了mybatis的逆向工程,本文来实现SSM(SpringMVC+Spring+Mybatis)整合中的Spring整合mybatis。
Dao层整合
1.项目结构
2.添加配置文件
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"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
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"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- SqlSessionFactory --> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加载mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/Mybatis-Config.xml" /> <!-- 映射文件和接口文件不在同一个目录下的时候 它的spring是不会去扫描jar包中的相应目录的,只会去他当前项目下获取。其实要改变这种情况很简单, 在classpath后面加一个*号,*号的作用是让spring的扫描涉及全个目录包括jar --> <property name="mapperLocations" value="classpath*:mapper/*.xml"/> </bean> <!-- Mapper映射文件的包扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.bobo.mapper" /> </bean> </beans>
数据库连接池配置的是druid连接池,Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。Druid已经在阿里巴巴部署了超过600个应用,经过多年多生产环境大规模部署的严苛考验。
数据库的配置直接读取的是配置文件,因此我们需要在classpath(src/main/resource)目录下新建一个properties文件夹,然后在该目录下新建一个db.properties文件,如下图所示。
因为数据库的映射文件我们放在了logistics-manager-dao的resource目录下的mapper文件夹下一样会被打包到jar中,所以我们在配置文件中额外添加这行配置
<!-- 映射文件和接口文件不在同一个目录下的时候 它的spring是不会去扫描jar包中的相应目录的,只会去他当前项目下获取。其实要改变这种情况很简单, 在classpath后面加一个*号,*号的作用是让spring的扫描涉及全个目录包括jar --> <property name="mapperLocations" value="classpath*:mapper/*.xml"/>
2.3数据库属性文件
添加数据库的属性文件
解决属性文件中文乱码问题:要解决该问题也很简单,就是我们在db.properties文件上右键----->Properties,会看到如下图所示界面,我们选择"UTF-8"编码,然后点击"OK"。点击"OK"后有个警告,我们直接点击确定即可
#mysql数据库连接 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/logistics?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456
注意:配置文件中千万不要有空格,否则会连不上数据库,请大家一定要仔细检查
到此Dao层配置完成~