首页 > 其他分享 >二、集成MyBatis

二、集成MyBatis

时间:2023-07-19 14:12:26浏览次数:22  
标签:集成 StudentMapper boot mybatis import MyBatis org com

1. 创建新模块

 

添加依赖

1.1. 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.7.13</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.wzh</groupId>
	<artifactId>_2_mybatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>_2_mybatis</name>
	<description>_2_mybatis</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.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.3.1</version>
		</dependency>
		<!--lombok-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter-test</artifactId>
			<version>2.3.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<resources>
			<!--编译时,扫描src/main/java下所有xx.xml文件-->
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

⚠️注意:build时需要将mapper文件夹下的xml文件构建进去

 

1.2. 环境准备

1.2.1. 数据库

创建student表

create table student
(
    id   int auto_increment comment '唯一标识id'
        primary key,
    name varchar(30) not null comment '姓名',
    age  int         not null comment '年龄'
)
    collate = utf8mb3_bin;

1.2.2. 数据库连接配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot
    username: root
    password: root

 

1.2.3. 创建vo

package com.wzh._2_mybatis.vo;

import lombok.Data;

@Data
public class StudentVo {
    private Integer id;
    private String name;
    private Integer age;
}

 

2. 创建mybatis相关类

2.1. 方式一(不推荐)

2.1.1. 项目结构

 

2.1.2. StudentMapper类

package com.wzh._2_mybatis.mapper;

import com.wzh._2_mybatis.vo.StudentVo;
import org.apache.ibatis.annotations.Mapper;

/**
 * @Mapper注解告知mybatis框架创建此接口的动态代理类,并交由spring容器管理
 * 相当于@MpperScan + @Repository
 */
@Mapper
public interface StudentMapper {
    StudentVo getStudentById(Integer id);
}

 

2.1.3. StudentMapper.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.wzh._2_mybatis.mapper.StudentMapper">
    <resultMap type="com.wzh._2_mybatis.vo.StudentVo" id="StudentMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="age" column="age" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="getStudentById" resultMap="StudentMap">
        select
            id, name, age
        from student
        where id = #{id}
    </select>
</mapper>

 

2.1.4. ⚠️注意

  1. pom文件中添加build标签,包含xml文件
  2. StudentMapper.java与StudentMapper.xml文件必须在同一个包中且名称相同

 

2.2. 方式二(推荐)

2.2.1. 项目结构

 

2.2.2. 配置文件

# 指定mapper.xml文件位置
mybatis:
  mapper-locations: mapper/*Mapper.xml

 

2.2.3. 启动类

package com.wzh._2_mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.wzh._2_mybatis.mapper") // 指定Mapper接口包位置
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

 

2.2.4. StudentMapper类

package com.wzh._2_mybatis.mapper;

import com.wzh._2_mybatis.vo.StudentVo;

//@Mapper 有@MapperScan注解来指定Mapper接口位置
public interface StudentMapper {
    StudentVo getStudentById(Integer id);
}

 

2.2.5. ⚠️注意

  1. pom文件中无需添加build标签
  2. 由配置文件中指定mapper.xml文件位置
  3. 启动类上添加@MapperScan注解指定Mapper接口包位置
  4. StudentMapper接口上无需添加注解
  5. StudentMapper.java与StudentMapper.xml文件名称必须相同

 

3. 测试

3.1. 创建测试类

package com.wzh._2_mybatis;

import com.wzh._2_mybatis.mapper.StudentMapper;
import com.wzh._2_mybatis.vo.StudentVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ApplicationTests {
    @Autowired
    private StudentMapper studentMapper;

    @Test
    void test_getStudentById() {
        StudentVo student = studentMapper.getStudentById(1);
        System.out.println("student = " + student);
    }

}

 

3.2. 测试结果

以上

 

 

 

 

标签:集成,StudentMapper,boot,mybatis,import,MyBatis,org,com
From: https://www.cnblogs.com/spike007blogs/p/17565392.html

相关文章

  • mybatis 中 if else 用法
    mybaits中没有else要用chosewhenotherwise代替下面就是MyBatis中的if....else...表示方法<choose><whentest="">//...</when><otherwise>//...</otherwise></choose> 例子<selecti......
  • Mybatis中传递多个参数的4种方法
    前言现在大多项目都是使用Mybatis了,但也有些公司使用Hibernate。使用Mybatis最大的特性就是sql需要自己写,而写sql就需要传递多个参数。面对各种复杂的业务场景,传递参数也是一种学问。下面给大家总结了以下几种多参数传递的方法。方法1:顺序传参法(不推荐)#{}里面的数字代表你......
  • jdbc-plus是一款基于JdbcTemplate增强工具包,基于JdbcTemplate已实现分页、多租户、动
    ......
  • MyBatis的SQL执行结果和客户端执行结果不一致问题排查
    1.代码中MyBatiesPlus执行的结果,如下图,handle字段中U后面是C2.相同sql拷贝到客户端中查询出的结果,如下图,handle字段U后面是B 原因:有程序去更改handle,把handle改为了带有C的值,但是在报错后,程序进行了回滚总结:代码中查询的sql的结果和客户端查询的结果不一致,应考虑程序执行过......
  • 调整TFA中集成的OSW工具的配置文件
    0、OSW工具主要通过执行操作系统命令来收集系统资源使用情况,对于RAC环境,OSW工具几乎是必须部署的工具之一。分析一些比较复杂的故障根因时,如果没有OSW日志,最终很可能无法给出定论。从11.2.0.4RAC开始,自动安装的TFA工具中已经集成了OSW工具,我们只需要简单调整集成的OSW工具,即可完......
  • postgresql + mybatis 使用中需要注意的问题
    1.mybatis是完全支持postgresql的。包括空间查询。<dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><scope>runtime</scope></dependency>2.通过my......
  • Spring整合Mybatis
    一.引入Spring等相关依赖<dependencies><!--版本建议换成提示的更安全的版本--><!--mybatis插件--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId>......
  • 使用Power Automate上传附件到Dynamics 365集成的SharePoint
      在Dynamics365中使用SharePoint集成做实体的附件管理,这里不像用Annotation实体存放附件可以直接用代码直接创建Annotation记录,如果想要对外部提供接口把附件上传到SharePoint,我们可以使用PowerAutomate中的SharePoint组件来生成文件,通过HTTP流供给外部系统调用。  下......
  • 轻松应对复杂集成场景!用友U8API开发适配
    在企业上云的大趋势下,U8+全面转向互联网方向,深入融合云应用,一站式提供财务、营销、制造、采购、设计、协同、人力等领域的“端+云”服务,并通过软硬一体化、产业链协同的策略全面赋能成长型企业在技术、供应链、生产、财税、营销等领域的创新升级。为成长型企业提供基于互联网......
  • Statement not bound, 使用MybatisPlus时的SqlSessionFactory和MybatisSqlSessionFact
    最近首次在真实项目中,实践用SpingBoot整合Mybatis、MybatisPlus、Spring、多数据源等常见SSH整合问题。遇到一个难题,MybatisPlus遇到了经典的问题“Statementnotbound”。如果是Mybatis,很容易解决,扫描到Mapper接口文件和Mapper.xml文件,肯定能搞定。这次整合进了MybatisPlus,之前......