0%

spring和mybatis的整合

好久没写文章了
很久之前就开始练习Spring框架和Mybatis框架的整合,历经了层层阻碍,包括但不限于:找实习、考试、大小作业、项目deadline、懒癌、以及搞的一些其他有的没的。。终于是有时间来写一下总结

因为只是简单的整合环境,所以并没有用到Spring的AOP和IoC这些,之后会再深入

1. 建库建表

2. 导入必要的jar包

这里就不赘述了,晚上教程都大同小异,运行的时候缺什么包就补什么包给他

3. 创建pojo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package pojo;

public class Store {
private Integer id;

private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}

4. 创建接口dao

这里简单点,写一个方法作为示范就行
StoreMybatisDao.java

1
2
3
4
5
6
7
8
9
10
11
package dao;

import pojo.Store;

public interface StoreMybatisDao{


Store getStore(int id) ;


}

5. 创建与dao对应的映射文件(mapper)

StoreMybatisDao.xml

1
2
3
4
5
6
7
8
9
10
<?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">

<mapper namespace="dao.StoreMybatisDao">
<select id="getStore" parameterType="_int" resultType="pojo.Store">
SELECT * FROM store WHERE id=#{id}
</select>
</mapper>

6. Spring和Mybatis的配置文件

##applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driver}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />

<!-- 加载mybatis配置文件,用spring管理mapper -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:mybatisConfig.xml"/>
<!-- 如果是扫描式加载映射文件,在mybatisConfig中即可不用配置 -->

<!-- 配置dao -->
<bean id="storeDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="dao.StoreMybatisDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>

##mybatisConfig.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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>
<settings>
<setting name="lazyLoadingEnabled" value="false" />
</settings>

<mappers>
<mapper resource="dao/StoreMybatisDao.xml"/>
</mappers>
<!-- 若在applicationContext中配置则此处可省略 -->
</configuration>

做这几步的时候注意有个不大不小的坑:

1.如果在mybatisConfig里没有设置别名type alies,则映射文件与相应的dao最好同名,并且注意映射文件namespace的写法,以及resultType的写法,最好把包名写上,虽然不知道为什么,没写就会报错,醉了

2.最后的最后报了个奇怪的错:
Exception in thread “main” java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
查了一下说是mybatis-spring 整合的那个jar包版本问题,我用的是1.2.3,改用1.3.0后就解决了