SpringBoot整合Mybatis注解详细版
引言
记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架!
本章说明
本期不采用IntelliJ IDEA自动构建SpringBoot工程,采用spring的eclipse工具SpringToolSuite纯Maven构建,这样对大家学习有很好的帮助。
Maven构建SpringBoot
- 新建一个Maven Project项目
pom.xml引入SpringBoot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
启动第一个SpringBoot工程
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMybatisApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
启动成功
第一个Controller
package com.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@RequestMapping("index")
public String index() {
return "xueqimiao";
}
}
测试
这样我们的springboot就跑通了。
接下来整合Mybatis,引入Mybatis和mysql依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置文件
本文不使用application.properties文件 而使用更加简洁的application.yml文件。到resource文件夹下创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),8081为端口号,默认是8080,我这里是因为8080已经有项目在跑了,这里更改下。
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username: root
password: root
实体层
package com.pojo;
public class User {
private Integer id;//编号
private String username;//用户名
private String password;//密码
private Integer age;//年龄
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
dao层
package com.mapper;
import com.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user")
List<User> getAll();
}
这里直接采用注解方式写sql语句
service层
package com.service;
import com.mapper.UserMapper;
import com.pojo.User;
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> getAll(){
return userMapper.getAll();
}
}
controller层
package com.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pojo.User;
import com.service.UserService;
@RestController
public class IndexController {
@Autowired
private UserService userService;
@RequestMapping("index")
public String index() {
return "xueqimiao";
}
@RequestMapping("getAll")
public List<User> getAll() {
return userService.getAll();
}
}
-
启动项目输入 localhost:8081/getAll 测试
-
这时我们从控制台可以看到没有SQL语句输出,我们可以到yml配置文件中加一段配置com.mapper是包名,层级区别开就行(一下两种方式都行)
logging:
level:
com:
mapper: debug
logging:
level:
com.mapper: debug
最后附上整个工程结构图
说明
到这里我们就整合完毕了,是不是觉得非常简单呢,大家整合时如有问题欢迎大家留言,我会为大家解答,如有不对的地方也希望大家及时指出。下面我为大家说明几个问题:
1.@SpringBootApplication是一个组合注解,功能强大。通常使用@EnableAutoConfiguration @ComponentScan配合使用比较合理。因为@SpringBootApplication会全路径扫描,相对来说效率会慢一点,当然@SpringBootApplication也可以指定包扫描,@SpringBootApplication(scanBasePackages = {“com.controller”,“com.service”})
2.大家看到我在controller层中采用的是@RestController注解,并没有使用@Controller注解,下面的方法上也没有使用@ResponseBody注解就直接返回的是JSON格式,那是因为在spring4.0之后,spring将@Controller和@ResponseBody合成了一个注解,那就是@RestController
3.@RequestMapping在指定请求方式通常这样@RequestMapping(value = “/getById”,method = RequestMethod.GET),这里可以直接使用@GetMapping,@PostMapping,@PostMapping,@DeleteMapping即可
4.我在dao层写了一个@Mapper注解,通常不推荐这么使用,如果dao层过多的话,每次都要写,影响效率,还容易遗漏,这里推荐直接到启动类写@MapperScan(“com.mapper”),也可以写多个包@MapperScan(basePackages = {})。
5.如果你是用的eclipse的话,工程名上会报一个红叉叉,强迫症看起来还是有点不舒服的。那怎么去掉呢?右键工程名–>Maven–>Update project–>OK 就行了。
下期再见。。。