日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

MyBatis - 6.Spring整合MyBatis

發布時間:2023/11/27 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis - 6.Spring整合MyBatis 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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;}@Overridepublic 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 {@Autowiredprivate employeeMapper employeeMapper;public List<employee> getemps(){return employeeMapper.getEmployees();}
}

1.4 mapper 配置XML

employeeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://mybatis.org/schema/mybatis-springhttp://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,兩個

轉載于:https://www.cnblogs.com/tangge/p/9558817.html

總結

以上是生活随笔為你收集整理的MyBatis - 6.Spring整合MyBatis的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。