本文只是简单的来介绍一下MyBatis与SpringBoot的集成,不涉及更进一步的应用,更进一步的内容可以参考该系列的其他博客。
1、集成MyBatis所需依赖
默认已经搭建了SpringBoot的开发环境,集成MyBatis所需依赖如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2、工程配置文件
完成了依赖的引入,接下来就可以完成工程配置文件部分:
server.port=9999
# datasource configuration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
# mapper configuration
mybatis.mapper-locations=classpath:mapper/*.xml
3、实体类
实体模型的代码如下:
import java.io.Serializable;
import lombok.Data;
@Data
public class User implements Serializable {
private String id;
private String name;
private String password;
private String sex;
}
由于在项目中引用了lombok,所以可以直接使用@Data注解,从而免去了写Set/Get方法,lombok的依赖如下:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
4、Mapper接口以及xml文件
import org.apache.ibatis.annotations.Mapper;
import com.learn.payment.entity.User;
@Mapper
public interface UserMapper {
public User findUserById(String id);
}
对应的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.learn.payment.mapper.UserMapper">
<select id="findUserById" parameterType="java.lang.String" resultType="com.learn.payment.entity.User">
select*from User where id=#{id}
</select>
</mapper>
在xml文件中,动态参数这里有一个小细节,动态参数的设置有两种方式,一种就是# {},另一种为$ {}一般常用的是#{},这种形式能够防止sql注入,而$ {}则有可能会引发sql注入。
select * from User where id=#{id},例如输入的id值为1时,返回的sql语句应该为:selectfrom User where id=‘1’,如果使用$ {},则返回的sql语句为:selectfrom User where id=1。
5、Service层的接口以及实现
Service层的接口:
import com.learn.payment.entity.User;
public interface IUserService {
public User findUserById(String id);
}
实现类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.learn.payment.entity.User;
import com.learn.payment.mapper.UserMapper;
@Service
public class UserService implements com.learn.payment.service.IUserService {
@Autowired
private UserMapper UserMapper;
@Override
public User findUserById(String id) {
return UserMapper.findUserById(id);
}
}
6、Controller层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.learn.payment.entity.User;
import com.learn.payment.service.IUserService;
@RestController
@RequestMapping("/v1/api")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public User findUserById(@PathVariable(value = "id") String id) {
return userService.findUserById(id);
}
}
关于在形参中使用的注解总结:
@PathVariable:应用于参数直接标记在链接之后的形式,如:/user/{id}
@RequestBody:应用于post请求中获得body中的参数值
@RequestParam:应用于"?id=1"这种形式
完成以上这些内容以后,在浏览器中输入:http://localhost:9999/v1/api/user/1,即可查询如下内容:
{"id":"1","name":"rhine","password":"28e5ea71eb6600afb02132dcf27b8e75","sex":null}
7、工程目录结构
--learn-mybatis
--|src/main/java
--|com.learn.payment
--|controller
--|UserController.java
--|entity
--|User.java
--|mapper
--|UserMapper.java
--|service
--|imp
--|UserService.java
--|IUserService.java
--|Application.java
--|src/main/resources
--mapper
--|UserMapper.xml
--|application.properties