SpringBoot+Mybatis+application.yml(仅供入门参考)

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: SpringBoot+Mybatis+application.yml(仅供入门参考)

项目目录:
在这里插入图片描述
说明,本文中有两个mapper接口,分别采用xml形式和java注解形式实现。

1、项目创建完成后,pom.xml文件配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>
<dependencies>
    <!-- springboot 整合web组件 整合springmvc-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.29</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
    </dependency>
</dependencies>

2、application.yml配置

spring:
  datasource:
    #   数据源基本配置
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo
    type: com.alibaba.druid.pool.DruidDataSource

server:
  port: 8888

mybatis:
#在yml文件中做了以下配置就无需做mybatis-config.xml配置,如果使用了mybatis-config.xml配置,就把下面两行注掉,将第三行配置打开
  mapper-locations: classpath:mybatis/mappers/*.xml
  type-aliases-package: com.example.mybatis.entity
  #config-location: classpath:mybatis-config.xml

3、在 resources 下创建个 mybatis-config.xml 内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--设置-->
    <settings>
        <!-- 使用全局的映射器启用或者禁用缓存。 -->
        <setting name="cacheEnabled" value="true"/>
        <!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <!--  设置驱动等待数据响应的超时数  默认没有设置-->
        <setting name="defaultStatementTimeout" value="30000"/>
        <!-- 使用驼峰命名法转换字段。 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
        <setting name="useGeneratedKeys" value="true"/>
    </settings>
    <!--别名 配置别名后不区分大小写-->
    <typeAliases>
        <typeAlias alias="person" type="com.example.mybatis.entity.Person"/>
    </typeAliases>
    <!--映射器-->
    <mappers>
        <mapper resource="mappers/PersonMapper.xml"/>
    </mappers>
</configuration>

4、在entity中创建实体类Deptment和Person类

Person类

@Data
public class Person {

    private int id;

    private String PersonName;

    private int department_id;
}

Deptment类

@Data
public class Department {

    private int id;

    private String name;

    private int testId;

    private String data1;
    private String data2;
    private String data3;
}

5、创建Mapper,Mapper类和Mapper对应的xml,注册Mapper接口要增加注解@Mapper 如下:(Mapper接口 如果不加 @Mapper 则需要在启动类上增加 @MapperScan("mapper接口所在包名") )

其中DeptMapper接口,采用注解实现

@Mapper
public interface DeptMapper {

    @Select("select * from department where id=#{id}")
    Department getDepById(int id);

    @Delete("delete from department where id=#{id}")
    int deleteDepById(int id);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into department(name) values(#{name})")
    int insertDept(Department department);

    @Update("update department set name=#{name} where id=#{id}")
    int updateDept(Department department);
}

PersonMapper接口采用xml实现,

@Mapper
public interface PersonMapper {

     Person getPersonById(int id);

     String insertPerson(Person person);

}

PersonMapper.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.example.mybatis.mapper.PersonMapper">

    <select id="getPersonById" parameterType="int" resultType="person">
         select * from person where id = #{id}
    </select>

    <insert id="insertPerson">
        insert into person(PersonName,department_id) values (#{PersonName},#{department_id})
    </insert>
    <update id="updatePerson">

    </update>
</mapper>

6、创建Controller 定义访问接口,如下:

PersonController

@RestController
public class PersonController {

    @Autowired
    PersonMapper personMapper;

    @GetMapping("/per/{id}")
    public Person getPersonById(@PathVariable int id){
        return personMapper.getPersonById(id);
    }
}

DepController

@RestController
public class DepController {

    @Autowired
    DeptMapper deptMapper;

    @GetMapping("/dept/{id}")
    public Department getDeptMapper(@PathVariable int id) {
        return deptMapper.getDepById(id);
    }

    @GetMapping("/dept")
    public Department insertDeptMapper( Department department){
        System.out.println("准备进行插入数据操作!");
        deptMapper.insertDept(department);
        return department;
    }
}

6、定义启动类,注意此类需要多增加一个注解@MapperScan("mapper类所在包名"),如下:(如果启动类增加@MapperSscan 那么 Mapper接口上则可不用加 @Mapper)

@MapperScan(value = "com.example.mybatis.mapper")
@SpringBootApplication
public class MybatisApplication {

    public static void main(String[] args) {

        SpringApplication.run(MybatisApplication.class, args);
        System.out.println("启动成功......");
    }

}

8、运行正常
在这里插入图片描述

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
22天前
|
缓存 前端开发 JavaScript
前后端分离 SpringBoot+Vue商城买卖系统通杀版本。大家可以参考学习一下
这篇文章介绍了一个使用SpringBoot+Vue开发的前后端分离商城系统,包括技术架构、开发环境、实现的功能以及项目截图,并展示了普通用户和商家端的功能界面。
前后端分离 SpringBoot+Vue商城买卖系统通杀版本。大家可以参考学习一下
|
22天前
|
Java 数据库连接 Spring
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
文章是关于Spring、SpringMVC、Mybatis三个后端框架的超详细入门教程,包括基础知识讲解、代码案例及SSM框架整合的实战应用,旨在帮助读者全面理解并掌握这些框架的使用。
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
|
3天前
|
前端开发 JavaScript Java
技术分享:使用Spring Boot3.3与MyBatis-Plus联合实现多层次树结构的异步加载策略
在现代Web开发中,处理多层次树形结构数据是一项常见且重要的任务。这些结构广泛应用于分类管理、组织结构、权限管理等场景。为了提升用户体验和系统性能,采用异步加载策略来动态加载树形结构的各个层级变得尤为重要。本文将详细介绍如何使用Spring Boot3.3与MyBatis-Plus联合实现这一功能。
19 2
|
13天前
|
Java 数据库连接 测试技术
SpringBoot 3.3.2 + ShardingSphere 5.5 + Mybatis-plus:轻松搞定数据加解密,支持字段级!
【8月更文挑战第30天】在数据驱动的时代,数据的安全性显得尤为重要。特别是在涉及用户隐私或敏感信息的应用中,如何确保数据在存储和传输过程中的安全性成为了开发者必须面对的问题。今天,我们将围绕SpringBoot 3.3.2、ShardingSphere 5.5以及Mybatis-plus的组合,探讨如何轻松实现数据的字段级加解密,为数据安全保驾护航。
49 1
|
22天前
|
Web App开发 前端开发 关系型数据库
基于SpringBoot+Vue+Redis+Mybatis的商城购物系统 【系统实现+系统源码+答辩PPT】
这篇文章介绍了一个基于SpringBoot+Vue+Redis+Mybatis技术栈开发的商城购物系统,包括系统功能、页面展示、前后端项目结构和核心代码,以及如何获取系统源码和答辩PPT的方法。
|
22天前
|
SQL Java 关系型数据库
SpringBoot 系列之 MyBatis输出SQL日志
这篇文章介绍了如何在SpringBoot项目中通过MyBatis配置输出SQL日志,具体方法是在`application.yml`或`application.properties`中设置MyBatis的日志实现为`org.apache.ibatis.logging.stdout.StdOutImpl`来直接在控制台打印SQL日志。
SpringBoot 系列之 MyBatis输出SQL日志
|
27天前
|
Java 关系型数据库 MySQL
1、Mybatis-Plus 创建SpringBoot项目
这篇文章是关于如何创建一个SpringBoot项目,包括在`pom.xml`文件中引入依赖、在`application.yml`文件中配置数据库连接,以及加入日志功能的详细步骤和示例代码。
|
6天前
|
Java 数据库连接 开发者
MyBatis-Plus整合SpringBoot及使用
MyBatis-Plus为MyBatis提供了强大的增强,使得在Spring Boot项目中的数据访问层开发变得更加快捷和简便。通过MyBatis-Plus提供的自动CRUD、灵活的查询构造器和简洁的配置,开发者
20 0
|
29天前
|
前端开发 Java 数据库连接
一天十道Java面试题----第五天(spring的事务传播机制------>mybatis的优缺点)
这篇文章总结了Java面试中的十个问题,包括Spring事务传播机制、Spring事务失效条件、Bean自动装配方式、Spring、Spring MVC和Spring Boot的区别、Spring MVC的工作流程和主要组件、Spring Boot的自动配置原理和Starter概念、嵌入式服务器的使用原因,以及MyBatis的优缺点。
|
28天前
|
Java 数据库连接 mybatis
基于SpringBoot+MyBatis的餐饮点餐系统
本文介绍了一个基于SpringBoot和MyBatis开发的餐饮点餐系统,包括系统的主控制器`IndexController`的代码实现,该控制器负责处理首页、点餐、登录、注册、订单管理等功能,适用于毕业设计项目。
42 0
基于SpringBoot+MyBatis的餐饮点餐系统