13springboot整合mybatis

懵懂的女人 提交于 2020-02-08 13:32:45

数据库表

CREATE TABLE `mall_category` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '类别Id',
  `parent_id` int(11) DEFAULT NULL COMMENT '父类别id当id=0时说明是根节点,一级类别',
  `name` varchar(50) DEFAULT NULL COMMENT '类别名称',
  `status` tinyint(1) DEFAULT '1' COMMENT '类别状态1-正常,2-已废弃',
  `sort_order` int(4) DEFAULT NULL COMMENT '排序编号,同类展示顺序,数值相等则自然排序',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100031 DEFAULT CHARSET=utf8

1 引入依赖

 <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>
</dependency>

2 编写yml文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/mall?characterEncoding=utf-8&useSSL=false

3别写实体类:Category

package com.example.mybaits.domain;

import java.util.Date;

public class Category {
    private Integer id;

    private Integer parentId;

    private String name;

    private Boolean status;

    private Integer sortOrder;

    private Date createTime;

    private Date updateTime;
}

4使用

4.1 注解方式

4.1.1 编写dao层

package com.example.mybaits.dao;

import com.example.mybaits.domain.Category;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface CategoryMapper {

    @Select("select * from mall_category where id =#{id}")
    Category findOne(@Param("id") Integer id);
}

4.1.1 测试

右键CategoryMapper----》Go to—>>Test 即可创建test
在这里插入图片描述

注意:
1 引入的包
import org.junit.Test;
import org.junit.runner.RunWith;

2 @test 的方法都是void

package com.example.mybaits.dao;

import com.example.mybaits.domain.Category;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.junit.runner.RunWith;


@RunWith(SpringRunner.class)
@SpringBootTest
public class CategoryMapperTest {

    @Autowired
    private CategoryMapper categoryMapper;

    @Test

    public void findOne() {
        Category one = categoryMapper.findOne(100001);
        System.out.print(one.toString());

    }

}

在这里插入图片描述

4.1.1.1 注意1

数据库中 parent_id 为0 没有显示
而实体类时 parentId 不匹配,所以修改yml文件

mybatis:
  configuration:
    map-underscore-to-camel-case: true

此时返回的结果:
在这里插入图片描述

4.1.1.1 注意2

每一个dao层都写@mapper 过于麻烦
可以dao层不写,在启动类上配置一个即可
@MapperScan(basePackages = “com.example.mybaits.dao”)

package com.imooc.mall;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.example.mybaits.dao")
public class MallApplication {

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

}

4.2 xml方式

4.2.1 编写dao层

public interface CategoryMapper {
    Category queryByid(Integer id);
}

4.2.2编写xml

在resource 下创建mapper目录:创建CategoryMapper.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.mybaits.dao.CategoryMapper">

    <select id="queryByid" resultType="com.example.mybaits.domain.Category">

        select * from mall_category where id = #{id}
    </select>

</mapper>

4.2.3修改yml

mapper-locations: classpath:mapper/*.xml
若想让 mapper 找到xml:
要么mapper和xml 具有相同的目录
要么在yml 文件中指定xml 位置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: njw19920905
    url: jdbc:mysql://47.96.95.15:3306/sell?characterEncoding=utf-8&useSSL=false

mybatis:
  configuration:
    map-underscore-to-camel-case: true
    # 控制台日志配置
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mapper/*.xml

4.2.4验证

package com.example.mybaits.dao;

import com.example.mybaits.domain.Category;
import com.google.gson.Gson;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.junit.runner.RunWith;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CategoryMapperTest {

    @Autowired
    private CategoryMapper categoryMapper;

    @Test
    public void queryByid() {
        Gson gson = new Gson();
        Category one = categoryMapper.queryByid(100001);
        System.out.print(gson.toJson(one));

    }

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!