首页 > 编程语言 > 详细

springboot笔记07——整合MyBatis

时间:2019-08-06 20:37:21      阅读:91      评论:0      收藏:0      [点我收藏+]

前言

Springboot 整合 MyBatis 有两种方式,分别是:“全注解版” 和 “注解、xml混合版”。


创建项目

创建Springboot项目,选择依赖
技术分享图片


Maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
    <scope>runtime</scope>
</dependency>


配置文件

#配置数据源
spring:
  datasource:
     url: jdbc:mysql://localhost:3306/springboot-test?useUnicode=true&characterEncoding=utf8
     username: root
     password: root
     driver-class-name: com.mysql.jdbc.Driver

全注解版

项目结构

技术分享图片


实体类

public class User {
    private int id;
    private String name;
    
    //getter、setter、toString
    }


Dao

@Mapper
public interface UserDao {

    @Select("select * from user where id = #{id}")
    public User getUserById(@Param("id") int id);

    @Select("select * from user")
    public List<User> getAllUser();
}


Service

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User getUserById(int id) {
        return this.userDao.getUserById(id);
    }

    @Override
    public List<User> getAllUser() {
        return this.userDao.getAllUser();
    }
}


控制类

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id")int id) {
        return this.userService.getUserById(id);
    }

}


测试

http://localhost:8080/user/getUserById/1


注解、xml混合版

项目结构

(比之前的多了一个映射文件)
技术分享图片


配置文件

添加了mybatis的配置,包括Mapper文件的位置、alias的配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot-test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

#mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.jotal.springboot05mybatis.entities


实体类

public class User {
    private int id;
    private String name;
//getter、setter、toString
}


Dao

/*
* 在主启动类使用了@MapperScan就不用每一个添加@Mapper了
* */
public interface UserDao {

//    @Select("select * from user where id = #{id}")

//    若使用全注解需要添加@Param("id")到形式参数之前
     User getUserById(int id);

//    @Select("select * from user")
     List<User> getAllUser();
}


映射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.jotal.springboot05mybatis.dao.UserDao">
    <select id="getUserById" parameterType="Integer" resultType="user">
        select * from user where id = #{id}
    </select>
    <select id="getAllUser" resultType="user">
        select * from user
    </select>
</mapper>


Service、控制类和之前的一样

可以在主启动类使用@MapperScan注解扫描Mapper类,就不需要在每个Mapper类添加@Mapper

@MapperScan("com.jotal.springboot05mybatis.dao")
@SpringBootApplication
public class Springboot05MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot05MybatisApplication.class, args);
    }

}

到此,Springboot整合Mybatis的两种方式就介绍到这里了。

springboot笔记07——整合MyBatis

原文:https://www.cnblogs.com/Jotal/p/11311563.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!