教大家看官方技术文档(二)- Spring MVC (续)
续上一篇文章,继续看看如何整合Mybatis实现mysql数据库操作
主要从以下方向出发:
1)Spring配置dataSource, 包括driverclassname, jdbc url等等
2)Spring配置Mybatis的SQLSessionFactory
3)Mybatis的Configuration XML和Mapper XML files
第一步:登录Mysql官网,查找mysql如何整合Spring相关内容。
2.12 Using Connector/J with Spring
http://dev.mysql.com/doc/connectors/en/connector-j-usagenotes-spring-config.html
<util:map id="dbProps">
<entry key="db.driver" value="com.mysql.jdbc.Driver"/>
<entry key="db.jdbcurl" value="jdbc:mysql://localhost/world"/>
<entry key="db.username" value="myuser"/>
<entry key="db.password" value="mypass"/>
</util:map>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.jdbcurl}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
另外,本文打算用connection pool的方法,于是继续看
2.12.3 Connection Pooling with Spring
http://dev.mysql.com/doc/connectors/en/connector-j-usagenotes-spring-config.html
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.jdbcurl}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="initialSize" value="3"/>
</bean>
综合上面的文档,标准的用法之一如下。
在本教程的spring上下文xml添加如下代码,写到
datasource-config.xml文件:
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/shop?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="initialSize" value="3"/>
</bean>
第二步:登录Mybatis官网研究如何整合到Spring
引入题外话:
Mybatis的标准用法,非整合Spring,对Mybatis不熟悉的可以先看:
http://www.mybatis.org/mybatis-3/index.html
标准用法的Getting started可以得到基本配置如下:
a.添加mybatis的Maven项目包pom
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
b. 配置configuration XML file mybatis-config.xml
<?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>
...
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
c. 配置Mapper XML file
<?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="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
明白了基本配置的用法,就可以比较着看如何整合Spring了。
Mybatis的整合Spring用法:
http://www.mybatis.org/spring/getting-started.html
a.添加mybatis-spring基础包pom
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>x.x.x</version>
</dependency>
b. 配置SqlSessionFactoryBean指定 Mapper XML files的位置
参考SqlSessionFactoryBean的Properties配置说明
http://www.mybatis.org/spring/factorybean.html
(在官网文档可以看出Spring整合Mybatis的方法其实有多种,下面列举的是比较直观的一种)
在Spring的上下文xml配置如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
</bean>
这里相当于标准用法的step b (mybatis-config.xml指定mapper.xml位置)
c. 配置Mapper XML file
(配置和标准用法的step c一样,这里把Blog改为User,位置放在sample/config/mappers/里)
<?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="org.mybatis.example.UserMapper">
<select id="selectUser" resultType="User">
select * from User where id = #{id}
</select>
</mapper>
d.在Spring的上下文xml添加MapperFactoryBean
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
e. Service层调用Mapper的方法:
http://www.mybatis.org/spring/getting-started.html和http://www.mybatis.org/spring/mappers.html
Register Service bean
<bean id="fooService" class="org.mybatis.spring.sample.mapper.FooServiceImpl">
<property name="userMapper" ref="userMapper" />
</bean>
Service调用Mapper
public class FooServiceImpl implements FooService {
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public User doSomeBusinessStuff(String userId) {
return this.userMapper.getUser(userId);
}
}
Mapper interface:
public interface UserMapper {
User getUser(String userId);
}
完整的例子稍后会在GitHub提供。
官方文档的好处:
资料详尽,有最新的用法说明,每种用法有细节提示,可以挖掘更多功能,适合有使用经验的人快速参考。
官方文档的不足:
资料零散,每个配置有太多用法,容易让人看着迷失方向,不适合没有使用经验的人快速入门。