整合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 {
-
-
-
-
-
- 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;
-
-
-
- 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管理,上次是放在myBatis的configuration.xml中的;
这次的configuration.xml中清减到只配置myBatis的别名和mapper如下所示:
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ccecwg/article/details/24378497
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
-
发表于 2021-06-26 22:34:33
- 阅读 ( 742 )
- 分类: