整合spring与myBatis之前,需要测试myBatis与数据库之间的链接,至少我喜欢这样做,参考上一篇文章,这次整合也是基于上一篇文章的!
javascript:void(0)
项目的源码和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的配置文件信息
我主张配置文件能分类就分类,要不然写到一块乱、难维护!
下就说说主要整合部分,其他的在上一章有说明,请参考:
javascript:void(0)
service接口:
[java]view plaincopyprint?
- package com.forum.service;
- import com.forum.po.User;
- public interface IUserService {
- /**
- * 根据ID获得User信息
- * @param id
- * @return
- */
- public User findById(String id);
- }
接口实现:
[java]view plaincopyprint?
- 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配置文件内容:
[html]view plaincopyprint?
- <?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管理,上次是放在myBatis的configuration.xml中的;
这次的configuration.xml中清减到只配置myBatis的别名和mapper如下所示:
[html]view plaincopyprint?
- <?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这个文件主要是把myBatis的dao实现放进来:
[html]view plaincopyprint?
- <?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>
Application—service.xml主要是将service接口注入进来,文件内容如下:
[html]view plaincopyprint?
- <?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:
[java]view plaincopyprint?
- 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和实体类如何请参看上一章:
sping和myBatis整合,主要就是将myBatis的
交给spring管理
项目的源码和jar包可以去我的资源下载
项目的源码和jar包可以去我的资源下载
欢迎各位广大同仁拍砖,哈哈.... 只求共同进步!