一文吃透Spring集成MyBatis
??集成思路
spring能集成很多的框架,是spring一個優(yōu)勢功能。通過集成功能,讓開發(fā)人員使用其他框架更方便。集成使用的是spring ioc 核心技術(shù)。
?怎么使用MyBatis
使用mybatis,需要創(chuàng)mybatis框架中的某些對象,使用這些對象,就能使用mybatis提供的功能了。
分析: mybatis執(zhí)行sql語句,需要使用那些對象?
1. 需要有Dao接口的代理對象,例如StudentDao接口,需要一個它的代理對象,使用 SqlSession.getMapper(StudentDao.class),得到dao代理對象。
2. 需要有SqlSessionFactory,創(chuàng)建SqlSessionFactory對象,才能使用openSession(得到SqlSession對象。
3. 數(shù)據(jù)源DataSource對象,使用一個更強大,功能更多的連接池對象代替mybatis自己的PooledDataSource。
?集成的步驟
實現(xiàn)步驟:
1.使用的mysql庫, 使用學(xué)生表 student2(id int 主鍵列, 自動增長,
name varchar(80),
age int)2.創(chuàng)建maven項目
3.加入依賴gav
spring依賴, mybatis依賴, mysql驅(qū)動。 junit依賴
mybatis-spring依賴(mybatis網(wǎng)站上提供的,用來在spring項目中,創(chuàng)建mybatis對象)
spring有關(guān)事務(wù)的依賴。mybatis和spring整合的時候, 事務(wù)是自動提交的。
4.創(chuàng)建實體類Student
5.創(chuàng)建Dao接口和mapper文件寫sql語句
6.寫mybatis主配置文件
7.創(chuàng)建service接口和他的實現(xiàn)類
8.創(chuàng)建spring的配置文件
1)聲明數(shù)據(jù)源DataSource,使用的阿里的Druid連接池
2) 聲明SqlSessionFactoryBean類,在這個類內(nèi)部創(chuàng)建的是SqlSessionFactory對象。
3)聲明MapperScannerConfiguration類,在內(nèi)部創(chuàng)建dao代理對象,
創(chuàng)建的對象都放在spring容器中。4)聲明Service對象,把3)的中dao賦值給service屬性
9.測試dao訪問數(shù)據(jù)庫
?pom加入依賴
<dependencies> <!--測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring依賴--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!--spring事務(wù)依賴--> <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依賴--> <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ū)動--> <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就要用到這個插件 編譯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)建實體類,生成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接口,在里面寫方法。
public interface StudentDao { //添加操作 int insertStudent(Student student); //查詢操作 List<Student> selectStudents(); }
3.創(chuàng)建mapper文件,寫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)簽寫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ù)源了,下面會介紹在哪里指定?;旧螹ybatis主配置文件里面就設(shè)置個日志、別名和其它的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語句--> <mappers> <!--使用mapper的resource屬性指定mapper文件的路徑。 這個路徑是從類路徑根目錄開始 使用注意:mapper文件的路徑使用 / 分割路徑 一個mapper resource 指定一個mapper文件 --> <!--<mapper resource="youfei1_v/dao/StudentDao.xml"/>--> <!--name:mapper文件所在的包名 滿足要求才能使用這種方式: 1.mapper文件和dao接口在同一目錄 2.mapper文件和dao接口名稱一致 --> <package name="youfei1_v.Dao"/> </mappers> </configuration>
?創(chuàng)建Service類
1.創(chuàng)建service接口。
public interface StudentService { int addStudent(Student student); List<Student> queryStudent(); }
2.實現(xiàn)service接口。
public class StudentServiceImpl implements StudentService { private StudentDao studentDao; //創(chuàng)建這個dao對象和set方法 public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } @Override public int addStudent(Student student) { int rows = studentDao.insertStudent(student); //方法中調(diào)用studentDao對象的方法 return rows; } @Override public List<Student> queryStudent() { List<Student> students = studentDao.selectStudents(); return students; } }
?創(chuàng)建Spring配置文件和測試集成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,在這個類的內(nèi)部,創(chuàng)建SqlSessionFactory bean的id就代表創(chuàng)建SqlSessionFactory對象--> <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所表示的包,把包中的每個接口都找到,調(diào)用SqlSession.getMapper 把每個dao接口都創(chuàng)建出dao對象 ,dao代理對象放在容器中。 ApplicationContext ctx = .... SqlSessionFactory sqlSessionFactory = ctx.getBean("factory"); SqlSession session = sqlSessionFactory.openSession(); for(接口: com.bjpowernode.dao){ 接口 對象 = session.getMapper(接口) springMap.put(接口名的首字母小寫, 對象) //創(chuàng)建dao代理,這個dao代理的對象的名字是這個接口的首字母小寫。也就是bean的id } --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定SqlSessionFactory對象的名稱--> <property name="sqlSessionFactoryBeanName" value="factory" /> <!--指定基本包,dao接口所在的包名--> <property name="basePackage" value="youfei1_v.Dao" /><!--創(chuàng)建dao代理,這個dao代理的對象的名字是這個接口名的首字母小寫。也就是bean的id--> </bean> <!--聲明service--> <bean id="studentService" class="youfei1_v.service.impl.StudentServiceImpl"> <property name="studentDao" ref="studentDao" /> <!--set注入:把dao代理賦值給StudentServiceImpl類中的StudentDao對象--> </bean> <!--ref用的就是上面的創(chuàng)建出來的dao代理,的id值,這個id值就是接口名的首字母小寫--> <!--ref="studentDao" :studentDao指的是Student這個接口的代理。上面那個bean,創(chuàng)建出Student接口的代理,這個代理的id就是接口名字小寫studentDao--> </beans>
2.測試
?使用外部屬性配置文件
1.創(chuàng)建一個.properties的文件
2.在spring配置文件中引用
以上就是一文吃透Spring集成MyBatis的詳細內(nèi)容,更多關(guān)于Spring集成MyBatis的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Spring?Cloud?Stream處理事件的示例詳解
Spring?Cloud?Stream?是基于?Spring?Boot?的用于構(gòu)建消息驅(qū)動微服務(wù)的框架,本文主要介紹了如何使用?Spring?Cloud?Stream?來處理事件,需要的可以參考一下2023-06-06詳解Mybatis-plus(MP)中CRUD操作保姆級筆記
本文主要介紹了Mybatis-plus(MP)中CRUD操作保姆級筆記,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11