1、查看不同MyBatis版本整合Spring时使用的适配包; http://www.mybatis.org/spring/

2、下载整合适配包

https://github.com/mybatis/spring/releases

• 3、官方整合示例,jpetstore

https://github.com/mybatis/jpetstore-6

MyBatis-Spring MyBatis Spring
1.0.0 或 1.0.1 3.0.1 到 3.0.5 3.0.0 或以上
1.0.2 3.0.6 3.0.0 或以上
1.1.0 3.1.0 或以上 3.0.0 或以上

Mybatis整合Spring包

mybatis-spring-1.3.2.jar

1.整合Mybatis和Spring配置

结构

1.1 类

com.tangge.bean.employee.java

package com.tangge.bean;

import java.io.Serializable;

public class employee implements Serializable{
private int id;
private String lastName;
private String email;
private String gender;
private deptment dept; public employee() {
} public employee(int id, String lastName, String email, String gender) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
} public employee(String lastName, String email, String gender) {
this.lastName = lastName;
this.email = email;
this.gender = gender;
} public deptment getDept() {
return dept;
} public void setDept(deptment dept) {
this.dept = dept;
} @Override
public String toString() {
return "employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
", dept=" + dept +
'}';
} public int getId() {
return id;
} public String getLastName() {
return lastName;
} public String getEmail() {
return email;
} public String getGender() {
return gender;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public void setEmail(String email) {
this.email = email;
} public void setGender(String gender) {
this.gender = gender;
} }

1.2 接口

com.tangge.dao.employeeMapper.java

public interface employeeMapper {
public List<employee> getEmployees();
}

1.3 服务层

com.tangge.service.employeeService.java

package com.tangge.service;

import com.tangge.dao.employeeMapper;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tangge.bean.employee; @Service
public class employeeService { @Autowired
private employeeMapper employeeMapper; public List<employee> getemps(){
return employeeMapper.getEmployees();
}
}

1.4 mapper 配置XML

employeeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
namespace:命名空间,指定为接口的全类名
-->
<mapper namespace="com.tangge.dao.employeeMapper">
<!--<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>--> <select id="getEmployees" resultType="com.tangge.bean.employee">
select `id`, `last_name` lastName, `gender`, `email` from tbl_employee
</select>
</mapper>

1.5 mybatis 简单配置 XML

mybatis-config.xml 只留下setting等一些简单配置

<?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> <settings>
<setting name="logImpl" value="LOG4J"/>
<setting name="jdbcTypeForNull" value="NULL"/>
<!--显示指定每个我们需要更改的值,即使他是默认的。防止版本更迭带来的问题-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!--<setting name="mapUnderscoreToCamelCase" value="true"></setting>-->
</settings> <databaseIdProvider type="DB_VENDOR">
<property name="SQL Server" value="sqlserver"/>
<property name="MySQL" value="mysql"/>
<property name="DB2" value="db2"/>
<property name="Oracle" value="oracle"/>
</databaseIdProvider> </configuration>

1.6 * 最重要的spring配置

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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd"> <!--指定Spring希望控制所有业务逻辑组件:扫描包-->
<context:component-scan base-package="com.tangge"></context:component-scan> <!--Spring控制业务逻辑。数据源。事务控制。AOP--> <!--(1)数据源: 引用外部文件db.properties -->
<context:property-placeholder location="classpath:tangge/db.properties"/>
<!--配置jdbc-->
<bean id="datasource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${drivername}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${pass}"></property>
</bean> <!--事务管理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<!--启用事务注解 http://www.springframework.org/schema/tx-->
<!--http://www.springframework.org/schema/tx/spring-tx.xsd-->
<tx:annotation-driven transaction-manager="transactionManager"/> <!--
(2)整合mybatis
目的:1.spring管理所有组件,mapper的实现类
2.spring管理事务,spring声明式事务
-->
<!--(2.1)创建 SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<!--全局配置文件位置-->
<property name="configLocation" value="classpath:tangge/mybatis-config.xml"></property>
<!--指定 mapper 文件位置-->
<property name="mapperLocations" value="classpath:tangge/mapper/*.xml"></property>
</bean> <!--(2.2)扫描所有mapper,自动注入
base-package:指定包下所有的mapper接口实现自动扫描并加入到ioc容器中
-->
<!--<mybatis-spring:scan base-package="com.tangge.dao" />-->
<!--第2个写法-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tangge.dao" />
</bean> </beans>

---->【测试】:

public class SpringTest {

    public static void main(String[] args) {
SpringTest test = new SpringTest();
test.getFirstLevelCache();
} public void getFirstLevelCache() {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:tangge/applicationContext.xml"); employeeService service = applicationContext.getBean(employeeService.class); List<employee> list = service.getemps();
System.out.println(list);
} }

2.对应关系图

3.问题

java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException

一:jar包未加载完整。

二:Eclipse/idea 运行JVM内存过小,调整JVM内存。

解决:

Eclipse:

在Window->Preferences中,选择Java->Installed JREs,修改已配置的JDK。

配置Default VM arguments即可。

-Xmx512M -Xms512M -XX:MaxPermSize=256M-Xss512K

https://blog.csdn.net/u013355724/article/details/52222463

IDEA:

idea.exe.vmoptions

idea64.exe.vmoptions

修改1024,两个

MyBatis - 6.Spring整合MyBatis的更多相关文章

  1. Mybatis(六) Spring整合mybatis

    心莫浮躁~踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些~ so,努力啥的都是强迫自己做自以为 ...

  2. Mybatis和Spring整合&逆向工程

    Mybatis和Spring整合&逆向工程Mybatis和Spring整合mybatis整合Spring的思路目的就是将在SqlMapConfig.xml中的配置移植到Spring的appli ...

  3. spring基础:什么是框架,框架优势,spring优势,耦合内聚,什么是Ioc,IOC配置,set注入,第三方资源配置,综合案例spring整合mybatis实现

    知识点梳理 课堂讲义 1)Spring简介 1.1)什么是框架 源自于建筑学,隶属土木工程,后发展到软件工程领域 软件工程中框架的特点: 经过验证 具有一定功能 半成品 1.2)框架的优势 提高开发效 ...

  4. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  5. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  6. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...

  7. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二

    接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...

  8. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)一

    MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中. 使用这个类库中的类, Spring 将会加载必要的MyBatis工厂类和 session 类. 这个类库 ...

  9. Mybatis学习--spring和Mybatis整合

    简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...

随机推荐

  1. MySQL常用的sql操作

    1.日期时间格式化 2.日期时间格式化 3.查询第11到第15条数据 ,5//落过多少,取出多少. 4.字符串转日期 select str_to_date('2016-01-02', '%Y-%m-% ...

  2. 正则表达式、BeautifulSoup、Lxml进行性能对比

    爬取方法 性能 使用难度 安装难度 正则表达式 快 困难 简单(内置) BeautifulSoup 慢 简单 简单 Lxml 快 简单 相对困难

  3. k64 datasheet学习笔记35---Analog-to-Digital Converter (ADC)

    0.前言 本文主要介绍K64的ADC部分的特性,内部架构,信号描述及操作接口等 1.简介 1.1.ADC模块特性 线性逐次逼近算法,达16bit分辨率 达到4对差分和24个单端模拟量输入 输出模式 差 ...

  4. Python学习前期准备---第九天

    一.Linux基础 - 计算机以及日后我们开发的程序防止的服务器的简单操作 二.Python开发 http://www.cnblogs.com/wupeiqi/articles/5433893.htm ...

  5. 运维与自动化系列③自动化部署基础与shell脚本实现

    自动化部署基础与shell脚本实现 关于自动化的基础知识: 1.1:当前代码部署的实现方式: 运维纯手工scp到web服务器纯手工登录git服务器执行git pull或svn服务器执行svn upda ...

  6. cache、session、cookie的区别

    session把数据保存在服务器端,每一个用户都有属于自己的Session,与别人的不冲突就是说,你登陆系统后,你的信息(如账号.密码等)就会被保存在服务器上一个单独的session中,当你退出系统后 ...

  7. VS下载Github的项目引用报错

    错误如下 1.添加新的Nuget包源 添加新的源 https://www.nuget.org/api/v2/ 2.还原Nuget包 3.升级Visual Studio到最新 工具-->获取工具和 ...

  8. 软件包.deb的安装及卸载------dpkg

    文章链接:https://blog.csdn.net/qq_36764147/article/details/81332606 删除带有rc的软件包:https://blog.csdn.net/chr ...

  9. vue打包项目后使用-webkit-line-clamp: 2;这个属性不生效?

    在项目中要实现多行省略,-webkit-line-clamp: 2;打包后不生效,使用下面的方法: word-break: break-all; text-overflow: ellipsis; di ...

  10. LuoGu P1083 借教室

    题目传送门 借教室这个题有两种做法,差分+前缀和或者是线段树 线段树维护区间最小值,因为能不能借教室使用是由这几天中可用教室最少的一天决定的 其实这题是个很裸的线段树维护区间最小值,但有一点需要注意, ...