美文网首页SpringBoot
第五章 SpringBoot 集成mybatis

第五章 SpringBoot 集成mybatis

作者: liaijuyyer | 来源:发表于2021-11-24 01:31 被阅读0次

    一、导入相关依赖

    <dependency>
       <groupId>org.mybatis.spring.boot</groupId>
       <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>2.1.1</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.25</version>
    </dependency>
    

    二 配置数据源

    spring.profiles.active=uat
    spring.datasource.username=root
    spring.datasource.password=12345678
    #mysql8的驱动包
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/nacos?serverTimeZone=UTC&useUnicode=true&characterEncoding=utf8
    #设置mapper.xml的位置 不然mybatis扫描不到mapper.xml的配置文件 会报无效的绑定错误
    mybatis.mapper-locations=classpath:mapper/*.xml
    

    三 配置实体类

    @Data
    public class ConfigInfo implements Serializable {
    
        private static final long serialVersionUID = 6337622087324151434L;
    
        private Long id;
        private String dataId;
        private String groupId;
        private String content;
        private String md5;
        private Date gmtCreate;
        private Date gmtModified;
        private String srcUser;
        private String srcIp;
        private String appName;
        private String tenantId;
        private String cDesc;
        private String cUse;
        private String effect;
        private String type;
        private String cSchema;
    }
    

    四 配置mapper.xml

    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.xxx.test.springboot2.mapper.ConfigInfoMapper">
    
        <resultMap type="com.xxx.test.springboot2.entity.ConfigInfo" id="configInfoMap">
            <id column="id" property="id" jdbcType="BIGINT" javaType="java.lang.Long"/> <!--jdbcType必须大写 -->
            <result column="data_id" property="dataId" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <result column="group_id" property="groupId"/> <!--省略javaType与jdbcType mybatis会自动进行映射 -->
            <result column="content" property="content"/>
            <result column="md5" property="md5"/>
            <result column="gmt_create" property="gmtCreate"/>
            <result column="gmt_modified" property="gmtModified"/>
            <result column="src_user" property="srcUser"/>
            <result column="src_ip" property="srcIp"/>
            <result column="app_name" property="appName"/>
            <result column="tenant_id" property="tenantId"/>
            <result column="c_desc" property="cDesc"/>
            <result column="c_use" property="cUse"/>
            <result column="effect" property="effect"/>
            <result column="type" property="type"/>
            <result column="c_schema" property="cSchema"/>
        </resultMap>
    
        <select id="queryAll" resultMap="configInfoMap">
            select * from config_info;
        </select>
    </mapper>
    

    五 配置mapper类

    //mapper类是一个接口
    @Mapper
    public interface ConfigInfoMapper {
    
        List<ConfigInfo> queryAll();
    }
    

    六 配置启动类

    //因为在mapper接口上加了@Mapper注解 所以不需要再在启动类上添加@MapperScan注解了
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    

    相关文章

      网友评论

        本文标题:第五章 SpringBoot 集成mybatis

        本文链接:https://www.haomeiwen.com/subject/zgogtrtx.html