一冯吓、導入相關依賴
<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>
二 配置數(shù)據(jù)源
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);
}
}