Spring Boot使用配置文件方式整合MyBatis

pom.xml

<?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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.uos</groupId>
    <artifactId>databases</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>databases</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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <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>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

pom.xml

项目结构

一、创建Mapper接口文件

ArticleMapper接口

package com.uos.databases.dao;

import com.uos.databases.domain.Article;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ArticleMapper {
    public Article selectArticle(Integer id);
    public int updateArticle(Article article);
}

实体类

package com.uos.databases.domain;

/**
 * 评论实体类
 */
public class Comment {
    private Integer id;
    private String content;
    private String author;
    private Integer aId;

    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", content=‘" + content + ‘\‘‘ +
                ", author=‘" + author + ‘\‘‘ +
                ", aId=" + aId +
                ‘}‘;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Integer getaId() {
        return aId;
    }

    public void setaId(Integer aId) {
        this.aId = aId;
    }
}

Comment

package com.uos.databases.domain;

import java.util.List;

/**
 * 文章类
 */
public class Article {
    private Integer id;
    private String title;
    private String content;
    private List<Comment> commentList;

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title=‘" + title + ‘\‘‘ +
                ", content=‘" + content + ‘\‘‘ +
                ", commentList=" + commentList +
                ‘}‘;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<Comment> getCommentList() {
        return commentList;
    }

    public void setCommentList(List<Comment> commentList) {
        this.commentList = commentList;
    }
}

Article

二、创建xml映射文件:编写对应的SQL语句

<?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.uos.databases.dao.ArticleMapper">
    <select id="selectArticle" resultMap="articleWithComment">
        SELECT a.*,c.id c_id,c.content c_content,c.author
        FROM t_article a,t_comment c
        WHERE a.id=c.a_id AND a.id = #{id}
    </select>

    <update id="updateArticle" parameterType="Article" >
        UPDATE t_article
        <set>
            <if test="title !=null and title !=‘‘">
                title=#{title},
            </if>
            <if test="content !=null and content !=‘‘">
                content=#{content}
            </if>
        </set>
        WHERE id=#{id}
    </update>

    <resultMap id="articleWithComment" type="Article">
        <id property="id" column="id" />
        <result property="title" column="title" />
        <result property="content" column="content" />
        <collection property="commentList" ofType="Comment">
            <id property="id" column="c_id" />
            <result property="content" column="c_content" />
            <result property="author" column="author" />
        </collection>
    </resultMap>

</mapper>

三、在全局文件中配置xml映射文件以及实体类别名映射路径

application.properties

spring.datasource.url=jdbc:mysql://192.168.152.120:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1

spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=20
spring.datasource.minIdle=10
spring.datasource.maxActive=100

#配置MyBatis的xml配置文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
#配置XML映射文件中指定的实体类别名路径
mybatis.type-aliases-package=com.uos.databases.domain

四、编写测试方法进行接口方法测试及整合测试

package com.uos;

import com.uos.databases.dao.ArticleMapper;
import com.uos.databases.domain.Article;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DatabasesApplicationTests {
    @Autowired
    private ArticleMapper articleMapper;
    @Test
    public void selectArticle() {
        Article article = articleMapper.selectArticle(1);
        System.out.println(article);
    }

    @Test
    void contextLoads() {
    }

}

原文地址:https://www.cnblogs.com/my-program-life/p/12028861.html

时间: 2024-08-01 12:00:11

Spring Boot使用配置文件方式整合MyBatis的相关文章

spring boot 1.5.4 整合 mybatis(十二)

上一篇:spring boot 1.5.4 整合log4j2(十一) Spring Boot集成Mybatis 更多更详细的配置参考文件:application.properties和<SpringBoot之application配置详解>(新版本新增属性缺失)  或参考官网http://projects.spring.io/spring-boot/ Spring Boot集成Mybatis有两种方式: 方式一:传统的引入外部资源配置的方式,方便对mybatis的控制: 方式二:mybatis

Spring Boot学习记录(三)--整合Mybatis

Spring Boot学习记录(三)–整合Mybatis 标签(空格分隔): spring-boot 控制器,视图解析器前面两篇都已弄好,这一篇学习持久层框架整合. 1.数据源配置 数据源使用druid,maven引入相关依赖,包括spring-jdbc依赖,mysql依赖 1.转换问题 配置的过程要学会为什么这样配置,而不是只学会了配置.这里我们可以和以前的配置方式对比: 以前版本 <!--配置数据库连接池Druid--> <bean id="dataSource"

Spring Boot框架 - 数据访问 - 整合Mybatis

一.新建Spring Boot项目 注意:创建的时候勾选Mybatis依赖,pom文件如下 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> 二.配置文件applica

spring boot 1.5.4 整合 druid(十三)

上一篇:spring boot 1.5.4 整合 mybatis(十二) 1      集成druid连接池 spring boot集成druid项目mybatis-spring-boot源码地址: https://git.oschina.net/wyait/springboot1.5.4.git 1.1  druid简介 Druid是阿里巴巴开源的一个项目.,整个项目由数据库连接池.插件框架和SQL解析器组成.该项目主要是为了扩展JDBC的一些限制,可以让程序员实现一些特殊的需求,比如向密钥服

spring boot 1.5.4 整合log4j2(十一)

上一篇:spring boot 1.5.4 定时任务和异步调用(十) Spring Boot整合log4j2 spring boot整合log4j2项目spring-boot-jsp源码: https://git.oschina.net/wyait/springboot1.5.4.git 1.1  log4j2概要 对于我们开发人员来说,日志记录往往不被重视.在生产环境中,日志是查找问题来源的重要依据.日志可记录程序运行时产生的错误信息.状态信息.调试信息和执行时间信息等多种多样的信息.可以在程

Spring Boot (七): Mybatis极简配置

Spring Boot (七): Mybatis极简配置 1. 前言 ORM 框架的目的是简化编程中的数据库操作,经过这么多年的发展,基本上活到现在的就剩下两家了,一个是宣称可以不用写 SQL 的 Hibernate ,一个是对 SQL 非常友好的 Mybaties ,,两者各有特点,在企业级系统开发中可以根据需求灵活使用.发现一个有趣的现象:传统企业大都喜欢使用 Hibernate ,互联网行业通常使用 Mybatis . Hibernate 特点就是所有的 SQL 都用 Java 代码来生成

Spring Boot (八): Mybatis 增强工具 MyBatis-Plus

1. 简介 在上一篇文章<Spring Boot (七): Mybatis极简配置> 中我们介绍了在 Spring Boot 中 Mybatis 的基础使用方式,其中有一部分美中不足的是 Mybatis 本身并未提供分页功能,还需要我们自己手动添加 PageHelper 插件或者自己实现分页的工具类,并且对单表的操作并不友好,简单的 insert . update . delete 还需我们添加 SQL 语句,在目前微服务的架构模式下,每个服务拥有自己的单独的数据库,单表的使用场景会越来越多,

springmvc3.2+spring+hibernate4全注解方式整合(一)

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.

Spring Boot 2.X(五):MyBatis 多数据源配置

前言 MyBatis 多数据源配置,最近在项目建设中,需要在原有系统上扩展一个新的业务模块,特意将数据库分库,以便减少复杂度.本文直接以简单的代码示例,如何对 MyBatis 多数据源配置. 准备 创建数据库 db_test SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for t_user -- -------------------------