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 https://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.3.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
按照pom.xml包名创建
- [src]
- [main]
- [java]
- [com.mybatis]
- [controller]
- [dao]
- [pojo]
- [service]
- [impl]
- [IndexService.java]
- [MybatisApplication.java]
- [resource]
- [mappers]
- [application.yml]
- [pom.xml]
application.yml
- 修改mappers文件目录
- 修改Mybatis的实体目录
server:
port: 8080
spring:
application:
name: mybatis
datasource:
name: localhost_mysql
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
mybatis:
mapper-locations: classpath:mappers/*.xml
type-aliases-package: com.example.mybatis.pojo
IndexController.java
package com.example.mybatis.controller;
import com.example.mybatis.pojo.Book;
import com.example.mybatis.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/index")
public class IndexController {
@Autowired
private IndexService indexService;
@RequestMapping("/bookAll")
public List<Book> bookAll(Book book) {
return indexService.bookAll(book);
}
}
IndexService.java
package com.example.mybatis.service;
import com.example.mybatis.pojo.Book;
import java.util.List;
public interface IndexService {
List<Book> bookAll(Book book);
}
IndexServiceImpl.java
package com.example.mybatis.service.impl;
import com.example.mybatis.dao.IndexDao;
import com.example.mybatis.pojo.Book;
import com.example.mybatis.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class IndexServiceImpl implements IndexService {
@Autowired
private IndexDao indexDao;
public List<Book> bookAll(Book book) {
return indexDao.bookAll(book);
}
}
IndexDao.java
package com.example.mybatis.dao;
import com.example.mybatis.pojo.Book;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface IndexDao {
List<Book> bookAll(Book book);
}
Book.java
package com.example.mybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
public Integer id;
public String name;
}
MybatisApplication.java
package com.example.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.example.mybatis.dao")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
IndexMapper.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.mybatis.dao.IndexDao">
<select id="bookAll" parameterType="Book" resultType="Book">
SELECT * FROM book
</select>
</mapper>