最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文吃透Spring集成MyBatis

 更新時(shí)間:2023年05月26日 10:25:03   作者:幾分醉意.  
spring能集成很多的框架,是spring一個(gè)優(yōu)勢(shì)功能,通過(guò)集成功能,讓開(kāi)發(fā)人員使用其他框架更方便,本文將給大家詳細(xì)介紹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.加入依賴gav
  spring依賴, mybatis依賴, mysql驅(qū)動(dòng)。 junit依賴
  mybatis-spring依賴(mybatis網(wǎng)站上提供的,用來(lái)在spring項(xiàng)目中,創(chuàng)建mybatis對(duì)象)
  spring有關(guān)事務(wù)的依賴。

  mybatis和spring整合的時(shí)候, 事務(wù)是自動(dòng)提交的。

4.創(chuàng)建實(shí)體類Student

5.創(chuàng)建Dao接口和mapper文件寫sql語(yǔ)句

6.寫mybatis主配置文件

7.創(chuàng)建service接口和他的實(shí)現(xiàn)類

8.創(chuàng)建spring的配置文件
  1)聲明數(shù)據(jù)源DataSource,使用的阿里的Druid連接池
  2) 聲明SqlSessionFactoryBean類,在這個(gè)類內(nèi)部創(chuàng)建的是SqlSessionFactory對(duì)象。
  3)聲明MapperScannerConfiguration類,在內(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加入依賴

<dependencies>
    <!--測(cè)試-->
    <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ū)動(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í)體類,生成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ù)源了,下面會(huì)介紹在哪里指定?;旧螹ybatis主配置文件里面就設(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è)路徑是從類路徑根目錄開(kāi)始
        使用注意:mapper文件的路徑使用 / 分割路徑
                 一個(gè)mapper resource 指定一個(gè)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.實(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è)類的內(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(接口名的首字母小寫, 對(duì)象)  //創(chuàng)建dao代理,這個(gè)dao代理的對(duì)象的名字是這個(gè)接口的首字母小寫。也就是bean的id
        }
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定SqlSessionFactory對(duì)象的名稱-->
        <property name="sqlSessionFactoryBeanName" value="factory" />
        <!--指定基本包,dao接口所在的包名-->
        <property name="basePackage" value="youfei1_v.Dao" /><!--創(chuàng)建dao代理,這個(gè)dao代理的對(duì)象的名字是這個(gè)接口名的首字母小寫。也就是bean的id-->
    </bean>
    <!--聲明service-->
    <bean id="studentService" class="youfei1_v.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao" /> <!--set注入:把dao代理賦值給StudentServiceImpl類中的StudentDao對(duì)象-->
    </bean>                         <!--ref用的就是上面的創(chuàng)建出來(lái)的dao代理,的id值,這個(gè)id值就是接口名的首字母小寫-->
    <!--ref="studentDao" :studentDao指的是Student這個(gè)接口的代理。上面那個(gè)bean,創(chuàng)建出Student接口的代理,這個(gè)代理的id就是接口名字小寫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)文章

  • Java通過(guò)調(diào)用FFMPEG獲取視頻時(shí)長(zhǎng)

    Java通過(guò)調(diào)用FFMPEG獲取視頻時(shí)長(zhǎng)

    這篇文章主要為大家詳細(xì)介紹了Java通過(guò)調(diào)用FFMPEG獲取視頻時(shí)長(zhǎng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Java單例模式簡(jiǎn)單示例

    Java單例模式簡(jiǎn)單示例

    這篇文章主要介紹了Java單例模式,結(jié)合實(shí)例形式簡(jiǎn)單分析了java單例模式的定義與使用技巧,需要的朋友可以參考下
    2017-06-06
  • Windows安裝Apache?Kafka保姆級(jí)詳細(xì)教程(圖文+可視化管理工具)

    Windows安裝Apache?Kafka保姆級(jí)詳細(xì)教程(圖文+可視化管理工具)

    本文詳細(xì)介紹了在Windows系統(tǒng)上安裝和配置Apache?Kafka的步驟,適合初學(xué)者,從環(huán)境準(zhǔn)備到啟動(dòng)服務(wù),再到測(cè)試功能和設(shè)置為Windows服務(wù),每個(gè)步驟都提供了詳細(xì)的圖文教程,最后,還介紹了如何使用KafkaTool進(jìn)行可視化管理,感興趣的朋友跟隨小編一起看看吧
    2025-11-11
  • spring cloud gateway轉(zhuǎn)發(fā)服務(wù)報(bào)錯(cuò)的解決

    spring cloud gateway轉(zhuǎn)發(fā)服務(wù)報(bào)錯(cuò)的解決

    這篇文章主要介紹了spring cloud gateway轉(zhuǎn)發(fā)服務(wù)報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名

    java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名

    這篇文章主要給大家介紹了關(guān)于java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名的相關(guān)資料,本文僅為日常操作記錄,方便后期使用查找本地電腦文件太多了,又不想一個(gè)一個(gè)重命名,改名字什么的很麻煩,需要的朋友可以參考下
    2024-02-02
  • SpringMVC教程之json交互使用詳解

    SpringMVC教程之json交互使用詳解

    本篇文章主要介紹了SpringMVC教程之json使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • SpringMVC和rabbitmq集成的使用案例

    SpringMVC和rabbitmq集成的使用案例

    這篇文章主要介紹了SpringMVC和rabbitmq集成的使用案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • JAVA中調(diào)用C語(yǔ)言函數(shù)的實(shí)現(xiàn)方式

    JAVA中調(diào)用C語(yǔ)言函數(shù)的實(shí)現(xiàn)方式

    這篇文章主要介紹了JAVA中調(diào)用C語(yǔ)言函數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Spring事件監(jiān)聽(tīng)機(jī)制ApplicationEvent方式

    Spring事件監(jiān)聽(tīng)機(jī)制ApplicationEvent方式

    這篇文章主要介紹了Spring事件監(jiān)聽(tīng)機(jī)制ApplicationEvent方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Nacos配置中心設(shè)計(jì)原理分析

    Nacos配置中心設(shè)計(jì)原理分析

    今天分享一下Nacos配置變更的相關(guān)知識(shí)點(diǎn),現(xiàn)在使用Java生態(tài)如果使用微服務(wù),如果部署在K8s上,那么可能會(huì)使用ConfigMap來(lái)存儲(chǔ)配置文件,如果沒(méi)有使用K8s,那么基本上都使用Nacos來(lái)做配置中心,所以有必要了解一下Nacos的配置的知識(shí)點(diǎn),本文只是對(duì)其中的部分實(shí)現(xiàn)原理進(jìn)行分析
    2023-10-10

最新評(píng)論

呼玛县| 宜川县| 开原市| 湟源县| 民权县| 全州县| 乃东县| 华亭县| 博湖县| 甘谷县| 和田县| 隆德县| 凤翔县| 印江| 侯马市| 浑源县| 宁都县| 连云港市| 陆丰市| 玉林市| 东莞市| 汨罗市| 新余市| 沽源县| 弥渡县| 彭州市| 湘阴县| 顺平县| 岳普湖县| 商都县| 隆化县| 泸定县| 祁门县| 泾川县| 富阳市| 米脂县| 黄梅县| 南和县| 六盘水市| 调兵山市| 察雅县|