码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-07-03 17:07:19      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:template   alibaba   port   import   lan   false   注意   创建   http   

示例需求

在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基础用法

标签:template   alibaba   port   import   lan   false   注意   创建   http   

原文地址:https://www.cnblogs.com/hans_gis/p/11127180.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!