记录一次springboot整合mybatis的xml方式

java 1.8.0_231

因为刚开始学mybatis的时候图省事,看着xml这么多的配置文件就头大,然后就选择了使用注解的方法,但是后来做项目的时候,发现全注解的方式对于动态SQL语句的支持是真的一言难尽,于是这时候就想起了xml的好处,经过一番周折终于配置好了,这篇随笔就记录一下需要注意的地方

首先这个是项目目录,其中有一个result/Result是用于定义响应码code来负责前后端分离用的,这里先不做讨论

application.properties文件

mybatis.mapperLocations是用来指定读取数据xml文件的位置
mybatis.mapperLocations=classpath:mybatis/*.xml

spring.datasource.url=jdbc:mysql://localhost:3306/jihisea?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

pom文件的依赖

<dependencies>
        <!-- springboot web -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.2.2.RELEASE</version>
            <scope>test</scope>
        </dependency>

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

User这一个类就是建立数据实体,注意数据格式要和数据库中一一对应,先写出

int id;

String username;

String password;

然后再右键Generate选择生成Getter Setter 和 toString()

创建mapper文件夹 UserMapper接口映射文件

记得在Application上创建MapperScan扫描mapper文件夹

 UserService类中使用@Autowired注入UserMapper 然后再方法getusername中返回一个userMapper.getusername();

这样就可以在Controller中使用了

其中@Controller表示这是一个控制类  @Autowired就是将UserService注入到这个类中,@RequestMapping("/test") 就是表示请求映射 也就是localhost:8080/ 这个 " / "后面跟test 就使用该注解下的内容

@ResponseBody 表示响应实体,加了这个之后, 底下方法里返回的值就会直接显示在浏览器界面上,就比如我在下面的getusername里面定义了一个String类型的s作为userService中getusername的值,表达的是

<?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.jihisea.hexb.mapper.UserMapper" >
    <select id="getusername" resultType="String">
        select username from login where id = #{id}
    </select>
</mapper>

这个xml文件中查询id为2的 username值,返回的数据就赋予给了s  因为@ResponseBody的原因打开浏览器,

 返回hxb对应数据库中id为2的用户名,成功。

其中有一点需要注意的地方就是我发现如果在controller中

注入的是userMapper时也可以使用userMapper.getusername(2)来调用 ,感觉UserService就是进行了一遍二次包装。

原文地址:https://www.cnblogs.com/Hexiaobin/p/12217274.html

时间: 2024-08-30 10:54:39

记录一次springboot整合mybatis的xml方式的相关文章

SpringBoot整合Mybatis(yml方式)

本来打算写个使用Sharding-JDBC的例程,但是在搭建Mybatis的过程中,一波三折,因为好久没搭建项目了,另外加上换了电脑.所以很破折,在这里记录一下Spring Boot整合Mybatis吧.可能很简单,但是我长时间没用忘记了,我这里备忘一下吧. 一.项目目录结构 注意这里Application文件的位置,它是与controller.entity.mapper.service等包处于并列的关系. 二.数据库文件 SET NAMES utf8mb4; SET FOREIGN_KEY_C

springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件

整合mybatis实在前面项目的基础上进行的,前面项目具体整合请参照springboot使用之一. 一.整合mybatis 整合mybatis的时候可以从mybatis官网下载mybatis官网整合的项目mybatis-spring-boot-start.对原理感兴趣可以研究2一下,这里只叙述步骤. 引入依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybati

springboot整合mybatis(xml+注解)

写在前面 ? 刚毕业的第一份工作是java开发,项目中需要用到mybatis,特此记录学习过程,这只是一个简单demo,mybatis用法很多不可能全部写出来,有更复杂的需求建议查看mybatis的官方中文文档,点击跳转.下面时项目环境/版本. 开发工具:IDEA jdk版本:1.8 springboot版本:2.03 其他依赖版本见下面pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project

springboot整合mybatis+oracle

第一步 认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合. 创建springboot项目:通过选择springinit初始化springboot,我们发现它的pom.xml拥有绝大部分的spring所需要的包. 第二步 打开项目的结构,发现有了有趣的部分 在原有的java结构上,公司名称下多了一级叫做自己的项目名的一个目录,这个目录下才是相应的Controlller等层,而且在此目录下面有了一个文件

SpringBoot 整合MyBatis案例详解

SpringBoot约定大于配置的特点,让框架的配置非常简洁,与传统的SSM框架相比,简化了大量的XML配置,使得程序员可以从繁琐的配置中脱离出来,将精力集中在业务代码层面,提高了开发效率,并且后期的维护成本也大大降低. 从源码中可以看到,每一个springboot集成的jar包也是一个maven的子module,springboot已经将相关依赖的jar包自动添加到工程中,不需要开发人员手动去添加了,这也是springboot能够简化配置的一个重要原因. 下面开始说明springboot是如何

练习小记2:SpringBoot整合MyBatis

本文主要介绍SpringBoot整合MyBatis的初步过程以及需要注意的细节. SpringBoot简介 官网https://spring.io/projects/spring-boot/ ? MyBatis简介 官网:https://mybatis.org/mybatis-3/zh/index.html MyBatis的优势: 历史发展 MyBatis是由Ibatis发展而来的,Ibatis1.x和Ibatis2.x,都称为Ibatis,在Ibatis3.x版本及以后都称为:MyBatis

SpringBoot 2.SpringBoot整合Mybatis

一.创建Springboot的配置文件:application.properties SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序: 当前目录下的/config子目录中 当前目录中 一个 classpath 包下的 /config 目录中 classpath 根目录中 大家根据自己习惯来即可. /application.properties 文件配置如下: spring.datasourc

springboot整合mybatis(SSM开发环境搭建)

0.项目结构: 1.application.properties中配置整合mybatis的配置文件.mybatis扫描别名的基本包与数据源 server.port=80 logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-loca

SpringBoot整合Mybatis多数据源 (AOP+注解)

SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?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:s