广告
返回顶部
首页 > 资讯 > 数据库 >【SpringBoot】| ORM 操作 MySQL(集成MyBatis)
  • 118
分享到

【SpringBoot】| ORM 操作 MySQL(集成MyBatis)

mysqlspringbootjava 2023-09-12 15:09:12 118人浏览 安东尼
摘要

目录 一:ORM 操作 MySQL  1. 创建 Spring Boot 项目 2. @MapperScan 3. mapper文件和java代码分开管理 4. 事务支持 一:ORM 操作 Mysql  使用mybatis框架操作数

目录

一:ORM 操作 MySQL 

1. 创建 Spring Boot 项目

2. @MapperScan

3. mapper文件和java代码分开管理

4. 事务支持


一:ORM 操作 Mysql 

使用mybatis框架操作数据, 在SpringBoot框架集成MyBatis,使用步骤:

(1)mybatis起步依赖 : 完成mybatis对象自动配置, 对象放在容器

(2)pom.xml 指定把src/main/java目录中的xml文件包含到classpath中

(3)创建实体类Student

(4)创建Dao接口 StudentDao , 创建一个查询学生的方法

(5)创建Dao接口对应的Mapper文件, xml文件, 写sql语句

(6)创建Service层对象, 创建StudentService接口和它的实现类。 去dao对象的方法,完成数据库的操作

(7)创建Controller对象,访问Service。

(8)写application.properties文件,配置数据库的连接信息。

1. 创建 Spring Boot 项目

(1)准备数据库表

字段及其类型

 插入数据

 (2)创建一个springBoot项目

选择Spring WEB依赖

MybatisFramework依赖、mysql Driver依赖

(3)生成的pom.xml配置和手动添加的resource插件配置

注:resource插件配置是表示将src/java/main下的或者说子包下的*.xml配置文件最终加载到target/classes目录下。

    4.0.0            org.springframework.boot        spring-boot-starter-parent        2.7.9                com.zl    study-springboot-mysql    0.0.1-SNAPSHOT            1.8                                    org.springframework.boot            spring-boot-starter-web                                    org.mybatis.spring.boot            mybatis-spring-boot-starter            2.3.0                                    com.mysql            mysql-connector-j            runtime                                    org.springframework.boot            spring-boot-starter-test            test                                                                            src/main/java                                                    ***.properties          ***.xml             ***.xml             **/*.properties           

 generatorConfig.xml配置

                                                                                                                                                                                                                                                                                                    

双击插件,执行结果如下:

第三步:编写application.properties配置

注:如果StudentMapper.xml的目录与StudentMapper目录保持一致,就不需要以下这个配置mybatis.mapper-locations=classpath:mapper/*.xml;这里我们是自己定义的mapper目录,把mapper.xml文件放进去了,所以需要我们指定出来它的位置!

#设置端口server.port=8082#配置项目根路径context-pathserver.servlet.context-path=/myboot#配置数据库spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8spring.datasource.username=rootspring.datasource.passWord=123#配置mybatismybatis.mapper-locations=classpath:mapper/*.xml#配置日志mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

第四步:编写service接口和实现类

StudentService接口

package com.zl.service;import com.zl.pojo.Student;public interface StudentService {    int addStudent(Student student);}

StudentService接口的实现类StudentServiceImpl

package com.zl.service.impl;import com.zl.mapper.StudentMapper;import com.zl.pojo.Student;import com.zl.service.StudentService;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;@Service // 交给Spring容器管理public class StudentServiceImpl implements StudentService {    @Resource // 属性赋值    private StudentMapper studentDao;    @Transactional // 事务控制    @Override    public int addStudent(Student student) {        System.out.println("准备执行sql语句");        int count = studentDao.insert(student);        System.out.println("已完成sql语句的执行");        // 模拟异常,回滚事务        int sum = 10 / 0;        return count;    }}

第五步:编写controller类去调用service

package com.zl.controller;import com.zl.pojo.Student;import com.zl.service.StudentService;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;@Controllerpublic class StudentController {    @Resource    private StudentService studentService;    @RequestMapping("/addStudent")    @ResponseBody    public String addStudent(String name,Integer age){        Student s = new Student();        s.setName(name);        s.setAge(age);        int count = studentService.addStudent(s);        return "添加的Student个数是:"+count;    }}

第六步:在启动类上面加上包扫描注解和启动事务管理器注解

package com.zl;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.transaction.annotation.EnableTransactionManagement;@SpringBootApplication@MapperScan(basePackages = "com.zl.mapper") // 添加包扫描@EnableTransactionManagement // 启动事务管理器public class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

第七步:进行测试

有异常发生,会回滚事务,无法插入数据

 无异常发生,正常插入数据

来源地址:https://blog.csdn.net/m0_61933976/article/details/129344765

您可能感兴趣的文档:

--结束END--

本文标题: 【SpringBoot】| ORM 操作 MySQL(集成MyBatis)

本文链接: https://www.lsjlt.com/news/404704.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql中coalesce的用法
    coalesce 函数返回指定的非空值,如果所有值均为 null,则返回指定的默认值。用法:1. 获取第一个非空值;2. 提供默认值;3. 级联检查多个值。注意事项:仅返回第一个非空值,...
    99+
    2024-04-29
  • sql中删除一列的命令
    在 sql 中,使用 alter table 语句可以删除一列,语法为:alter table table_name drop column column_name;步骤包括:① 指定要...
    99+
    2024-04-29
  • sql中修改数据的命令
    sql 中使用 update 和 delete 命令修改数据:update 命令更新现有记录,语法为:update table_name set column1 = value...
    99+
    2024-04-29
  • sql中修改表的命令
    sql 中修改表的命令有:alter table:执行各种修改表操作。add column:添加新列。drop column:删除列。modify column:修改列的类型、约束或默认...
    99+
    2024-04-29
  • 在sql中删除视图用什么命令
    sql 中用于删除视图的命令是 drop view,语法为 drop view view_name;,其中 view_name 是视图的名称。删除视图不会删除其基础表中的数据,也不应删除...
    99+
    2024-04-29
  • sql中删除一个视图的命令
    sql 中删除视图的命令为:drop view [schema_name.]view_name;。此命令将删除名为 view_name 的视图(位于模式 schema_name 中),但...
    99+
    2024-04-29
  • 在sql中as什么意思
    as 在 sql 中分配表达式、子查询或表的别名,使复杂查询结果易于理解。别名语法为:select as from ,其中 可以是列、表达式或子查询, 为分配的别名。优点...
    99+
    2024-04-29
  • sql中using是什么意思
    sql 中 using 关键字用于连接表,通过指定一个参与连接的列来指定连接条件。它只允许按一个列连接表,因此当 join 条件涉及一个列时,它提供了简洁易读的语法。 SQL 中 US...
    99+
    2024-04-29
  • mysql中删除表的语句
    mysql 中删除表的语句是:drop table table_name; 它将永久删除表及其数据,注意操作不可逆。此语句不适用于视图或临时表,应分别使用 drop view 和 dro...
    99+
    2024-04-29
    mysql
  • mysql中删除一个表的命令
    mysql 中删除表命令:drop table。语法:drop table table_name。用法:1. 连接数据库;2. 输入命令:drop table table_name;3....
    99+
    2024-04-29
    mysql
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作