RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
SpringBootMyBatis怎么快速入门

这篇文章主要介绍“SpringBoot MyBatis怎么快速入门”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot MyBatis怎么快速入门”文章能帮助大家解决问题。

合肥网站建设公司创新互联,合肥网站设计制作,有大型网站制作公司丰富经验。已为合肥上千多家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的合肥做网站的公司定做!

一、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依赖

  
        
            MySQL
            mysql-connector-java
            5.1.32
        
        
            org.mybatis
            mybatis
            3.4.6
        

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

SpringBoot MyBatis怎么快速入门

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

SpringBoot MyBatis怎么快速入门





    
        
        
        
        
        
        
        
        
        
        
    

11、在resources下mapper文件下创建UserMapper.xml

SpringBoot MyBatis怎么快速入门





    
    
        
        
        
    

    
    
        select
             id,
             username,
             password
        from
             user
        where
             id= #{id,jdbcType=INTEGER}
    

12、创建UserController.java

SpringBoot 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怎么快速入门”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。


分享标题:SpringBootMyBatis怎么快速入门
本文来源:http://scyingshan.cn/article/pdpgch.html