目录
简单记录一下spingboot集成Mybatis注解版过程。
<?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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.michael</groupId>
<artifactId>my-blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-blog</name>
<description>Demo project for Vue</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.michael.myblog.entity;
/**
* @ClassName: Customer
* @Package com.michael.myblog.entity
* @Description: TODO
* @Author Mr.Z
* @Date 2020/7/26 19:56
* @Version 1.0
*/
public class Customer {
private String name;
private String phone;
private String email;
//getter、setter方法....
}
package com.michael.myblog.dao;
import com.michael.myblog.entity.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @ClassName: CustomerMapper
* @Package com.michael.myblog.dao
* @Description: TODO
* @Author Mr.Z
* @Date 2020/7/26 19:59
* @Version 1.0
*/
@Mapper
@Repository
public interface CustomerMapper {
@Select("select * from customer")
List<Customer> getCustomers();
}
@RestController
@RequestMapping("/customer")
public class UserController {
@Autowired
private CustomerMapper customerMapper;
@GetMapping
public List<Customer> getCustomers() {
return customerMapper.getCustomers();
}
}
@SpringBootApplication
@MapperScan("com.michael.myblog.dao") //扫描Mapper类
public class MyBlogApplication {
public static void main(String[] args) {
SpringApplication.run(MyBlogApplication.class, args);
}
}