首頁 >Java >java教程 >SpringBoot MyBatis怎麼快速入門

SpringBoot MyBatis怎麼快速入門

PHPz
PHPz轉載
2023-05-11 14:25:121282瀏覽

一、MyBatis簡介

MyBatis 是一款優秀的持久層框架,它支援自訂 SQL、預存程序以及進階映射。 MyBatis 幾乎免除了幾乎所有的 JDBC 程式碼以及設定參數和取得結果集的工作。 MyBatis 可以透過簡單的 XML 或註解來配置和映射原始類型、介面和 Java POJO(Plain Old Java Objects,普通老式 Java 物件)為資料庫中的記錄。

二、MyBatis使用步驟

 1、MyBatis工程整體目錄結構

SpringBoot MyBatis怎么快速入门

2、建立簡單的SpringBoot工程

SpringBoot MyBatis怎么快速入门
SpringBoot MyBatis怎么快速入门
SpringBoot MyBatis怎么快速入门

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>

SpringBoot MyBatis怎么快速入门

##4、在資料庫建立USER表

SpringBoot MyBatis怎么快速入门

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設定資料庫連線資訊

#数据库相关配置
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、建立USER表對應的實體類別

SpringBoot MyBatis怎么快速入门

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建立UserMapper.java

SpringBoot MyBatis怎么快速入门

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

SpringBoot MyBatis怎么快速入门

package com.example.mybatisdemo.service;

import com.example.mybatisdemo.entity.User;

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

9、在service/impl/UserServiceImpl 建立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);
        }
}
SpringBoot MyBatis怎么快速入门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"/>
        <!-- 集合为空时强制返回空集合实例而不是null -->
        <setting name="returnInstanceForEmptyRow" value="true"/>
        <!-- 结果集中value为空时保留key -->
        <setting name="callSettersOnNulls" value="true"/>
    </settings>
</configuration>
SpringBoot MyBatis怎么快速入门11、在resources下mapper檔案下建立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、創建UserController.javaSpringBoot MyBatis怎么快速入门

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、測試SpringBoot MyBatis怎么快速入门

以上是SpringBoot MyBatis怎麼快速入門的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除