SpringBoot(九)_springboot集成 MyBatis

简介: MyBatis 是一款标准的 ORM 框架,被广泛的应用于各企业开发中。具体细节这里就不在叙述,大家自行查找资料进行学习下。加载依赖 org.mybatis.spring.boot mybatis-spring-boot-starter 1.

MyBatis 是一款标准的 ORM 框架,被广泛的应用于各企业开发中。具体细节这里就不在叙述,大家自行查找资料进行学习下。

加载依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

application 配置(application.yml)

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.zhb.entity
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    password: root
    username: root

启动类

在启动类中添加对 Mapper 包扫描@MapperScan,Spring Boot 启动的时候会自动加载包路径下的 Mapper。

@Spring BootApplication
@MapperScan("com.zhb.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

或者直接在 Mapper 类上面添加注解@Mapper,建议使用上面那种,不然每个 Mapper 加个注解会很麻烦。

代码展示

这里只说下,我们在controller 中 尽量使用 Restful 风格

@RestController
public class UserController {

    @Resource
    private UserMapper userMapper;


    @GetMapping(value = "/users")
    public List<UserEntity> getUsers() {
        List<UserEntity> users=userMapper.getAll();
        return users;
    }



    @GetMapping(value = "/users/{id}")
    public UserEntity getUser(@PathVariable(value = "id") Long id) {
        UserEntity user=userMapper.getOne(id);
        return user;
    }

    @PostMapping("/users")
    public void save(UserEntity user) {
        userMapper.insert(user);
    }

    @PutMapping("/users")
    public void update(UserEntity user) {
        userMapper.update(user);
    }

    @DeleteMapping(value="/users/{id}")
    public void delete(@PathVariable("id") Long id) {
        userMapper.delete(id);
    }


}

实际开发常遇问题

在平常开发中,我们经常会遇到返回树结构,如下展示

[
  {
    "id": "1",
    "name": "山东",
    "pid": "0",
    "children": [
      {
        "id": "2",
        "name": "济南",
        "pid": "1",
        "children": [
          {
            "id": "3",
            "name": "高新区",
            "pid": "2",
            "children": null
          }
        ]
      }
    ]
  }
]
(1) java中 通过对list数据进行拼接成树

如下面的方法(这里直接声明了list,往里面添加数据,来演示查询出的list集合)

@GetMapping(value = "/area")
    public   List<AreaTreeEntity> get(){
        
        List<AreaTreeEntity> res= new ArrayList<>();
        Map<String,List<AreaTreeEntity>> childMap = new HashMap<>(16);

        //为了方便测试,构建list数据
        List<AreaTreeEntity> s =  new ArrayList<>();
        AreaTreeEntity a = new AreaTreeEntity();
        a.setId("1");
        a.setName("山东");
        a.setPid("0");
        s.add(a);

        AreaTreeEntity a1 = new AreaTreeEntity();
        a1.setId("2");
        a1.setName("济南");
        a1.setPid("1");
        s.add(a1);

        AreaTreeEntity a2 = new AreaTreeEntity();
        a2.setId("3");
        a2.setName("高新区");
        a2.setPid("2");
        s.add(a2);

        for(AreaTreeEntity entity : s){

            if ("0".equals(entity.getPid())) {

                res.add(entity);
            }
            else {

                List<AreaTreeEntity> childList = (childMap.containsKey(entity.getPid())) ? childMap.get(entity.getPid()) : new ArrayList<>();
                childList.add(entity);

                if (!childMap.containsKey(entity.getPid())){
                    childMap.put(entity.getPid(),childList);
                }
            }
        }

        for(AreaTreeEntity entity : res){
            findChild(entity,childMap);
        }

        return res;
    }

    public void findChild(AreaTreeEntity entity,Map<String,List<AreaTreeEntity>> childMap){

        if (childMap.containsKey(entity.getId())){
            List<AreaTreeEntity> chidList = childMap.get(entity.getId());
            for (AreaTreeEntity childEntity : chidList){
                findChild(childEntity,childMap);
            }
            entity.setChildren(chidList);
        }
    }

经过验证,返回数据如下

[
  {
    "id": "1",
    "name": "山东",
    "pid": "0",
    "children": [
      {
        "id": "2",
        "name": "济南",
        "pid": "1",
        "children": [
          {
            "id": "3",
            "name": "高新区",
            "pid": "2",
            "children": null
          }
        ]
      }
    ]
  }
]
(2) 使用mabatis的collection 直接查询出树结构

<mapper namespace="com.zhb.mapper.AreaTreeMapper" >
    <resultMap id="TreeResultMap" type="com.zhb.entity.AreaTreeEntity" >
        <result column="id" property="id" jdbcType="VARCHAR" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <collection property="children" column="id" ofType="com.zhb.entity.AreaTreeEntity" javaType="ArrayList" select="selectChildrenById"/>
    </resultMap>


    <select id="getAreaTree" resultMap="TreeResultMap">
        select  id,name  from  area where parent_id  = '0'
    </select>

    <select id="selectChildrenById" parameterType="java.lang.String" resultMap="TreeResultMap">
        select id,name from  area where parent_id  = #{id}
    </select>

</mapper>

完整代码下载:github

注:项目中加入swagger ,方便大家测试,直接访问 http://localhost:8080/swagger-ui.html

学习不是要么0分,要么100分的。80分是收获;60分是收获;20分也是收获。有收获最重要。但是因为着眼于自己的不完美,最终放弃了,那就是彻底的0分了。
相关文章
|
1月前
|
XML Java 关系型数据库
【SpringBoot系列】SpringBoot集成Fast Mybatis
【SpringBoot系列】SpringBoot集成Fast Mybatis
|
4月前
|
XML SQL Java
ClickHouse【SpringBoot集成】clickhouse+mybatis-plus配置及使用问题说明(含建表语句、demo源码、测试说明)
ClickHouse【SpringBoot集成】clickhouse+mybatis-plus配置及使用问题说明(含建表语句、demo源码、测试说明)
145 0
|
2月前
|
SQL Java 数据库连接
【极光系列】SpringBoot集成Mybatis
【极光系列】SpringBoot集成Mybatis
【极光系列】SpringBoot集成Mybatis
|
3月前
|
缓存 Java 数据库连接
微服务框架(六)Spring Boot集成Mybatis及Druid
  此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。   本文为Spring Boot集成Mybatis,包括mybatis-generator的使用
|
3月前
|
缓存 Java 数据库连接
一文彻底搞懂Mybatis系列(十六)之MyBatis集成EhCache
一文彻底搞懂Mybatis系列(十六)之MyBatis集成EhCache
|
4月前
|
SQL Java 数据库连接
SpringBoot:第三篇 集成mybatis
SpringBoot:第三篇 集成mybatis
27 0
|
4月前
|
设计模式 缓存 Java
干翻Mybatis源码系列之第七篇:Mybatis提供的集成缓存方案
干翻Mybatis源码系列之第七篇:Mybatis提供的集成缓存方案
|
4月前
|
Java 数据库连接 数据库
Spring Boot 3 集成 MyBatis详解
MyBatis是一款开源的持久层框架,它极大地简化了与数据库的交互流程。与类似Hibernate的ORM框架不同,MyBatis更具灵活性,允许开发者直接使用SQL语句与数据库进行交互。Spring Boot和MyBatis分别是两个功能强大的框架,它们的协同使用可以极大地简化数据访问层的开发,提高整体的开发效率。本文将详细介绍在Spring Boot项目中如何集成MyBatis,以实现对数据库的轻松访问和操作。
176 2
Spring Boot 3 集成 MyBatis详解
|
28天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
1月前
|
druid Java 数据库连接
Spring Boot3整合MyBatis Plus
Spring Boot3整合MyBatis Plus
39 1