SpringBoot整合Mybatis(简单详细)IDEA
程序员文章站
2022-05-26 10:01:02
...
1.导入依赖(包含个人工程所需)
<?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.dxf</groupId>
<artifactId>springboot_crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_crud</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--jquery http://localhost:8080/webjars/jquery/3.4.1/jquery.js-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis-generator自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</plugin>
</plugins>
</build>
</project>
2.配置文件
#添加项目名
server.servlet.context-path=/crud
#国际化属性
spring.messages.basename=i18n/login
#禁用缓存,ctrl+F9重新编译网页更改
spring.thymeleaf.cache=false
#数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=root
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/exam?serverTimezone=UTC
#配置mybatis
#别名扫描包
mybatis.type-aliases-package=com.dxf.springboot.pojo
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
3.扫描接口
package com.dxf.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.dxf.springboot.mapper")//**注意这里是重点**
@SpringBootApplication
public class SpringbootCrudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCrudApplication.class, args);
}
}
4.总结
这个例子是使用mybatis****生成的映射和接口,通过我的另一个文章https://blog.csdn.net/weixin_42408648/article/details/104020750可以结合学习。如果出现Invalid bound statement (not found): com.dxf.springboot.mapper.UserMapper.selectByPrimaryKey错误说明mapper没有交给springboot管理,请检查是否与我上面书写一致。或者检查你的xml映射文件<mapper namespace="com.dxf.springboot.mapper.ClazzMapper">
是否和接口位置对应。
上一篇: AxureUX
推荐阅读
-
IDEA搭建Springboot+SpringMVC+Mybatis+Mysql(详细、易懂)
-
详解mall整合SpringBoot+MyBatis搭建基本骨架
-
Spring Boot入门系列八(SpringBoot 整合Mybatis)
-
荐 使用IDEA搭建一个简单的JavaWeb图书管理项目(详细步骤指导、提供源码)
-
SpringBoot系列-整合Mybatis(XML配置方式)
-
SpringBoot系列-整合Mybatis(注解方式)
-
SpringBoot + Mybatis 增删改查实例教程详细解读
-
SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用
-
springboot+mybatis日志显示SQL的最简单方法
-
SpringBoot无废话入门04:MyBatis整合