一、目的
在已经建好的SpringBoot项目中,进一步建立起Mybatis框架,以实现建立起SpringBoot+Mybatis项目。
二、Mybatis介绍
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC
和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口
Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
三、项目骨架
如图所示,在建好SpringBoot项目的基础上构建如下项目骨架。
四、对应文件及代码
1 UserController.java
package com.zpark.controller;
import com.zpark.service.UserService;
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.RestController;
@RestController
@RequestMapping("/testBoot")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getUser/{id}")
public String GetUser(@PathVariable int id){
return userService.Sel(id).toString();
}
}
2 User.java
实体类的作用:用来保存数据,结果集里的数据就存放到实体类中
package com.zpark.entity;
public class User {
private Integer id;
private String userName;
private String passWord;
private String realName;
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 String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", realName='" + realName + '\'' +
'}';
}
}
3 UserMapper.java(接口)
package com.zpark.mapper;
import com.zpark.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
User Sel(int id);
}
4 UserService.java
package com.zpark.service;
import com.zpark.entity.User;
import com.zpark.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public User Sel(int id){
return userMapper.Sel(id);
}
}
5 SbdemoApplication.java
启动类
package com.zpark;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.zpark.mapper")
@SpringBootApplication
public class SbdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SbdemoApplication.class, args);
}
}
6 UserMapping.xml
select * from user where id = #{id}相当于UserMapping.xml说了一句话,交由MyBatis去执行,让MyBatis去连接数据库,再执行该SQL语句。
<?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.zpark.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.zpark.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
<result column="passWord" jdbcType="VARCHAR" property="passWord" />
<result column="realName" jdbcType="VARCHAR" property="realName" />
</resultMap>
<select id="Sel" resultType="com.zpark.entity.User">
select * from user where id = #{id}
</select>
</mapper>
7 application.yml
server:
port: 8080
spring:
datasource:
username: root
password:
url: jdbc:mysql://localhost:3306/db_test?characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*Mapping.xml
type-aliases-package: com.zpark.entity
五、测试
在浏览器中输入如下URL