spring整合myBatis

整合spring与myBatis之前,需要测试myBatis与数据库之间的链接,至少我喜欢这样做,参考上一篇文章,这次整合也是基于上一篇文章的!

http://blog.csdn.net/huzheaccp/article/details/7399124

项目的源码和jar包可以去我的资源下载

项目的源码和jar包可以去我的资源下载

整合之前需要jar包,网上搜一搜,本次用:spring 3.0.1 + myBatis 3.0+ mybatis-spring 1.0

整个项目的一个结构:

 

说明:

UserMapper:dao接口 userMapper.xml是myBatis针对dao接口的实现

Entity不用管、user是实体类、

IuserService 是service接口 UserService是针对service接口的实现

SpringTest 是针对本次整合的一个测试类

ApplicationContext-mapper.xml 是myBatis的配置文件信息

ApplicationContext-service.xml 是spring配置文件信息

ApplicationContext.xml是spring的配置文件信息

我主张配置文件能分类就分类,要不然写到一块乱、难维护!

下就说说主要整合部分,其他的在上一章有说明,请参考:

http://blog.csdn.net/huzheaccp/article/details/7399124

 

service接口:

package com.forum.service; import com.forum.po.User; public interface IUserService { /** * 根据ID获得User信息 * @param id * @return */ public User findById(String id); }
接口实现:

 

package com.forum.service.impl; import com.forum.dao.UserMapper; import com.forum.po.User; import com.forum.service.IUserService; public class UserServiceImpl implements IUserService { private UserMapper userMapper; /** * 根据ID获得USER信息 */ public User findById(String id) { return userMapper.findById(id); } public UserMapper getUserMapper() { return userMapper; } public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } }

 

ApplicationContext.xml配置文件内容:

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"></property> <property name="url" value="jdbc:db2://localhost:50000/forum"></property> <property name="username" value="DB2ADMIN"></property> <property name="password" value="admin"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:configuration.xml"></property> <property name="dataSource" ref="dataSource" /> </bean> <import resource="applicationContext-*.xml"/> </beans>

整合的时候我把数据库配置文件放到了spring管理,上次是放在myBatisconfiguration.xml中的;

这次的configuration.xml中清减到只配置myBatis的别名和mapper如下所示:

 

<?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> <typeAliases> <!--给实体类起一个别名 user --> <typeAlias type="com.forum.po.User" alias="User" /> </typeAliases> <mappers> <!--userMapper.xml装载进来 同等于把“dao”的实现装载进来 --> <mapper resource="com/forum/dao/impl/userMapper.xml" /> </mappers> </configuration>

 

ApplicationContext-mapper.xml这个文件主要是把myBatisdao实现放进来:

 

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="userMapper" class="org.mybatis.spring.MapperFactoryBean"> <property name="mapperInterface" value="com.forum.dao.UserMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>

 

Applicationservice.xml主要是将service接口注入进来,文件内容如下:

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="userService" class="com.forum.service.impl.UserServiceImpl"> <property name="userMapper" ref="userMapper"></property> </bean> </beans>

测试类:springTest:

package com.forum.test; import junit.framework.TestCase; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.forum.po.User; import com.forum.service.IUserService; public class SpringTest extends TestCase { @Test public void testSpring(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserService userService = (IUserService) applicationContext.getBean("userService"); User user = userService.findById("1"); System.out.println(user.getName()); } }

Dao和实体类如何请参看上一章:

http://blog.csdn.net/huzheaccp/article/details/7399124

spingmyBatis整合,主要就是将myBatis

交给spring管理


 

项目的源码和jar包可以去我的资源下载

项目的源码和jar包可以去我的资源下载

 


欢迎各位广大同仁拍砖,哈哈.... 只求共同进步!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值