SpringBoot示例教程MySQL与Mybatis基础用法

Posted hans_gis

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot示例教程MySQL与Mybatis基础用法相关的知识,希望对你有一定的参考价值。

示例需求

在Springboot2框架中,使用mysql和Mybatis功能:
1. Mysql+Datasource集成
2. Mybatis+XML用法详解

数据库准备

采用了Oracle中的scott用户下的四张表,迁移至MySQL下,详见MySQL中建立Oracle中SCOTT数据示例

创建示例工程

本工程代码采用idea进行构建

1、新建工程

技术图片

2、 创建工程时,直接勾选所需依赖

技术图片

3、工程创建完毕后,删除无用文件

技术图片

4、手动添加额外依赖,配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lock</groupId>
    <artifactId>smartmis-scott</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>smartmis-scott</name>
    <description>SmartMIS for Scott</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.42</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

注意:手动修改一下mysql驱动的版本,默认版本比较高,应用时可能会有错误;我这里手动指定为5.1.46版本。

5、修改原application.properties为application.yml,并做如下配置

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/scott?useUnicode=true&autoReconnect=true&allowMultiQueries=true&useSSL=false
    username: root
    password: ****
server:
  port: 8686
mybatis:
  config-location: classpath:mybatis-config.xml

6、resources目录下新增mybatis-config.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>
    <typeAliases>
        <package name="com.lock"/>
    </typeAliases>
    <mappers>
        <mapper resource="mybatis/mapper/DeptMapper.xml"/>
        <mapper resource="mybatis/mapper/EmpMapper.xml"/>
        <mapper resource="mybatis/mapper/SalgradeMapper.xml"/>
        <mapper resource="mybatis/mapper/BonusMapper.xml"/>
    </mappers>
</configuration>

对应DeptMapper.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.lock.smartmis.business.vo.DeptVo">

    <select id="getAllDepts" resultType="com.lock.smartmis.business.vo.DeptVo">
        select * from dept
    </select>

    <select id="getDeptByNo" resultType="com.lock.smartmis.business.vo.DeptVo">
        select * from dept where deptno = #deptNo
    </select>

</mapper>

7、建立bo类(以Dept类为例),并以此为基础建立vo类

package com.lock.smartmis.business.bo;

import java.io.Serializable;

public class Dept implements Serializable 

    private static final long serialVersionUID = 1L;

    private long deptNo;

    private String dName;

    private String loc;

    public long getDeptNo() 
        return deptNo;
    

    public void setDeptNo(long deptNo) 
        this.deptNo = deptNo;
    

    public String getdName() 
        return dName;
    

    public void setdName(String dName) 
        this.dName = dName;
    

    public String getLoc() 
        return loc;
    

    public void setLoc(String loc) 
        this.loc = loc;
    

    @Override
    public String toString()
        return getDeptNo() + "," + getdName() + "," + getLoc();
    
package com.lock.smartmis.business.vo;

import com.lock.smartmis.business.bo.Dept;

public class DeptVo extends Dept

    private static final long serialVersionUID = 1L;
    

8、Dao层接口及其实现

package com.lock.smartmis.business.dao;

import com.lock.smartmis.business.vo.DeptVo;

import java.util.List;

public interface DeptDao 

    public List<DeptVo> getAllDepts();

    public DeptVo getDeptByNo(Long id);
package com.lock.smartmis.business.dao.impl;

import com.lock.smartmis.business.dao.DeptDao;
import com.lock.smartmis.business.vo.DeptVo;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DeptDaoimpl implements DeptDao 

    @Autowired
    public SqlSessionTemplate sqlSessionTemplate;

    @Override
    public List<DeptVo> getAllDepts() 
        return this.sqlSessionTemplate.selectList("getAllDepts");
    

    @Override
    public DeptVo getDeptByNo(Long deptNo) 
        return this.sqlSessionTemplate.selectOne("getDeptByNo", deptNo);
    

9、Controller层

package com.lock.smartmis.business.controller;

import com.lock.smartmis.business.service.DeptService;
import com.lock.smartmis.business.util.JsonResult;
import com.lock.smartmis.business.util.ResultCode;
import com.lock.smartmis.business.vo.DeptVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/restapi")
public class ScottController 

    protected static Logger logger = LoggerFactory.getLogger(ScottController.class);

    @Autowired
    private DeptService deptService;

    @GetMapping("/test")
    public String test() 
        return "API接口测试";
    

    @GetMapping("/depts")
    public JsonResult getAllDepts()
        List<DeptVo> list = this.deptService.getAllDepts();
        return new JsonResult(ResultCode.SUCCESS, list);
    

    @GetMapping("/dept/deptNo")
    public JsonResult getDeptByNo(@PathVariable("deptNo") Long deptNo)
        DeptVo deptVo = this.deptService.getDeptByNo(deptNo);
        return new JsonResult(ResultCode.SUCCESS, deptVo);
    

10、项目运行起来,测试一下接口

技术图片

~

以上是关于SpringBoot示例教程MySQL与Mybatis基础用法的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot+MyBatis+Mysql 详细示例

SpringBoot项目连接MySQL数据库

(转)SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql

macOS下MySQL 8.0 安装与配置教程

#yyds干货盘点#动力节点王鹤Springboot教程笔记ORM操作MySQL

springboot整合内置嵌入式mysql服务教程