SpringBoot中Mybatis的使用
首先,我们需要在项目的pom.xml中引入mybatis的依赖和Druid连接池依赖
<?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Lirs</groupId>
<artifactId>Spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring</name>
<description>SpringBoot学习框架</description>
<properties>
<java.version>1.8</java.version>
<mysql.version>5.0.8</mysql.version>
</properties>
<!--SpringBoot集成web服务-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot测试-->
<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>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--alibabaDruid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

本文介绍了如何在SpringBoot项目中整合Mybatis和JdbcTemplate。首先讲解了引入Mybatis和Druid连接池的依赖,以及Druid的监控配置和使用。接着展示了如何创建表、实体类和Mapper,分别用注解和XML方式实现动态SQL。此外,还介绍了Springboot中JdbcTemplate的使用,包括返回集合的两种方式,并提醒了使用JdbcTemplate时应注意的泛型问题。最后强调了良好的编程习惯对程序可读性和维护性的重要性。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



