Springboot集成mybatis(10分钟上手)

我是黑帽子K,话不多说,直接上集成mybatis

ps:请在完成下列步骤前,先搭建好springboot环境,传送门 springboot快速搭建(五分钟上手)。

 

  • 复制下方mybatis的依赖至自己的项目中
    <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.2</version>
            </dependency>
  •  application.properties文件配置
    server.port=8081
    
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.url=jdbc:mysql://localhost:3306/heimaozik?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    mybatis.mapper-locations=classpath:mapper/*Mapper.xml
    mybatis.type-aliases-package=com.example.entity
  • 新建application.yml文件(yml文件结构清晰,建议用该文件替代,如果不需要yml文件可忽略这一步)

        说明:如果两个文件都存在则会以.properties结尾的文件为准。

#tomcat 端口号
server:
  port: 8081

spring:
  datasource:
    #mysql用户名
    username: root
    #密码
    password: 123456
    #连接地址
    url: jdbc:mysql://localhost:3306/heimaozik?&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    #数据库驱动类
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  #mybatis xml文件定位目录
  mapper-locations: classpath:mapper/*Mapper.xml
  #mybatis 别名全包路径
  type-aliases-package: com.example.entity
  • 新建TestMybatis.java实体类
    package com.example.demo.eitity;
    
    import lombok.Data;
    
    /**
     * 测试实体类
     *
     * @author heimaozik
     */
    @Data
    public class TestMybatis {
        private Integer id;
        private String name;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
  • 新建TestMybatisMapper.java类
    package com.example.demo.mapper;
    
    import com.example.demo.eitity.TestMybatis;
    import org.apache.ibatis.annotations.Insert;
    
    /**
     * 测试接口类
     *
     * @author heimaozik
     */
    public interface TestMybatisMapper {
    
        /**
         * xml形式写sql语句
         *
         * @param testMybatis 要插入的对象
         */
        void insertSomething(TestMybatis testMybatis);
    
        /**
         * 注解形式写sql语句
         *
         * @param testMybatis 要插入的对象
         */
        @Insert("insert into t_test_mybatis(name) values(#{name})")
        void insertSomethingWithTitle(TestMybatis testMybatis);
    }
    
  • 新建 TestMyabtisMapper.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.demo.mapper.TestMybatisMapper">
        <insert id="insertSomething" parameterType="com.example.demo.eitity.TestMybatis">
            insert into t_test_mybatis(name) values(#{name})
        </insert>
    
    </mapper>
  • 新建TestMybatisService.java类
    package com.example.demo.service;
    
    import com.example.demo.eitity.TestMybatis;
    import com.example.demo.mapper.TestMybatisMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    /**
     * 测试服务类
     *
     * @author heimaozik
     */
    @Service
    public class TestMybatisService {
    
        @Autowired
        private TestMybatisMapper testMybatisMapper;
    
        public void insert() {
            TestMybatis testMybatis = new TestMybatis();
            testMybatis.setName("heimaozik");
            testMybatisMapper.insertSomething(testMybatis);
        }
    
        public void insertWithTitle(){
            TestMybatis testMybatis = new TestMybatis();
            testMybatis.setName("heimaozik_title");
            testMybatisMapper.insertSomethingWithTitle(testMybatis);
        }
    }
    
  • 新建TestController.java类
    package com.example.demo.controller;
    
    import com.example.demo.service.TestMybatisService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RequestMapping("/test")
    @Controller
    public class TestController {
    
        @Autowired
        private TestMybatisService testMybatisService;
    
        @PostMapping(value = "/insert")
        public void insert() {
            testMybatisService.insert();
        }
    
        @PostMapping(value = "/insertWithTitle")
        public void insertWithTitle() {
            testMybatisService.insertWithTitle();
        }
    }
    
  •  最终的项目结构如下(最好不要修改结构,如修改需替换对应的包类路径)

  • 最后完整的pom文件
    <?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.3.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</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.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
  • 最后,创建一张表
    CREATE TABLE heimaozik.`t_test_mybatis` (
      `id` tinyint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `name` varchar(255) DEFAULT NULL COMMENT '姓名',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='测试表';
    
    
  •  测试

  •  可以看到,插入了几条记录,表明集成mybatis成功

 

写在最后,如果文章有什么疑问或者问题,可以评论区或者私信,谢谢。 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章