Spring Boot使用配置文件方式整合MyBatis
Posted 红尘年少
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
项目结构
一、创建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; } }
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; } }
二、创建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() { } }
以上是关于Spring Boot使用配置文件方式整合MyBatis的主要内容,如果未能解决你的问题,请参考以下文章
❤️Spring从入门到大神--Spring整合MyBatis
springboot整合mybatis使用xml实现sql语句的配置,在insert之后返回自增id