spring boot 应用mybatis
创始人
2024-03-16 02:12:27
0

Mybatis入门:

Mybatis入门_做测试的喵酱的博客-CSDN博客

 

目录

一、spring boot 应用mybatis 核心

二、举例:

2.1 背景

2.2 项目结构:

 2.3 依赖包 pom

2.4 项目配置文件application.yml

2.5 实例层entity

2.6 mybatis的mapper层

2.7 spring boot service 层

2.8 controller层

2.9 应用启动层StudentmybatisspringApplication

三、数据库字段名称与java实体类字段名称不一致 


一、spring boot 应用mybatis 核心

spring boot 应用mybatis 核心,就是把mybatis 的mapper 通过spring注入实例。

二、举例:

2.1 背景

实现一个查询的接口,返回结果为多个实例的集合。

select * from MyStudent where name = #{name}

2.2 项目结构:

相对于单纯的mybatis 使用,spring boot 应用mybatis 缺少了mybatis 的总配置文件。

 2.3 依赖包 pom


4.0.0org.springframework.bootspring-boot-starter-parent2.4.4 com.examplestudentmybatisspring0.0.1-SNAPSHOTstudentmybatisspringDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.projectlomboklombokmysqlmysql-connector-java8.0.20org.mybatis.spring.bootmybatis-spring-boot-starter2.1.4org.springframework.bootspring-boot-maven-plugin

2.4 项目配置文件application.yml

application.yml

server:port: 8080spring:datasource:url: jdbc:mysql://124.70.87.136:3306/chen?useUnicode=true&characterEncoding=UTF-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverlogging:level:com.example.studentmybatisspring.mapper: debug

里面包含项目端口信息

数据库连接信息

以及开启查看执行sql的日志。

这里设置查看日志,我们一般只设置查看对应包的日志。这里是想要查看的包名。

这个mapper就是我们mabits里面的mapper。

当我们启动项目,触发相应的sql时,就会显示对应的sql日志。

2.5 实例层entity

存放的是我们需要的实例。

ResultMsg,我们用来封装接口的返回结果。

package com.example.studentmybatisspring.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultMsg {public static final String SUCCESS_NO = "200";public static final String SUCCESS_SUCCESS = "success";private String no;private String msg;private T data;public ResultMsg(String no, String msg) {this.no = no;this.msg = msg;}public static  ResultMsg success(T data){return new ResultMsg(SUCCESS_NO,SUCCESS_SUCCESS,data);}public static  ResultMsg error(String no,String msg){return new ResultMsg(no,msg);}
}

MyStudent,是数据库里MyStudent表对应的实例。

数据库的表:

 对应表的实例MyStudent

package com.example.studentmybatisspring.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyStudent {private long id;private String name;private int age;public MyStudent(String name, int age) {this.name = name;this.age = age;}public MyStudent(String name) {this.name = name;}
}

2.6 mybatis的mapper层

接口:MyStudentMapper

package com.example.studentmybatisspring.mapper;import com.example.studentmybatisspring.entity.MyStudent;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;import java.util.List;@Repository
public interface MyStudentMapper {@Select("select * from MyStudent where name = #{name}")List findByName(String name);
}

接口:MyStudentMapper中,包含sql语句,以及sql语句对应的方法。

2.7 spring boot service 层

service接口

package com.example.studentmybatisspring.service;import com.example.studentmybatisspring.entity.MyStudent;import java.util.List;public interface MyStudentService {List findByName(String name);}

service实现类

package com.example.studentmybatisspring.service.impl;import com.example.studentmybatisspring.entity.MyStudent;
import com.example.studentmybatisspring.mapper.MyStudentMapper;
import com.example.studentmybatisspring.service.MyStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class MyStudentServiceImpl implements MyStudentService {@Autowiredprivate MyStudentMapper myStudentMapper;@Overridepublic List findByName(String name) {return myStudentMapper.findByName(name);}}

我们在spring boot service 实现类中,注入了mybatis 的 mapper接口。

2.8 controller层

MyStudentController

package com.example.studentmybatisspring.controller;import com.example.studentmybatisspring.entity.MyStudent;
import com.example.studentmybatisspring.entity.ResultMsg;
import com.example.studentmybatisspring.service.MyStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/chen")
public class MyStudentController {@Autowiredprivate MyStudentService myStudentService;@RequestMapping("/findByName")public ResultMsg> findByName(String name){List byName = myStudentService.findByName(name);return ResultMsg.success(byName);}
}

2.9 应用启动层StudentmybatisspringApplication

我们需要在项目启动层,增加mapper的扫描。

相对于单纯的mybatis 使用,spring boot 应用mybatis 缺少了mybatis 的总配置文件。

这里填写包名。@MapperScan(basePackages = 

package com.example.studentmybatisspring;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan(basePackages = "com.example.studentmybatisspring.mapper")
public class StudentmybatisspringApplication {public static void main(String[] args) {SpringApplication.run(StudentmybatisspringApplication.class, args);}}

在浏览器里访问:

在控制台显示sql日志

 

三、数据库字段名称与java实体类字段名称不一致 

同一个表,数据库的字段名称,与java实体类字段名称不一致。

如数据库中,字段名称为user_id

但是在java项目中,实体类的名称为驼峰格式,userId。

我们需要在application.yml,开启驼峰命名的映射。

 

 

 

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
AWSECS:哪种网络模式具有... 使用AWS ECS中的awsvpc网络模式来获得最佳性能。awsvpc网络模式允许ECS任务直接在V...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...