欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot MyBatis簡單快速入門例子

 更新時間:2021年07月22日 11:41:07   作者:程序之大道至簡  
MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。這篇文章主要介紹了SpringBoot MyBatis快速入門-簡單例子,需要的朋友可以參考下

一、MyBatis簡介

MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 對象)為數(shù)據(jù)庫中的記錄。

二、MyBatis使用步驟

 1、MyBatis工程總體目錄結(jié)構(gòu)

在這里插入圖片描述

2、創(chuàng)建簡單的SpringBoot工程

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

3、添加MyBatis依賴

  <!--MyBatis-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

在這里插入圖片描述

4、在數(shù)據(jù)庫創(chuàng)建USER表

在這里插入圖片描述

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL  DEFAULT '' COMMENT '用戶名',
  `password` varchar(50) NOT NULL DEFAULT '' COMMENT '密碼',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;

5、在application.properties配置數(shù)據(jù)庫連接信息

#數(shù)據(jù)庫相關(guān)配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useAffectedRows=true
spring.datasource.username=root
spring.datasource.password=QQ796413

#mybaits配置
#mapper加載路徑
mybatis.mapper-locations= classpath:mapper/*.xml
#實體包位置
mybatis.type-aliases-package= com.example.mybatisdemo.entity
#myatbis配置文件
mybatis.config-location= classpath:mybatis-config.xml

6、創(chuàng)建USER表對應(yīng)的實體類

在這里插入圖片描述

package com.example.mybatisdemo.entity;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

7、在mapper/UserMapper創(chuàng)建UserMapper.java

在這里插入圖片描述

package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper{

     User findUserById(Integer id);
}

8、在service/UserService新建UserService.java

在這里插入圖片描述

package com.example.mybatisdemo.service;

import com.example.mybatisdemo.entity.User;

public interface UserService {
    User findUserById(Integer id);
}

9、在service/impl/UserServiceImpl 創(chuàng)建UserServiceImpl.java

在這里插入圖片描述

package com.example.mybatisdemo.service.impl;

import com.example.mybatisdemo.entity.User;
import com.example.mybatisdemo.mapper.UserMapper;
import com.example.mybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
        @Autowired
        private UserMapper userMapper;

        @Override
        public User findUserById(Integer id) {
            return userMapper.findUserById(id);
        }
}

10、在resources下新建mybatis-conf.xml

在這里插入圖片描述

<?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="STDOUT_LOGGING"/>
        <!--開啟駝峰命名法-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--開啟全局延遲加載-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 集合為空時強(qiáng)制返回空集合實例而不是null -->
        <setting name="returnInstanceForEmptyRow" value="true"/>
        <!-- 結(jié)果集中value為空時保留key -->
        <setting name="callSettersOnNulls" value="true"/>
    </settings>
</configuration>

11、在resources下mapper文件下創(chuàng)建UserMapper.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">
<!--注意:1.這里的namespace要是你usermapper的位置-->
<mapper namespace="com.example.mybatisdemo.mapper.UserMapper">
    <!--注意這里的返回類型-->
    <resultMap id="BaseResultMap" type="com.example.mybatisdemo.entity.User">
        <result column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
    </resultMap>

    <!--2.id和你的方法名一樣,resultMap為上面的id名一致-->
    <select id="findUserById" resultMap="BaseResultMap">
        select
             id,
             username,
             password
        from
             user
        where
             id= #{id,jdbcType=INTEGER}
    </select>
</mapper>

12、創(chuàng)建UserController.java

在這里插入圖片描述

package com.example.mybatisdemo.controller;

import com.example.mybatisdemo.entity.User;
import com.example.mybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/findUserById")
    public User findUserById(@RequestParam Integer id){
       return userService.findUserById(1);
    }
}

13、測試

在這里插入圖片描述

工程可以去我的資源下載

到此這篇關(guān)于SpringBoot MyBatis快速入門-簡單例子的文章就介紹到這了,更多相關(guān)SpringBoot MyBatis入門內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java如何寫接口給別人調(diào)用的示例代碼

    java如何寫接口給別人調(diào)用的示例代碼

    這篇文章主要介紹了java如何寫接口給別人調(diào)用的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 徹底了解java中ReentrantLock和AQS的源碼

    徹底了解java中ReentrantLock和AQS的源碼

    這篇文章主要介紹了徹底了解java中ReentrantLock和AQS的源碼,想了解鎖機(jī)制的同學(xué),一定要參考下
    2021-04-04
  • Spring Boot實現(xiàn)熱部署的實例方法

    Spring Boot實現(xiàn)熱部署的實例方法

    在本篇文章里小編給大家整理的是關(guān)于Spring Boot實現(xiàn)熱部署的實例方法和實例,需要的朋友們可以參考下。
    2020-02-02
  • Java設(shè)計模式之享元模式示例詳解

    Java設(shè)計模式之享元模式示例詳解

    享元模式(FlyWeight?Pattern),也叫蠅量模式,運用共享技術(shù),有效的支持大量細(xì)粒度的對象,享元模式就是池技術(shù)的重要實現(xiàn)方式。本文將通過示例詳細(xì)講解享元模式,感興趣的可以了解一下
    2022-03-03
  • SpringBoot圖文并茂帶你掌握devtools熱啟動

    SpringBoot圖文并茂帶你掌握devtools熱啟動

    這篇文章主要介紹springBoot插件工具熱部署Devtools,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作

    SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作

    這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Nacos+Spring Cloud Gateway動態(tài)路由配置實現(xiàn)步驟

    Nacos+Spring Cloud Gateway動態(tài)路由配置實現(xiàn)步驟

    Nacos最近項目一直在使用,本文通過gateway、nacos-consumer、nacos-provider三個簡單模塊來展示:Nacos下動態(tài)路由配置,,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java調(diào)用Shell命令的方法

    Java調(diào)用Shell命令的方法

    這篇文章主要介紹了Java調(diào)用Shell命令的方法,實例分析了java調(diào)用shell命令的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • why在重寫equals時還必須重寫hashcode方法分享

    why在重寫equals時還必須重寫hashcode方法分享

    首先我們先來看下String類的源碼:可以發(fā)現(xiàn)String是重寫了Object類的equals方法的,并且也重寫了hashcode方法
    2013-10-10
  • Java靜態(tài)方法不具有多態(tài)性詳解

    Java靜態(tài)方法不具有多態(tài)性詳解

    下面小編就為大家?guī)硪黄狫ava靜態(tài)方法不具有多態(tài)性詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06

最新評論