string(21) "{"docs":[],"count":0}" array(2) { ["docs"]=> array(0) { } ["count"]=> int(0) } Spring Boot+Mybatis+MVC的简单Demo - 爱码网

      一个Spring Boot+Mybatis+MVC的简单Demo,没搞复杂的东西。
Spring Boot+Mybatis+MVC的简单Demo

1、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>

    <groupId>com.boot.demo</groupId>
    <artifactId>bootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bootdemo</name>
    <description>Demo project for Spring Boot</description>

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

</project>

2、application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/testdb?characterEncoding=utf8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  typeAliasesPackage: com.boot.demo.bootdemo.domain

server:
  port: 8081

3、Java Bean

package com.boot.demo.bootdemo.domain;

import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
    private String createDate;
}

4、UserMapper

package com.boot.demo.bootdemo.mapper;

import com.boot.demo.bootdemo.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from user")
    List<User> selectAll();
}

5、UserService

package com.boot.demo.bootdemo.service;

import com.boot.demo.bootdemo.domain.User;
import com.boot.demo.bootdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService{
    @Autowired
    private UserMapper userMapper;

    public List<User> listAll() {
        return userMapper.selectAll();
    }
}

6、UserController

package com.boot.demo.bootdemo.controller;

import com.boot.demo.bootdemo.domain.User;
import com.boot.demo.bootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/listAll")
    public List<User> listAll() {
        return userService.listAll();
    }
}

7、BootdemoApplication

package com.boot.demo.bootdemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.boot.demo.bootdemo.mapper")
public class BootdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootdemoApplication.class, args);
    }

}

8、启动Spring Boot项目,http://localhost:8081/user/listAll。
Spring Boot+Mybatis+MVC的简单Demo

相关文章: