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.dao.DeptDao">

    <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.bean;

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.bean.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、Service层

package com.lock.smartmis.business.service;

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

import java.util.List;

public interface DeptService {

    public List<DeptVo> getAllDepts();

    public DeptVo getDeptByNo(Long deptNo);
}
package com.lock.smartmis.business.service.impl;

import com.lock.smartmis.business.dao.DeptDao;
import com.lock.smartmis.business.service.DeptService;
import com.lock.smartmis.business.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Override
    public List<DeptVo> getAllDepts() {
        return this.deptDao.getAllDepts();
    }

    @Override
    public DeptVo getDeptByNo(Long deptNo) {
        return this.deptDao.getDeptByNo(deptNo);
    }
}

10、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、项目运行起来,测试一下接口

~

posted @ 2019-07-03 16:20  百折不回  阅读(586)  评论(0编辑  收藏  举报