spring整合myBatis - Go语言中文社区

spring整合myBatis


spring整合myBatis

分类: MyBatis   4884人阅读  评论(6)  收藏  举报

整合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接口:

  1. package com.forum.service;  
  2.   
  3. import com.forum.po.User;  
  4.   
  5. public interface IUserService {  
  6.     /** 
  7.      * 根据ID获得User信息 
  8.      * @param id 
  9.      * @return 
  10.      */  
  11.     public User findById(String id);  
  12.   
  13. }  

接口实现:

  1. package com.forum.service.impl;  
  2.   
  3. import com.forum.dao.UserMapper;  
  4. import com.forum.po.User;  
  5. import com.forum.service.IUserService;  
  6.   
  7. public class UserServiceImpl implements IUserService {  
  8.     private UserMapper userMapper;  
  9.     /** 
  10.      * 根据ID获得USER信息 
  11.      */  
  12.     public User findById(String id) {  
  13.         return userMapper.findById(id);  
  14.     }  
  15.   
  16.     public UserMapper getUserMapper() {  
  17.         return userMapper;  
  18.     }  
  19.   
  20.     public void setUserMapper(UserMapper userMapper) {  
  21.         this.userMapper = userMapper;  
  22.     }  
  23.       
  24.       
  25.     
  26. }  

ApplicationContext.xml配置文件内容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  8.         <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"></property>  
  9.         <property name="url" value="jdbc:db2://localhost:50000/forum"></property>  
  10.         <property name="username" value="DB2ADMIN"></property>  
  11.         <property name="password" value="admin"></property>  
  12.         <property name="maxActive" value="100"></property>  
  13.         <property name="maxIdle" value="30"></property>  
  14.         <property name="maxWait" value="500"></property>  
  15.         <property name="defaultAutoCommit" value="true"></property>  
  16.     </bean>  
  17.   
  18.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  19.         <property name="configLocation" value="classpath:configuration.xml"></property>  
  20.         <property name="dataSource" ref="dataSource" />  
  21.     </bean>  
  22.     <import resource="applicationContext-*.xml"/>  
  23. </beans>  

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

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

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢