一文吃透Spring集成MyBatis
??集成思路
spring能集成很多的框架,是spring一個(gè)優(yōu)勢(shì)功能。通過(guò)集成功能,讓開(kāi)發(fā)人員使用其他框架更方便。集成使用的是spring ioc 核心技術(shù)。
?怎么使用MyBatis
使用mybatis,需要?jiǎng)?chuàng)mybatis框架中的某些對(duì)象,使用這些對(duì)象,就能使用mybatis提供的功能了。
分析: mybatis執(zhí)行sql語(yǔ)句,需要使用那些對(duì)象?
1. 需要有Dao接口的代理對(duì)象,例如StudentDao接口,需要一個(gè)它的代理對(duì)象,使用 SqlSession.getMapper(StudentDao.class),得到dao代理對(duì)象。
2. 需要有SqlSessionFactory,創(chuàng)建SqlSessionFactory對(duì)象,才能使用openSession(得到SqlSession對(duì)象。
3. 數(shù)據(jù)源DataSource對(duì)象,使用一個(gè)更強(qiáng)大,功能更多的連接池對(duì)象代替mybatis自己的PooledDataSource。
?集成的步驟
實(shí)現(xiàn)步驟:
1.使用的mysql庫(kù), 使用學(xué)生表 student2(id int 主鍵列, 自動(dòng)增長(zhǎng),
name varchar(80),
age int)2.創(chuàng)建maven項(xiàng)目
3.加入依賴(lài)gav
spring依賴(lài), mybatis依賴(lài), mysql驅(qū)動(dòng)。 junit依賴(lài)
mybatis-spring依賴(lài)(mybatis網(wǎng)站上提供的,用來(lái)在spring項(xiàng)目中,創(chuàng)建mybatis對(duì)象)
spring有關(guān)事務(wù)的依賴(lài)。mybatis和spring整合的時(shí)候, 事務(wù)是自動(dòng)提交的。
4.創(chuàng)建實(shí)體類(lèi)Student
5.創(chuàng)建Dao接口和mapper文件寫(xiě)sql語(yǔ)句
6.寫(xiě)mybatis主配置文件
7.創(chuàng)建service接口和他的實(shí)現(xiàn)類(lèi)
8.創(chuàng)建spring的配置文件
1)聲明數(shù)據(jù)源DataSource,使用的阿里的Druid連接池
2) 聲明SqlSessionFactoryBean類(lèi),在這個(gè)類(lèi)內(nèi)部創(chuàng)建的是SqlSessionFactory對(duì)象。
3)聲明MapperScannerConfiguration類(lèi),在內(nèi)部創(chuàng)建dao代理對(duì)象,
創(chuàng)建的對(duì)象都放在spring容器中。4)聲明Service對(duì)象,把3)的中dao賦值給service屬性
9.測(cè)試dao訪問(wèn)數(shù)據(jù)庫(kù)
?pom加入依賴(lài)
<dependencies>
<!--測(cè)試-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring依賴(lài)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--spring事務(wù)依賴(lài)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--mybatis依賴(lài)-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mybatis和spring集成-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!--阿里的連接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
<build>
<!--用mybatis就要用到這個(gè)插件 編譯java目錄下,除了java文件,還編譯xml和properties文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>?創(chuàng)建MyBatis使用代碼
1.創(chuàng)建實(shí)體類(lèi),生成get、set和toString方法。
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}2.創(chuàng)建Dao接口,在里面寫(xiě)方法。
public interface StudentDao {
//添加操作
int insertStudent(Student student);
//查詢(xún)操作
List<Student> selectStudents();
}3.創(chuàng)建mapper文件,寫(xiě)SQL。
<?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">
<mapper namespace="youfei1_v.Dao.StudentDao">
<!-- 使用insert,update,select標(biāo)簽寫(xiě)sql -->
<insert id="insertStudent" >
insert into student2(name,age) values (#{name},#{age})
</insert>
<select id="selectStudents" resultType="youfei1_v.domain.Student">
select id,name ,age from student2
</select>
</mapper>4.創(chuàng)建Mybatis主配置文件。
注意:Spring集成MyBatis,MyBatis主配置文件里面不需要指定數(shù)據(jù)源了,下面會(huì)介紹在哪里指定。基本上Mybatis主配置文件里面就設(shè)置個(gè)日志、別名和其它的mapper文件。
<?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>
<!--設(shè)置日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--指定其它mapper文件的位置,目的是找到其它文件的sql語(yǔ)句-->
<mappers>
<!--使用mapper的resource屬性指定mapper文件的路徑。
這個(gè)路徑是從類(lèi)路徑根目錄開(kāi)始
使用注意:mapper文件的路徑使用 / 分割路徑
一個(gè)mapper resource 指定一個(gè)mapper文件
-->
<!--<mapper resource="youfei1_v/dao/StudentDao.xml"/>-->
<!--name:mapper文件所在的包名
滿(mǎn)足要求才能使用這種方式:
1.mapper文件和dao接口在同一目錄
2.mapper文件和dao接口名稱(chēng)一致
-->
<package name="youfei1_v.Dao"/>
</mappers>
</configuration>?創(chuàng)建Service類(lèi)
1.創(chuàng)建service接口。
public interface StudentService {
int addStudent(Student student);
List<Student> queryStudent();
}2.實(shí)現(xiàn)service接口。
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao; //創(chuàng)建這個(gè)dao對(duì)象和set方法
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
int rows = studentDao.insertStudent(student); //方法中調(diào)用studentDao對(duì)象的方法
return rows;
}
@Override
public List<Student> queryStudent() {
List<Student> students = studentDao.selectStudents();
return students;
}
}?創(chuàng)建Spring配置文件和測(cè)試集成MyBatis
1.創(chuàng)建Spring配置文件。
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--加載外部的屬性配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--聲明數(shù)據(jù)源DataSource 德魯伊-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/springdb"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--聲明SqlSessionFactoryBean,在這個(gè)類(lèi)的內(nèi)部,創(chuàng)建SqlSessionFactory bean的id就代表創(chuàng)建SqlSessionFactory對(duì)象-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定數(shù)據(jù)源-->
<property name="dataSource" ref="myDataSource" /> <!--用的是什么數(shù)據(jù)源的id-->
<!--指定mybatis主配置文件
Resource可以直接使用 value屬性賦值。
-->
<property name="configLocation" value="classpath:mybatis.xml" />
</bean>
<!--
相當(dāng)于:SqlSessionFactory factory = new SqlSessonFactoryBuider.build(classpath:mybatis.xml)
-->
<!--聲明MapperScannerConfigurer
SqlSession.getMapper(StudentDao.class)
MapperScannerConfigurer作用是:
循環(huán)basePackage所表示的包,把包中的每個(gè)接口都找到,調(diào)用SqlSession.getMapper
把每個(gè)dao接口都創(chuàng)建出dao對(duì)象 ,dao代理對(duì)象放在容器中。
ApplicationContext ctx = ....
SqlSessionFactory sqlSessionFactory = ctx.getBean("factory");
SqlSession session = sqlSessionFactory.openSession();
for(接口: com.bjpowernode.dao){
接口 對(duì)象 = session.getMapper(接口)
springMap.put(接口名的首字母小寫(xiě), 對(duì)象) //創(chuàng)建dao代理,這個(gè)dao代理的對(duì)象的名字是這個(gè)接口的首字母小寫(xiě)。也就是bean的id
}
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定SqlSessionFactory對(duì)象的名稱(chēng)-->
<property name="sqlSessionFactoryBeanName" value="factory" />
<!--指定基本包,dao接口所在的包名-->
<property name="basePackage" value="youfei1_v.Dao" /><!--創(chuàng)建dao代理,這個(gè)dao代理的對(duì)象的名字是這個(gè)接口名的首字母小寫(xiě)。也就是bean的id-->
</bean>
<!--聲明service-->
<bean id="studentService" class="youfei1_v.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" /> <!--set注入:把dao代理賦值給StudentServiceImpl類(lèi)中的StudentDao對(duì)象-->
</bean> <!--ref用的就是上面的創(chuàng)建出來(lái)的dao代理,的id值,這個(gè)id值就是接口名的首字母小寫(xiě)-->
<!--ref="studentDao" :studentDao指的是Student這個(gè)接口的代理。上面那個(gè)bean,創(chuàng)建出Student接口的代理,這個(gè)代理的id就是接口名字小寫(xiě)studentDao-->
</beans>2.測(cè)試

?使用外部屬性配置文件
1.創(chuàng)建一個(gè).properties的文件

2.在spring配置文件中引用

以上就是一文吃透Spring集成MyBatis的詳細(xì)內(nèi)容,更多關(guān)于Spring集成MyBatis的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Netty啟動(dòng)流程注冊(cè)多路復(fù)用源碼解析
這篇文章主要介紹了Netty啟動(dòng)流程注冊(cè)多路復(fù)用源碼分析,繼續(xù)分析channel是如何注冊(cè)到selector中的,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03
使用Spring?Cloud?Stream處理事件的示例詳解
Spring?Cloud?Stream?是基于?Spring?Boot?的用于構(gòu)建消息驅(qū)動(dòng)微服務(wù)的框架,本文主要介紹了如何使用?Spring?Cloud?Stream?來(lái)處理事件,需要的可以參考一下2023-06-06
詳解Mybatis-plus(MP)中CRUD操作保姆級(jí)筆記
本文主要介紹了Mybatis-plus(MP)中CRUD操作保姆級(jí)筆記,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
詳解HTTP請(qǐng)求與響應(yīng)基礎(chǔ)及實(shí)例
這篇文章主要介紹了詳解HTTP請(qǐng)求與響應(yīng)基礎(chǔ)及實(shí)例的相關(guān)資料,這里對(duì)http的請(qǐng)求和響應(yīng)進(jìn)行詳細(xì)分析并附有實(shí)現(xiàn)實(shí)例,需要的朋友可以參考下2017-07-07

