springboot整合mybatis

环境说明

win10 + jdk8 + intellij2019.1 + springboot2.2.0 + mybatis-spring-boot-starter2.1.1 + MySQL5.7

准备工作

1. 在本地mysql中创建test数据库

2. 创建city表

user test;
CREATE TABLE city
(
  id      INT PRIMARY KEY auto_increment,
  name    VARCHAR(16),
  state   VARCHAR(16),
  country VARCHAR(16)
);

详细步骤

1. 创建 springboot项目,勾选所需依赖(mysql、mybatis)

2. 创建 mybatis项目结构(entity.java实体类、mapper.java接口、mapper.xml映射文件)

  • 目录结构
    目录结构

  • entity实体类

    package cn.juxiewl.springbootmybatis.entity;
    
    public class City {
    
        private Long id;
        private String name;
        private String state;
        private String country;
    
        public Long getId() {
            return this.id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getState() {
            return this.state;
        }
    
        public void setState(String state) {
            this.state = state;
        }
    
        public String getCountry() {
            return this.country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        @Override
        public String toString() {
            return getId() + "," + getName() + "," + getState() + "," + getCountry();
        }
    }
    
  • mapper接口

    package cn.juxiewl.springbootmybatis.mapper;
    
    import cn.juxiewl.springbootmybatis.entity.City;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Options;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    /**
    * @Date 2019-10-25 18:09
    * @Version 1.0
    */
    //@Mapper
    public interface CityMapper {
    
        /**
        * 插入方法
        * @param city
        */
        @Insert("insert into city (name,state, country) values(#{name},#{state}, #{country})")
        @Options(useGeneratedKeys = true, keyProperty = "id")
        void insert(City city);
    
        @Select("select id,name,state,country FROM city WHERE id = #{id}")
        City findById(long id);
    
        /**
        * 查询所有城市信息
        * @return
        */
        List<City> selectAll();
    }
    
  • mapper.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="cn.juxiewl.springbootmybatis.mapper.CityMapper">
        <sql id="wrongId">
            *
        </sql>
        <select id="selectAll" resultType="cn.juxiewl.springbootmybatis.entity.City">
            SELECT
            <include refid="wrongId"/>
            FROM city
        </select>
    </mapper>
    
    

3. 配置( 4个:指定实体类、接口类、映射文件位置、数据源)

  • application.yml中

    # mybatis 配置
    mybatis:
    # mapper.xml映射文件 位置
    mapper-locations: classpath:/mapper/**/*.xml
    # entity实体类包 位置
    type-aliases-package: cn.juxiewl.springbootmybatis.entity
    
    spring:
    # 数据源
    datasource:
        driver-class-name: com.mysql.cj.jdbc.NonRegisteringDriver
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false
        username: root
        password: root
    
  • springboot启动类

    package cn.juxiewl.springbootmybatis;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    # 指定需要扫描mapper接口的包
    @MapperScan("cn.juxiewl.springbootmybatis.mapper")
    public class SpringbootMybatisApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootMybatisApplication.class, args);
        }
    
    }
    

4. 测试

    package cn.juxiewl.springbootmybatis;

    import cn.juxiewl.springbootmybatis.entity.City;
    import cn.juxiewl.springbootmybatis.mapper.CityMapper;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;

    //@RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringbootMybatisApplicationTests {

        @Autowired
        private CityMapper cityMapper;

        @Test
        public void inset(){
            City city = new City();
            city.setName("xiaobailong");
            city.setCountry("EN");
            city.setState("US");
            cityMapper.insert(city);
            System.out.println("刚才插入的数据为:");
            System.out.println(cityMapper.findById(city.getId()));
        }

        @Test
        public void findAll(){
            System.out.println("-------- 查询全部数据 --------");
            System.out.println(cityMapper.selectAll());
        }

    }

测试结果

参考

  1. mybatis/spring-boot-starter项目

  2. mybatis 在github上的所有项目

posted @ 2019-10-29 18:21  小白=>龙  阅读(209)  评论(0编辑  收藏  举报