SpringBoot整合MyBatis(基于注解)

SpringBoot整合MyBatis(注解)

  1. 导入依赖
    org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java

  2. 编写配置文件
  • 配置application-dev.yml文件
(配置数据库driver、url、username、password)
spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver

  • 配置application.yml文件
spring: profiles: active: dev

【SpringBoot整合MyBatis(基于注解)】? 两个文件的意思是:在项目中,我们会配置多个环境,比如开发环境,测试环境等,每个环境的连接的数据库可能会不一样,所以我们把每个环境的参数配置到yml中。在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式
application-dev.yml:开发环境 application-test.yml:测试环境 application-prod.yml:生产环境

至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
  1. 编写实体User
    package com.mooc.springboot_mybatisdemo.pojo; public class User { private Integer uid; private String username; private String password; private String fullname; public Integer getUid() { return uid; }public void setUid(Integer uid) { this.uid = uid; }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 getFullname() { return fullname; }public void setFullname(String fullname) { this.fullname = fullname; }@Override public String toString() { return "User{" + "uid=" + uid + ", username='" + username + '\'' + ", password='" + password + '\'' + ", fullname='" + fullname + '\'' + '}'; } }

  2. 编写Mapper
    package com.mooc.springboot_mybatisdemo.dao; import com.mooc.springboot_mybatisdemo.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface UserDao { @Select("select * from user") List findAll(); }

    注意的是@Mapper注解,是声明这个类是mapper接口,实再mybatis的动态代理。
    这个只是单个类中的注解,如果有多个mppaer接口,还可以采取包扫描的方式进行,在启动类中加上mapper包扫描
    @MapperScan(basePackages = {"com.mooc.springboot_mybatisdemo.dao"})

  3. 编写controller层(为了方便演示,不编写service层了)
    package com.mooc.springboot_mybatisdemo.controller; import com.mooc.springboot_mybatisdemo.dao.UserDao; import com.mooc.springboot_mybatisdemo.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.List; import java.util.Map; @RestControllerpublic class UserController {@Autowiredprivate UserDao userDao; @RequestMapping("/hello")public Map,Object> findAll(){List list=userDao.findAll(); System.out.println(list); Map,Object> map=new HashMap<>(); map.put("status",200); map.put("list",list); return map; }}

  4. 页面显示
最启动应用,在浏览器访问,页面如下
SpringBoot整合MyBatis(基于注解)
文章图片

有看到返回的json数据,说明整合mybatis成功咯!!!
参考:https://blog.csdn.net/iku5200/article/details/82856621

    推荐阅读