Spring Boot + MySQL + Mybatis + Maven + IDEA 组合

IDEA + Spring Boot + Maven + MySQL + Mybatis 组合

1. 创建 Spring Boot

Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第1张图片
Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第2张图片
Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第3张图片
Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第4张图片

2. Hello,Word 完成

Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第5张图片
引入Mybatis

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.4version>
        dependency>

Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第6张图片

加入MySQL地址

server.port= 8008

spring.datasource.url=jdbc:mysql://***.***.*.**:3306/*****?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=*****

Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第7张图片

    @RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getHello() {
        return "Hello,Word!!!";
    }

Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第8张图片
Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第9张图片

3. Mybatis 、MySQL 数据输出到页面展示

实体类

package com.example.demo.model;

public class User {
    private Integer id;
    private String company;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
}

Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第10张图片
DAO类

package com.example.demo.dao;

import com.example.demo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserDao {
    // MyBatis 的注解
    @Select("select id, company from suppliers")
    public List getUserList();
}

Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第11张图片
新增User输出数据验证

package com.example.demo.controller;

import com.example.demo.dao.UserDao;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getHello() {
        return "Hello,Word!!!";
    }

    @RequestMapping(value = "/user", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public List getUser() {
        return userDao.getUserList();
    }

}

Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第12张图片
Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第13张图片
自此组合完毕,如果途中遇到报错,记得 install 、clean 、或刷新下。可能是没有下载导致的报错
Spring Boot + MySQL + Mybatis + Maven + IDEA 组合_第14张图片
祝愿各位,技术成长的道路上,不断摸索,不断创新,不断成长、
活着就是为了改变世界

你可能感兴趣的:(JAVA)