Spring+mybatis整合

之前都在做平台类的项目,因为最近有个海外项目要急着上线,但是微服务改造计划还没完完成,只能由我来先写一个过渡性的东西,供海外使用,计划使用springboot去做,但是因为要先做一个实验性的改造,先用spring+mybatis实现原有的功能以测试和hibernate的差别是否有很大的区别,重新构建sm框架时,遇到了很多坑,研究了一下午终于搞定。

首先是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:context="http://www.springframework.org/schema/context" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd"> 
<!-- 开启自动扫描 --> 
<context:component-scan base-package="com.travelsky.cupps.db.other"></context:component-scan> 
<!-- --> 
<aop:aspectj-autoproxy /> 
<!-- 引入外部数据库的配置文件,location位置填写的是相对位置 --> 
<context:property-placeholder location="jdbc.properties" 
ignore-resource-not-found="false" local-override="false" /> 
<!-- 配置数据库连接池 --> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>


<!-- ========================================针对myBatis的配置项============================== --> 
<!-- 配置SessionFactory --> 
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
<!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 --> 
<property name="dataSource" ref="dataSource" /> 
<property name="typeAliasesPackage" value="classpath:com/travelsky/cupps/db/other/entity"></property> 
<property name="mapperLocations" value="classpath:com/travelsky/cupps/db/other/mapper/*.xml" /> 
</bean> 
<!-- 扫描mapper配置文件的地址 --> 
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
<property name="basePackage" value="com.travelsky.cupps.db.other.mapper.*xml" /> 
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
</bean> -->
<!-- DAO接口所在包名,Spring会自动查找其下的类 Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.travelsky.cupps.db.other.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置Spring的事务管理器 --> 
<bean id="transactionManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
<property name="dataSource" ref="dataSource" /> 
</bean> 
</beans>

其次是jdbc.properties:

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
jdbc.username=root
jdbc.password=root
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=1
jdbc.maxWait=60000

最后是pom.xml:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2   <modelVersion>4.0.0</modelVersion>
  3   <groupId>com.travelsky</groupId>
  4   <artifactId>data</artifactId>
  5   <version>1.0</version>
  6    <properties>
  7         <!-- spring版本号 -->
  8         <spring.version>4.1.5.RELEASE</spring.version>
  9         <!-- mybatis版本号 -->
 10         <mybatis.version>3.2.6</mybatis.version>
 11         <!-- log4j日志文件管理包版本 -->
 12         <slf4j.version>1.7.7</slf4j.version>
 13         <log4j.version>1.2.17</log4j.version>
 14     </properties>
 15   <dependencies>
 16     <dependency>
 17       <groupId>junit</groupId>
 18       <artifactId>junit</artifactId>
 19       <version>3.8.1</version>
 20       <scope>test</scope>
 21     </dependency>
 22     <!-- springmvcjar包 -->
 23         <!-- jacksonjar包 -->
 24         <dependency>
 25             <groupId>com.fasterxml.jackson.core</groupId>
 26             <artifactId>jackson-databind</artifactId>
 27             <version>2.5.0</version>
 28         </dependency>
 29         <dependency>
 30             <groupId>com.fasterxml.jackson.core</groupId>
 31             <artifactId>jackson-core</artifactId>
 32             <version>2.5.0</version>
 33         </dependency>
 34         
 35         <!-- json -->
 36         <dependency>
 37             <groupId>net.sf.json-lib</groupId>
 38             <artifactId>json-lib</artifactId>
 39             <version>2.2.3</version>
 40             <classifier>jdk15</classifier>
 41         </dependency>
 42         
 43         <dependency>
 44             <groupId>com.fasterxml.jackson.core</groupId>
 45             <artifactId>jackson-annotations</artifactId>
 46             <version>2.5.0</version>
 47         </dependency>
 48         <dependency>
 49             <groupId>org.springframework</groupId>
 50             <artifactId>spring-core</artifactId>
 51             <version>${spring.version}</version>
 52         </dependency>
 53 
 54         <dependency>
 55             <groupId>org.springframework</groupId>
 56             <artifactId>spring-web</artifactId>
 57             <version>${spring.version}</version>
 58         </dependency>
 59         <dependency>
 60             <groupId>org.springframework</groupId>
 61             <artifactId>spring-orm</artifactId>
 62             <version>${spring.version}</version>
 63         </dependency>
 64         <dependency>
 65             <groupId>org.springframework</groupId>
 66             <artifactId>spring-oxm</artifactId>
 67             <version>${spring.version}</version>
 68         </dependency>
 69         <dependency>
 70             <groupId>org.springframework</groupId>
 71             <artifactId>spring-tx</artifactId>
 72             <version>${spring.version}</version>
 73         </dependency>
 74 
 75         <dependency>
 76             <groupId>org.springframework</groupId>
 77             <artifactId>spring-jdbc</artifactId>
 78             <version>${spring.version}</version>
 79         </dependency>
 80 
 81         <dependency>
 82             <groupId>org.springframework</groupId>
 83             <artifactId>spring-webmvc</artifactId>
 84             <version>${spring.version}</version>
 85         </dependency>
 86         <dependency>
 87             <groupId>org.springframework</groupId>
 88             <artifactId>spring-aop</artifactId>
 89             <version>${spring.version}</version>
 90         </dependency>
 91         
 92         <dependency>
 93             <groupId>org.springframework</groupId>
 94             <artifactId>spring-context</artifactId>
 95             <version>${spring.version}</version>
 96         </dependency>
 97 
 98         <dependency>
 99             <groupId>org.springframework</groupId>
100             <artifactId>spring-context-support</artifactId>
101             <version>${spring.version}</version>
102         </dependency>
103 
104         <dependency>
105             <groupId>org.springframework</groupId>
106             <artifactId>spring-test</artifactId>
107             <version>${spring.version}</version>
108         </dependency>
109         
110         <dependency>
111                <groupId>org.springframework</groupId>
112                <artifactId>spring-aspects</artifactId>
113            <version>${spring.version}</version>
114 </dependency>
115     <!-- mybatis jar包 -->
116     <dependency>
117             <groupId>org.mybatis</groupId>
118             <artifactId>mybatis</artifactId>
119             <version>${mybatis.version}</version>
120         </dependency>
121         <!-- mybatis/spring包 -->
122         <dependency>
123             <groupId>org.mybatis</groupId>
124             <artifactId>mybatis-spring</artifactId>
125             <version>1.2.2</version>
126         </dependency>
127     <!-- 其他jar -->
128     <!-- 导入oracle数据库链接jar包 -->
129         <dependency>
130             <groupId>com.travelsky.cupps</groupId>
131             <artifactId>oracle</artifactId>
132             <version>10.2.0.1.0</version>
133         </dependency>
134         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
135         <dependency>
136             <groupId>commons-dbcp</groupId>
137             <artifactId>commons-dbcp</artifactId>
138             <version>1.2.2</version>
139         </dependency>
140         <!-- JSTL标签类 -->
141         <dependency>
142             <groupId>jstl</groupId>
143             <artifactId>jstl</artifactId>
144             <version>1.2</version>
145         </dependency>
146         <!-- 日志文件管理包 -->
147         <!-- log start -->
148         <dependency>
149             <groupId>log4j</groupId>
150             <artifactId>log4j</artifactId>
151             <version>${log4j.version}</version>
152         </dependency>
153         
154         <dependency>
155             <groupId>org.slf4j</groupId>
156             <artifactId>slf4j-api</artifactId>
157             <version>${slf4j.version}</version>
158         </dependency>
159 
160         <dependency>
161             <groupId>org.slf4j</groupId>
162             <artifactId>slf4j-log4j12</artifactId>
163             <version>${slf4j.version}</version>
164         </dependency>
165         <!-- 上传组件包 -->
166         <dependency>
167             <groupId>commons-fileupload</groupId>
168             <artifactId>commons-fileupload</artifactId>
169             <version>1.3.1</version>
170         </dependency>
171         <dependency>
172             <groupId>commons-io</groupId>
173             <artifactId>commons-io</artifactId>
174             <version>2.4</version>
175         </dependency>
176         <dependency>
177             <groupId>commons-codec</groupId>
178             <artifactId>commons-codec</artifactId>
179             <version>1.9</version>
180         </dependency>
181         <dependency>
182             <groupId>commons-codec</groupId>
183             <artifactId>commons-codec</artifactId>
184             <version>1.9</version>
185         </dependency>
186         <dependency>
187             <groupId>javax.servlet</groupId>
188             <artifactId>javax.servlet-api</artifactId>
189             <version>3.0.1</version>
190             <scope>provided</scope>
191         </dependency>
192         <dependency>
193             <groupId>javax.servlet.jsp</groupId>
194             <artifactId>jsp-api</artifactId>
195             <version>2.1</version>
196             <scope>provided</scope>
197         </dependency>
198         
199   </dependencies>
200 </project>

由着三个文件就能将sm框架给搭建起来,当然,一些实体还得自己写啦

posted @ 2018-06-13 18:10  程序员的苦逼生活  阅读(223)  评论(0编辑  收藏  举报