SpringBoot.04.SpringBoot整合MyBatis

x33g5p2x  于2021-09-19 转载在 Spring  
字(7.0k)|赞(0)|评价(0)|浏览(599)

准备工作

1.mapper模板

SpringBoot集成MyBatis我们需要新建mapper文件,这里我们先新建一个mapper模板。打开IDEA,依次选择File->settings->Editor->File and Code Templates,然后选中Files;在name中键入Mapper、在Extension中键入xml,将以下内容填到文本框中

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.christy.mapper">
  4. </mapper>

最后点击Apply->OK;如下图所示:

2.t_user

我们使用数据库christy中的t_user表作为本次使用的数据表,建表语句如下:

  1. USE `christy`;
  2. /*Table structure for table `t_user` */
  3. DROP TABLE IF EXISTS `t_user`;
  4. CREATE TABLE `t_user` (
  5. `id` int(6) NOT NULL AUTO_INCREMENT,
  6. `name` varchar(40) CHARACTER SET utf8 DEFAULT NULL,
  7. `age` int(3) DEFAULT NULL,
  8. `salary` double(7,2) DEFAULT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

整合MyBatis

1.新建Module

我们选择Spring Initializr的方式新建一个Module,按照下图填写信息后点击Next

Dependencies中选择Spring Web,点击Finish,如下图所示:

2.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.5.4</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <groupId>com.christy</groupId>
  11. <artifactId>springboot-04-mybatis</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>springboot-04-mybatis</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <!-- spring-boot-starter-web -->
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <!-- spring-boot-starter-test -->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <!-- druid -->
  31. <dependency>
  32. <groupId>com.alibaba</groupId>
  33. <artifactId>druid</artifactId>
  34. <version>1.2.6</version>
  35. </dependency>
  36. <!-- mysql-connector-java -->
  37. <dependency>
  38. <groupId>mysql</groupId>
  39. <artifactId>mysql-connector-java</artifactId>
  40. <version>5.1.38</version>
  41. </dependency>
  42. <!-- mybatis-spring-boot-starter 由于springboot整合mybatis版本中默认依赖mybatis 因此不需要额外引入mybati版本,否则会出现冲突 -->
  43. <dependency>
  44. <groupId>org.mybatis.spring.boot</groupId>
  45. <artifactId>mybatis-spring-boot-starter</artifactId>
  46. <version>2.1.4</version>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

3.application.yml

  1. server:
  2. port: 8804
  3. # datasource
  4. spring:
  5. datasource:
  6. type: com.alibaba.druid.pool.DruidDataSource
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/christy?characterEncoding=UTF-8
  9. username: root
  10. password: 123456
  11. # mybatis
  12. mybatis:
  13. mapper-locations: classpath:com/christy/mapper/*.xml #指定mapper配置文件位置
  14. type-aliases-package: com.christy.entity #指定起别名所在包

4.Springboot04MybatisApplication.java

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /** * * @author Christy * @date 2021/9/2 14:44 * * @MapperScan 作用在类上,扫描指定包下面的mapper接口 * MapperScan可以根据指定的包名一次性扫描所有的接口 * Mapper只能作用在指定的某个Mapper接口上 */
  5. @SpringBootApplication
  6. @MapperScan("com.christy.mapper")
  7. public class Springboot04MybatisApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Springboot04MybatisApplication.class, args);
  10. }
  11. }

5.User.java

  1. /** * @Author Christy * @Date 2021/9/2 14:48 **/
  2. public class User {
  3. private Integer id;
  4. private String name;
  5. private Integer age;
  6. private double salary;
  7. public User() {
  8. }
  9. public User(Integer id, String name, Integer age, double salary) {
  10. this.id = id;
  11. this.name = name;
  12. this.age = age;
  13. this.salary = salary;
  14. }
  15. public Integer getId() {
  16. return id;
  17. }
  18. public void setId(Integer id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public Integer getAge() {
  28. return age;
  29. }
  30. public void setAge(Integer age) {
  31. this.age = age;
  32. }
  33. public double getSalary() {
  34. return salary;
  35. }
  36. public void setSalary(double salary) {
  37. this.salary = salary;
  38. }
  39. @Override
  40. public String toString() {
  41. return "User{" +
  42. "id=" + id +
  43. ", name='" + name + '\'' +
  44. ", age=" + age +
  45. ", salary=" + salary +
  46. '}';
  47. }
  48. }

6.UserMapper.java

  1. import com.christy.entity.User;
  2. import org.apache.ibatis.annotations.Mapper;
  3. import java.util.List;
  4. /** * @Author Christy * @Date 2021/9/2 14:50 * 我们已经在入口类上加了注解@MapperScan,这里就不要加注解@Mapper了 **/
  5. // @Mapper
  6. public interface UserMapper {
  7. /** * 查找所有用户 * @author Christy * @date 2021/9/2 14:52 * @param * @return java.util.List<User> */
  8. List<User> findAll();
  9. }

7.UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.christy.mapper.UserMapper">
  4. <select id="findAll" resultType="com.christy.entity.User">
  5. select * from t_user
  6. </select>
  7. </mapper>

8.UserService

  1. import com.christy.entity.User;
  2. import java.util.List;
  3. /** * @Author Christy * @Date 2021/9/2 14:55 **/
  4. public interface UserService {
  5. /** * 查询所有用户 * @author Christy * @date 2021/9/2 14:56 * @param * @return java.util.List<com.christy.entity.User> */
  6. List<User> findAll();
  7. }
  8. import com.christy.entity.User;
  9. import com.christy.mapper.UserMapper;
  10. import com.christy.service.UserService;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import java.util.List;
  14. /** * @Author Christy * @Date 2021/9/2 14:56 **/
  15. @Service
  16. public class UserServiceImpl implements UserService {
  17. private UserMapper userMapper;
  18. @Autowired
  19. public UserServiceImpl(UserMapper userMapper) {
  20. this.userMapper = userMapper;
  21. }
  22. @Override
  23. public List<User> findAll() {
  24. return userMapper.findAll();
  25. }
  26. }

9.UserController.java

  1. import com.christy.entity.User;
  2. import com.christy.service.UserService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.util.List;
  7. /** * @Author Christy * @Date 2021/9/2 14:58 **/
  8. @RestController
  9. @RequestMapping("user")
  10. public class UserController {
  11. private UserService userService;
  12. @Autowired
  13. public UserController(UserService userService) {
  14. this.userService = userService;
  15. }
  16. @RequestMapping("findAll")
  17. public List<User> findAll(){
  18. return userService.findAll();
  19. }
  20. }

10.测试

我们启动项目,浏览器访问http://localhost:8804/user/findAll,结果如下:

相关文章