pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.4.RELEASE
com.mybatis
mybatis
0.0.1-SNAPSHOT
mybatis
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-validation
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-maven-plugin
按照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的实体目录
spring:
port: 8080
# 应用名称
application:
name: mybatis
#MySql
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映射
mybatis:
mapper-locations: classpath:mappers/*.xml #指定Mybatis的Mapper文件
type-aliases-package: com.example.mybatis.pojo #指定Mybatis的实体目录
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 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 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 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 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
SELECT * FROM book