mybatis-spring-boot-starter初探

该博客的源码的下载地址

1·创建项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2·pom.xml 看看即可

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gabriel</groupId>
    <artifactId>mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3·配置application

1·resources文件夹–>右键–>new–>自定义yml模板(示例中:my-yml,自定义文件模板详见:IDEA 文件模板

在这里插入图片描述
2·在 file name 输入application,点击ok
在这里插入图片描述
3·删除原有的 application.properties,以下是我 yml 模板的内容:

spring:
# profiles:
#   active: dev
 datasource:
   driver-class-name: com.mysql.cj.jdbc.Driver
   url: jdbc:mysql://localhost:3307/usertest?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
   username: root
   password: 123456
server:
  port: 8080
mybatis:
  config-location: classpath:mybatis-config.xml

这里安利一下 yml 的插件,(此处欠一篇插件推荐的链接,下次补充)

4·配置mybatis

按照上文提到的yml配置,里面涉及到mybatis的配置

mybatis:
  config-location: classpath:mybatis-config.xml

resources–>右键–>new–>自定义xml模板(示例中:mybatis-config,自定义文件模板详见:IDEA 文件模板

在这里插入图片描述
file name 填入:mybatis-config(与yml 的 配置对应即可)
在这里插入图片描述
我的mybatis-config.xml 内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
        <!--<mapper resource="mapper/UserMapper.xml"/>-->
    </mappers>
</configuration>

5·创建数据库查询的mapper的xml

1·创建一个存放mapper 相关xml 的文件夹
在这里插入图片描述
2·创建mapper 的xml,例如我这边要访问的数据表 my_demo
在这里插入图片描述
文件夹mapper右键–>new–>mapper 的xml模板文件(示例中为:mybatis-mapper,IDEA 文件模板

在这里插入图片描述

在这里插入图片描述

6· 创建实体对象

1·右键创建包文件夹
在这里插入图片描述
2·新建实体
在这里插入图片描述
在这里插入图片描述

3·按住按钮 Alt+Ins,选择Getter and Setter,然后按住 Ctrl+A。
在这里插入图片描述
在这里插入图片描述
于是乎,如下:
在这里插入图片描述

7·dao 的接口 和实现

1· 跟上文创建 entity 包文件一样的,方式创建 dao,以及接口 MyDemoDao 。
在这里插入图片描述
Kind 选择 Interface ,Name 为:MyDemoDao
在这里插入图片描述
2·在dao 包文件下,新建实现的包文件 impl,并在impl下 新建 java 文件,MyDemoImplDao
在这里插入图片描述
3·定义接口里的方法,例如 获取数据的列表
在这里插入图片描述

package com.gabriel.mybatis.dao;

import com.gabriel.mybatis.entity.MyDemo;

import java.util.List;

public interface MyDemoDao {

    //    获取数据的列表
    List<MyDemo> getList();

}

4·实现接口的方法,implements MyDemoDao

package com.gabriel.mybatis.dao.impl;

import com.gabriel.mybatis.dao.MyDemoDao;

public class MyDemoImplDao implements MyDemoDao {

}

接着 按住Ctrl+i ,重写接口里的方法
在这里插入图片描述

package com.gabriel.mybatis.dao.impl;

import com.gabriel.mybatis.dao.MyDemoDao;
import com.gabriel.mybatis.entity.MyDemo;

import java.util.List;

public class MyDemoImplDao implements MyDemoDao {
    @Override
    public List<MyDemo> getList() {
        return null;
    }
}

这时候 注入 SqlSessionTemplate,并给该类 加上@Component的注解,以及 getList()的实现。

package com.gabriel.mybatis.dao.impl;

import com.gabriel.mybatis.dao.MyDemoDao;
import com.gabriel.mybatis.entity.MyDemo;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyDemoImplDao implements MyDemoDao {

    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    @Override
    public List<MyDemo> getList() {
//这里的 getMyDemoList 需要与mapper.xml 里的id 一一对应
        return sqlSessionTemplate.selectList("getMyDemoList");
    }
}

8 增加Mapper xml 文件里的sql 查询语句

注意:这里的 id 值为:getMyDemoList 需要与上文提到的一一对应。
resultType 指定的是 返回的对象实体package名.

<?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.gabriel.mybatis.dao">

    <select id="getMyDemoList" resultType="com.gabriel.mybatis.entity.MyDemo">
        select id,name from my_demo
    </select>

</mapper>

9· 检查mapper 的xml 是否有在 mybatis的mapper 范围内

在这里插入图片描述

10· 单元测试

1·增加@MapperScan的注解,以及包路径。@MapperScan("com.gabriel.mybatis.dao.impl.*")
2·注入dao的实现类

  @Resource
    MyDemoImplDao myDemoImplDao;

3·方法测试
在这里插入图片描述

package com.gabriel.mybatis;

import com.gabriel.mybatis.dao.impl.MyDemoImplDao;
import com.gabriel.mybatis.entity.MyDemo;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@MapperScan("com.gabriel.mybatis.dao.impl.*")
@SpringBootTest
class MybatisApplicationTests {

    @Resource
    MyDemoImplDao myDemoImplDao;

    @Test
    void contextLoads() {
        List<MyDemo> myDemoList = myDemoImplDao.getList();
        System.out.println(String.format("MyDemo 表的全部记录数为:%d", myDemoList.size()));

    }

}

11· 插入数据的测试

重复以上的接口定义方法、接口方法实现、mapper 的xml 里的sql 语句,我们增加一个 插入数据的测试。

1·接口方法定义

package com.gabriel.mybatis.dao;

import com.gabriel.mybatis.entity.MyDemo;

import java.util.List;

public interface MyDemoDao {

    //    获取数据的列表
    List<MyDemo> getList();

    //    插入一条数据
    void addOneRecord(MyDemo myDemo);
}

2·接口方法实现

package com.gabriel.mybatis.dao.impl;

import com.gabriel.mybatis.dao.MyDemoDao;
import com.gabriel.mybatis.entity.MyDemo;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyDemoImplDao implements MyDemoDao {

    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    @Override
    public List<MyDemo> getList() {
//这里的 getMyDemoList 需要与mapper.xml 里的id 一一对应
        return sqlSessionTemplate.selectList("getMyDemoList");
    }

    @Override
    public void addOneRecord(MyDemo myDemo) {
        sqlSessionTemplate.insert("addRecord", myDemo);
    }
}

3·mapper 的xml 里的sql 语句

<?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.gabriel.mybatis.dao">

    <select id="getMyDemoList" resultType="com.gabriel.mybatis.entity.MyDemo">
        select id,name from my_demo
    </select>

    <insert id="addRecord" parameterType="com.gabriel.mybatis.entity.MyDemo">
        insert into my_demo (id,name) values (#{Id},#{Name})
    </insert>
</mapper>

4·单元测试

package com.gabriel.mybatis;

import com.gabriel.mybatis.dao.impl.MyDemoImplDao;
import com.gabriel.mybatis.entity.MyDemo;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@MapperScan("com.gabriel.mybatis.dao.impl.*")
@SpringBootTest
class MybatisApplicationTests {

    @Resource
    MyDemoImplDao myDemoImplDao;

    @Test
    void contextLoads() {

//        增加一条记录
        MyDemo myDemo = new MyDemo();
        myDemo.Id = Integer.toUnsignedLong(myDemoImplDao.getList().size() + 1);
        myDemo.Name = String.format("哈哈哈:%d", myDemo.Id);
        myDemoImplDao.addOneRecord(myDemo);
//        查询记录
        List<MyDemo> myDemoList = myDemoImplDao.getList();
        if (myDemoList.size() > 0) {
            myDemoList.forEach(MybatisApplicationTests::PrintAll);
        }
    }

    static void PrintAll(MyDemo myDemo) {
        System.out.println(String.format("Id:%s,Name:%s", myDemo.Id, myDemo.Name));
    }

}

在这里插入图片描述
该博客的源码的下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值