1、依赖注入
注入MySQL依赖(MySQL驱动使用Springboot版本对应的推荐版本,不需要手动指定版本)
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
注入Mybatis依赖(使用Springboot版本的推荐版本)
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
注入Lombok依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version
<scope>provided</scope>
</dependency>
2、配置数据源
在application.aproperties文件中,配置MySQL数据库的驱动、数据库url、用户名、密码。
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimeZone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
mybatis_mapperlocation.classpath=mapper/*.xml
3、编程
在Java目录下创建实体(entity)、映射(mapper)、服务(service)、服务实现(service/impl)、及控制器(controller)文件夹;
在resources目录下创建com.example.mapper文件夹
(1)、在entity中创建User实体
package com.example.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private String mobile;
private String email;
}
(2)、在mapper中创建UserMapper接口UserMapper.java
package com.example.mapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> listAll();
}
(3)、在资源文件夹下创建UserMapper.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.example.mapper.UserMapper">
<select id="listAll" resultType="com.example.entity.User">
select * from user
</select>
</mapper>
(4)、在服务中创建service接口UserService.java
package com.example.service;
import com.example.entity.User;
import java.util.List;
public interface UserService {
List<User> listAll();
}
(5)、在服务实现中创建UserServiceImpl.java
package com.example.service.impl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> listAll() {
return userMapper.listAll();
}
}
(6)、在控制器中创建UserController.java
package com.example.controller;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> selectAll() {
return userService.listAll();
}
}
4、另外一种方法(不需要在资源种创建xml文件)
在mapper中创建UserMapper接口UserMapper.java,添加注解。
package com.example.mapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user")
List<User> listAll();
}
5、相关支持
springboot官网:https://spring.io/projects/spring-boot
Maven仓库:https://mvnrepository.com