在实际开发中,整合框架中的ORM框架使用的不一定是Hibernate,MyBatis也是一个轻量级、灵活的框架,也是占有这一定的分量,所以在这里记录一下Spring集成MyBatis的配置步骤,方便日后的巩固复习。
需求:
一个简单的用户类:User{id, name,password}
①根据Id查询用户对象
②添加一条用户记录
1.创建java项目Spring_MyBatis
2.添加jar包:
(1)MyBatis的jar包(http://code.google.com/p/mybatis/)
(2)spring的jar包(
http://repo.springsource.org/libs-release-local/org/springframework/spring/
)
(3)两者整合之后的jar包 mybatis-spring-1.2.2.jar(同(1))
3.创建User类:
package com.wzj.entity;
public class User {
private int id;
private String name;
private String password;
//省略get、set
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
}
4.创建UserDao接口:
package com.wzj.dao;
import com.wzj.entity.User;
public interface UserDao {
//根据Id查询用户
User selectById(int id);
//插入一条用户记录
int insert(User user);
}
5.编写UserService业务类:
package com.wzj.service;
import com.wzj.dao.UserDao;
import com.wzj.entity.User;
public class UserService {
private UserDao userDao;
//省略get、set
public User getUserById(int id){
return userDao.selectById(id);
}
//不添加param注解的话,在sql语句中的#{名字}的名字一定要相同
public int addUser(@Param("user")User user){
return userDao.insert(user);
}
}
6.创建UserMapper.xml映射文件:
<?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">
<!-- namespace:与接口的完整名称对应 -->
<mapper namespace="com.wzj.dao.UserDao">
<!-- java的User类与数据库之间的映射关系 -->
<resultMap type="com.wzj.entity.User" id="userMap">
<result property="id" column="USERID"/>
<result property="name" column="USERNAME"/>
<result property="password" column="USERPWD"/>
</resultMap>
<!--
id:与UserDao 中的方法名一致
resultType:参数类型
resultMap:返回结果类型如何映射,引用上面的resultMap
-->
<select id="selectById" parameterType="int" resultMap="userMap">
select * from myuser1 where userid=#{id}
</select>
<insert id="insert" parameterType="com.wzj.entity.User" >
insert into myuser1 values(user_sequence.nextval,#{name},#{password})
</insert>
</mapper>
7.配置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>
<!-- MyBatis的配置文件中现在只是一个空壳,其他必要配置已放在spring的配置中 -->
<mappers>
<mapper resource="com/wzj/dao/UserMapper.xml"/>
</mappers>
</configuration>
8.配置Spring配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- 1.配置数据源:DriverManagerDataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- 2.配置MyBatis的SQLSession工厂:SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- <property name="mapperLocations" value="classpath:com/wzj/dao/*.xml"/> -->
<!-- 如果在sql映射文件中使用了别名,可以使用下面的方式扫描使用别名的类的包 -->
<!-- <property name="typeAliasesPackage" value="com.wzj.entity"/> -->
</bean>
<!-- 3. 配置dao的bean:使用MapperFactoryBean -->
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 使用mapperInterface属性指定让spring实现的接口,而且只能是接口,不能是类 -->
<property name="mapperInterface" value="com.wzj.dao.UserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 配置Service -->
<bean id="userService" class="com.wzj.service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<!--创建一个事务管理的bean:DataSourceTransactionManager-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务管理的相关属性 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--织入增强 -->
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.wzj.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
</aop:config>
</beans>
注意:
(1)在sqlSessionFactory的bean中配置sql映射文件
如果sql映射文件过多,可以直接在sqlSessionFactory的bean中配置自动扫描sql映射文件,这时候就可以把mybatis-config.xml文件删除了,然后在spring中做出如下配置:
<!-- 2.配置MyBatis的SQLSession工厂:SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 配置mapperLocations属性,让spring自动扫描sql映射文件 -->
<property name="mapperLocations" value="classpath:com/wzj/dao/*.xml"/>
</bean>
(2)配置spring自动实现数据映射接口,并自动装配
应用中必然有多个像UserDao一样的接口,如果接口多了的话,相应的配置的mapperInterface也会很多。可以使用MapperScannerConfigurer类来实现让spring扫描指定的包下的接口并实现,然后通过自动装配的设置让userDao自动装配到UserService的属性中,如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- 省略其他 -->
<!-- spring自动扫描指定包下的接口并实现,不用再定义UserDao的bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.wzj.dao"/>
</bean>
<!-- 配置Service,添加autowire属性,或者在UserService的userDao属性上使用@Autowire注解 -->
<bean id="userService" class="com.wzj.service.UserService" autowire="byName"/>
<!-- 省略其他 -->
</beans>
9.编写Main方法(只是为了测试,web应用中不存在)
package com.wzj.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wzj.entity.User;
import com.wzj.service.UserService;
public class Demo {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取UserService对象
UserService us=(UserService) context.getBean("userService");
User user=us.getUserById(1);
System.out.println(user.getName()+"\t"+user.getPassword());
}
}
输出结果:user-1admin
当然,插入一条数据也是成功的。以上就是spring+mybatis的基本配置,再整合Struts2的话相对而言就比较简单了,同SSH的配置一致。