springboot简单整合mybatis

2021-06-24  本文已影响0人  穿越沙漠的马兰花

为了验证一个小的功能,搭建了一下这个小的服务,希望可以帮到刚刚入门的同学!

此操作基于idea!

一、new project

项目

项目结构图

具体的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.5.0</version>

<relativePath/>

    </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.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

        <dependency>

<groupId>org.apache.kafka</groupId>

<artifactId>kafka-clients</artifactId>

<version>1.1.1</version>

</dependency>

        <dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.16</version>

<scope>provided</scope>

</dependency>

        <dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>2.1.4</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

        <dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.22</version>

</dependency>

        <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

<profiles>

<profile>

<id>dev</id>

<properties>

<activeProfile>dev</activeProfile>

</properties>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

</profile>

<profile>

<id>test</id>

<properties>

<activeProfile>test</activeProfile>

</properties>

</profile>

<profile>

<id>online</id>

<properties>

<activeProfile>online</activeProfile>

</properties>

</profile>

</profiles>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>


三、application.properties

server.port=8889

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.url=jdbc:mysql://localhost:3306/test0622?useUnicode=true&characterEncoding=utf-8&relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true

mybatis.type-aliases-package=com.example.demo

mybatis.configuration.map-underscore-to-camel-case=true

mybatis.mapper-locations=classpath:mapper/*.xml

四、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="com.example.demo.dao.ValidationCodeEnumDao">

    <select id="queryList" resultType="com.example.demo.bean.ValidationCodeEnumEntity">

select* from aitpm_validation_code_enum

</select>

</mapper>

五、controller层

六、注意点

1、mapper映射的时候 namespace 要指定对

2、在启动类上要添加@mapperscan() 注解,项目启动的时候扫描到我们的dao层

3、配置文件中别忘了指定mybatis的相关配置,例如驼峰、mapper映射文件等

上一篇下一篇

猜你喜欢

热点阅读