Spring Boot MyBatis连接MySQL数据库

SQL部分

CREATE TABLE test(
  id int(10) primary key,
  name varchar(50) not null,
  age int(10),
  address varchar(50)
);

insert into test values(1,'zs',20,'bj');
insert into test values(2,'ls',20,'sh');
insert into test values(3,'ww',20,'bj');

1.application.yml

server:
  port: 8080 # 端口
  servlet:
    context-path: /test # 项目路径

# mysql连接
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

# mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.entity
  configuration:
    map-underscore-to-camel-case: true

2.pom.xml

        <!-- Spring Boot web依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- MySQL连接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>

DemoApplication(这里新增了一个MapperScan注解)

package com.example.demo;

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

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

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

}

TestController

package com.example.demo.controller;

import com.example.demo.entity.Test;
import com.example.demo.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TestController {
    @Resource
    private TestService testService;

    @RequestMapping("/findAll")
    public List<Test> findAll(){
        return testService.findAll();
    }
}

TestService

package com.example.demo.service;

import com.example.demo.entity.Test;
import com.example.demo.mapper.TestMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TestService {
    @Resource
    private TestMapper testMapper;

    public List<Test> findAll(){
        return testMapper.findAll();
    }
}

TestMapper

package com.example.demo.mapper;

import com.example.demo.entity.Test;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface TestMapper {

    List<Test> findAll();
}

Test类

package com.example.demo.entity;

public class Test {
    private Integer id;
    private String name;
    private Integer age;
    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

TestMapper.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.example.demo.mapper.TestMapper">

    <select id="findAll" resultType="Test">
        SELECT id,name,age,address FROM test
    </select>

</mapper>

浏览器访问地址 localhost:8080/test/findAll

 

项目目录

 

 

Spring Boot连接MySQL数据库,JdbcTemplate方式 - 大飞90 - 博客园 (cnblogs.com)

 

posted @ 2022-02-04 19:03  大飞90  阅读(513)  评论(0编辑  收藏  举报