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

Spring?Boot?利用?XML?方式整合?MyBatis

 更新時間:2022年05月23日 11:38:10   作者:??村雨遙????  
這篇文章主要介紹了Spring?Boot?利用?XML?方式整合?MyBatis,文章圍繞主題的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,組要的小伙伴可以參考一下

一、前言

上一篇文章中我們已經(jīng)Spring Boot 利用注解方式整合 MyBatis,今天我們就來看看,如何利用 XML 文件的方式來將兩者整合起來!

下圖是整個整合過程,接下來開始整合:

二、整合過程

最終項目結(jié)構(gòu)如下圖所示:

新建 Spring Boot 項目

新建一個 Spring Boot 項目,添加 Web 組件,具體過程可以參照我的另一篇博客 Spring Boot 教程之創(chuàng)建項目的三種方式

添加 pom 依賴

由于要整合 MyBatis,所以我們需要在項目的配置文件pom.xml中添加 MySQL 驅(qū)動和 SpringBoot MyBatis 整合包;

<!--     springboot mybatis 整合包   -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>
<!--    mysql 驅(qū)動    -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

準(zhǔn)備數(shù)據(jù)庫

  • 數(shù)據(jù)庫創(chuàng)建及輸入插入

準(zhǔn)備一張 user 表,有 id、name、age 三個屬性,其中 id 為主鍵且自增,然后插入三條數(shù)據(jù);

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `name` varchar(50) DEFAULT NULL COMMENT '姓名',
  `age` int(11) DEFAULT NULL COMMENT '年齡',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO  user values (1,"村雨遙",25);
INSERT INTO  user values (2,"張三",26);
INSERT INTO  user values (3,"李四",27);
  • 數(shù)據(jù)源配置

在項目配置文件 application.properties 中配置數(shù)據(jù)源;

# 數(shù)據(jù)庫配置
spring.datasource.username=root
spring.datasource.password=1112233
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

pojo 層

根據(jù)數(shù)據(jù)庫創(chuàng)建實體類,為了精簡代碼,后面過程中都或多或少用了 Lombok 插件,所以需要事先在 pom.xml 引入;

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
package com.cunyu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
 * @author : cunyu
 * @version : 1.0
 * @className : User
 * @date : 2020/7/26 20:44
 * @description : User 實體類
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
}

dao 層

  • 接口編寫

實體類創(chuàng)建完成后,編寫實體類對應(yīng)接口;

package com.cunyu.dao;
import com.cunyu.pojo.User;
import org.apache.ibatis.annotations.Mapper;
/**
 * @InterfaceName : UserDao
 * @Author : cunyu
 * @Version : 1.0
 * @Description : User 類對應(yīng)接口
 **/

@Mapper
public interface UserDao {
    /**
     * @param id 用戶 id
     * @return 對應(yīng) id 的用戶
     * @description 根據(jù)用戶 id 查詢用戶
     * @author cunyu1943
     * @version 1.0
     */
    User getUserById(Long id);
}
  • 配置 MyBatis

在項目配置文件 application.properties 中添加 MyBatis 配置;

# MyBatis 配置
mybatis.type-aliases-package=com.cunyu.pojo.User
mybatis.mapper-locations=classpath:mapper/*.xml
  • mapper 編寫

在 src/main/resources/mapper 下新建 UserDao.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">
<mapper namespace="com.cunyu.dao.UserDao">
    <select id="getUserById" resultType="com.cunyu.pojo.User">
        SELECT id, name, age
        FROM user
        WHERE id = #{id}
    </select>
</mapper>

service 層

  • service 接口
package com.cunyu.service;
import com.cunyu.pojo.User;
/**
 * @author : cunyu
 * @version : 1.0
 * @className : UserService
 * @description : User service 接口
 */
public interface UserService {
    /**
     * @param id 用戶 iD
     * @return 對應(yīng) id 的用戶
     * @description 根據(jù) id 查找用戶
     * @date 2020/7/26 20:58
     * @author cunyu1943
     * @version 1.0
     */
    User getUserById(Long id);
}
  • service 接口實現(xiàn)類
package com.cunyu.service.impl;
import com.cunyu.dao.UserDao;
import com.cunyu.pojo.User;
import com.cunyu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author : cunyu
 * @version : 1.0
 * @className : UserServiceImpl
 * @description : service 接口實現(xiàn)類
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User getUserById(Long id) {
        return userDao.getUserById(id);
    }
}

controller 層

package com.cunyu.controller;
import com.cunyu.pojo.User;
import com.cunyu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author : cunyu
 * @version : 1.0
 * @className : UserController
 * @description : User controller
 */
@RestController
public class UserController {
    /**
     * 自動注入
     */
    @Autowired
    private UserService userService;
    @GetMapping("/user")
    public User getUserById() {
        User user = userService.getUserById(1L);
        return user;
    }
}

入口程序配置

在入口程序中配置 mapper 自動掃描;

package com.cunyu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan(basePackages = "com.cunyu.dao")
@SpringBootApplication
public class MybatisXmlApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisXmlApplication.class, args);
    }
}

網(wǎng)頁測試

完成上述所有步驟之后,在瀏覽器中訪問 http://localhost:8080/user,就可以在網(wǎng)頁中顯示對應(yīng) id 的 User 對象的所有信息;

總結(jié)

通過 XMl 文件來整合 Spring Boot 和 MyBatis 的具體過程了,是不是很簡單呢?對比 XML 文件和注解的方式,最大的不同就在于 DAO 層。前者是通過 XML 配置文件的方式,而后者則是使用 MyBatis 中所提供的注解來實現(xiàn)。兩種方式各有優(yōu)劣,而且大家也都有使用,不過貌似大家使用的更多的還是 XML 配置的方式。

到此這篇關(guān)于Spring Boot 利用 XML 方式整合 MyBatis的文章就介紹到這了,更多相關(guān)Spring Boot 整合 MyBatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Scala生成隨機數(shù)的方法示例

    使用Scala生成隨機數(shù)的方法示例

    這篇文章主要介紹了使用Scala生成隨機數(shù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Java?for循環(huán)倒序輸出的操作代碼

    Java?for循環(huán)倒序輸出的操作代碼

    在Java中,要實現(xiàn)一個for循環(huán)的倒序輸出,通常我們會使用數(shù)組或集合(如ArrayList)作為數(shù)據(jù)源,然后通過倒序遍歷這個數(shù)組或集合來實現(xiàn),這篇文章主要介紹了Java?for循環(huán)倒序輸出,需要的朋友可以參考下
    2024-07-07
  • SpringBoot利用jackson格式化時間的三種方法

    SpringBoot利用jackson格式化時間的三種方法

    日常開發(fā)過程中經(jīng)常會使用json進(jìn)行數(shù)據(jù)的傳輸,這就涉及到了對象和json的相互轉(zhuǎn)化,常用的解決方案有:Jackson(推薦)、谷歌的Gson、阿里的Fastjson,這篇文章主要給大家介紹了關(guān)于SpringBoot如何利用jackson格式化時間的相關(guān)資料,需要的朋友可以參考下
    2021-06-06
  • Springboot2 配置AOP日志的方法步驟

    Springboot2 配置AOP日志的方法步驟

    這篇文章主要介紹了Springboot2 配置AOP日志的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java序列化的原理分析及解決

    Java序列化的原理分析及解決

    文章介紹了Java中的序列化機制,包括Serializable和Externalizable接口的區(qū)別,以及serialVersionUID的作用,Serializable接口可以序列化的所有子類型本身都是可序列化的,如果要序列化的類有父類,父類也應(yīng)該實現(xiàn)Serializable接口
    2024-11-11
  • java正則表達(dá)式驗證郵箱、電話號碼示例

    java正則表達(dá)式驗證郵箱、電話號碼示例

    這篇文章主要介紹了java正則表達(dá)式驗證郵箱、電話號碼示例,需要的朋友可以參考下
    2014-03-03
  • Spring MVC文件配置以及參數(shù)傳遞示例詳解

    Spring MVC文件配置以及參數(shù)傳遞示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring MVC文件配置以及參數(shù)傳遞的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • SpringCloud微服務(wù)之Hystrix組件實現(xiàn)服務(wù)熔斷的方法

    SpringCloud微服務(wù)之Hystrix組件實現(xiàn)服務(wù)熔斷的方法

    微服務(wù)架構(gòu)特點就是多服務(wù),多數(shù)據(jù)源,支撐系統(tǒng)應(yīng)用。這樣導(dǎo)致微服務(wù)之間存在依賴關(guān)系。這篇文章主要介紹了SpringCloud微服務(wù)之Hystrix組件實現(xiàn)服務(wù)熔斷的方法,需要的朋友可以參考下
    2019-08-08
  • Java中this關(guān)鍵字的用法詳解

    Java中this關(guān)鍵字的用法詳解

    我知道很多朋友都和我一樣,在JAVA程序中似乎經(jīng)常見到this,自己也偶爾用到它,但是到底this該怎么用,卻心中無數(shù),下面這篇文章主要給大家介紹了關(guān)于Java中this關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Spring中BeanFactory和ApplicationContext的作用和區(qū)別(推薦)

    Spring中BeanFactory和ApplicationContext的作用和區(qū)別(推薦)

    這篇文章主要介紹了Spring中BeanFactory和ApplicationContext的作用和區(qū)別,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09

最新評論

团风县| 上高县| 新巴尔虎左旗| 余干县| 闸北区| 孟津县| 察哈| 万盛区| 思茅市| 桓仁| 舒兰市| 永丰县| 平舆县| 古蔺县| 汶川县| 桃江县| 商丘市| 莱芜市| 五常市| 体育| 子长县| 宣威市| 邻水| 峨眉山市| 德庆县| 剑河县| 东安县| 泌阳县| 屏东市| 勐海县| 沅江市| 新营市| 治县。| 太原市| 德阳市| 和顺县| 楚雄市| 陈巴尔虎旗| 芜湖县| 昭苏县| 含山县|