Mybatis与Spring集成
本文主要介绍Spring与Mybatis二种常用整合方法,需要的整合架包是mybatis-spring.jar;
1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引入数据库资源-->
<property name="dataSource" ref="dataSource"></property>
<!--加载mybatis核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--引入你的dao层-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--引入userMapper 具体的方法引入-->
<property name="mapperInterface" value="com.offcn.mapper.UserMapper"></property>
<!--注入你的sqlSessionFactory-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--通过扫描dao层注解配置dao层的实现-->
<!--扫描这个包下的所有的类-->
<!-- <property name="basePackage" value="com.offcn.mapper"></property>-->
<!--</bean>-->
</beans>
mybatis核心配置文件:
<?xml version="1.0" encoding="UTF-8" ?><typeAliases>
<!--取别名,类名就是别名-->
<package name="com.offcn.entity"></package>
</typeAliases>
</configuration>
UserMapper.xml文件配置:
<?xml version="1.0" encoding="UTF-8" ?><select id="selectAllPage" resultType="User" >
select * from smbms_user limit #{currentPageNo},#{pageSize}
</select>
**UserMapper接口**:
import com.offcn.entity.User;
/导入注解包/
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
//分页:第一个参数当前页,第二个是页量
List<User> selectAllPage(@Param("currentPageNo") Integer currentPageNo,
@Param("pageSize") Integer pageSize);
}
测试类:
package com.offcn.test;
import com.offcn.entity.User;
import com.offcn.mapper.UserMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Text {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) app.getBean("userMapper");
List<User> list = userMapper.selectAllPage(1, 5);
for (User u:list
) {
System.out.println(u.getUserName()+"---"+u.getUserPassword());
}
}
}
Mybatis与Spring集成配置事物代理:
第一种方式:
applicationContext.xml文件:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引入数据库资源-->
<property name="dataSource" ref="dataSource"></property>
<!--加载mybatis核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--配置userDao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.offcn.mapper"></property>
</bean>
<!--<bean id="userService" class="com.offcn.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
</bean>-->
<!--定义一个事物管理器的第一种-->
<bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--引入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--定义事物通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManger">
<tx:attributes>
<!--代表对应的方法名 第二个代表是事物的传播行为,事物失效的时间,事物的隔离级别,指定异常进行回滚-->
<tx:method name="find" propagation="SUPPORTS" timeout="10000" isolation="DEFAULT" />
<tx:method name="add" propagation="REQUIRED"></tx:method>
<tx:method name="*" propagation="REQUIRED"></tx:method>
</tx:attributes>
</tx:advice>
<!--定义切面-->
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.offcn.UserService.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"></aop:advisor>
</aop:config>
</beans>
多加两个包:
一个是UserService层;一个是它的实现层UserServiceImpl
UserService
package com.offcn.service;
import com.offcn.entity.User;
import com.offcn.mapper.UserMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface UserService {
void addUser(List<User> user);
}
UserServiceImpl
package com.offcn.service.impl;
import com.offcn.entity.User;
import com.offcn.mapper.UserMapper;
import com.offcn.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service(“userService”)
/声明事物的注解/
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
/*--等于application.xml文件<tx>标签里的add对应--*/
@Transactional(propagation = Propagation.REQUIRED)
/*用于默认的,只有查寻不用这个*/
public void addUser(List<User> list) {
for (User u:list
) {
userMapper.addUser(u);
throw new RuntimeException("数据库出错去");
}
}
}
测试类:
package com.offcn.test;
import com.offcn.entity.User;
import com.offcn.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
public class Text {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) app.getBean("userService");
List<User> list=new ArrayList<User> ();
User user=new User();
user.setUserName("ddd");
user.setUserPassword("1111");
User user1=new User();
user1.setUserName("ffff");
user1.setUserPassword("22222");
list.add(user);
list.add(user1);
userService.addUser(list);
}
}
Mybatis与Spring集成配置事物代理:
第二种方式:applicationContext.xml的配置
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--自动扫描包-->
<context:component-scan base-package=“com.offcn.service”/>
tx:annotation-driven/
aop:config
<aop:pointcut id=“serviceMethod”
expression=“execution(* com.offcn.service..(…))” />
<aop:advisor advice-ref=“txAdvice” pointcut-ref=“serviceMethod” />
</aop:config>
下一篇: Web 前端开发怎么和后台进行相衔接?
推荐阅读