博客
关于我
(二)MyBatis学习笔记——使用映射配置文件进行CRUD、核心配置文件使用
阅读量:544 次
发布时间:2019-03-09

本文共 3054 字,大约阅读时间需要 10 分钟。

MyBatis 映射配置文件与核心配置文件指南

一、映射配置文件

映射配置文件是 MyBatis 具体实现中最重要的部分,它定义了数据访问逻辑,包含SQL 操作和数据绑定映射关系。

1. 查询

在映射配置文件的 <mapper> 标签内,编写 SQL 语句实现通过 ID 查询一行记录的功能。

调用示例:

public void selectById() throws Exception {    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);    SqlSession sqlSession = sqlSessionFactory.openSession();    Student stu = sqlSession.selectOne("StudentMapper.selectById", 3);    System.out.println(stu);    sqlSession.close();    is.close();}
2. 新增
INSERT INTO student VALUES(#{sid}, #{name}, #{age}, #{birthday});

调用示例:

public void insert() throws Exception {    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);    SqlSession sqlSession = sqlSessionFactory.openSession();    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    Student stu = new Student(5, "大王", 26, dateFormat.parse("1999-09-09"));    int res = sqlSession.insert("StudentMapper.insert", stu);    sqlSession.commit();    System.out.println(res);    sqlSession.close();    is.close();}
3. 修改
UPDATE student SET name=#{name}, age=#{age}, birthday=#{birthday} WHERE sid=#{sid}

调用示例:

public void update() throws Exception {    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);    SqlSession sqlSession = sqlSessionFactory.openSession();    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    Student stu = new Student(5, "大李", 21, dateFormat.parse("2000-01-01"));    int res = sqlSession.update("StudentMapper.update", stu);    sqlSession.commit();    if (res != 0) {        System.out.println("修改成功!");    }    sqlSession.close();    is.close();}
4. 删除
DELETE FROM student WHERE sid=#{sid}

调用示例:

public void delete() throws Exception {    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);    SqlSession sqlSession = sqlSessionFactory.openSession();    int res = sqlSession.delete("StudentMapper.delete", 5);    sqlSession.commit();    if (res != 0) {        System.out.println("删除成功!");    }    sqlSession.close();    is.close();}

二、核心配置文件

核心配置文件配置了 MyBatis 的核心设置,包括数据库环境、事务管理、连接池参数等。

1. 核心配置文件标签介绍
2. 数据库连接配置文件的引入
3. 类型别名配置

MyBatis 内置了许多常用的别名,如:| 别名 | 对应 Java 类型 ||------------|----------------------------|| string | java.lang.String || long | java.lang.Long || int | java.lang.Integer || double | java.lang.Double || boolean | java.lang.Boolean |

MyBatis 的核心配置文件主要用于配置数据库环境和资源管理,确保应用程序能够正常连接数据库,并在多个环境下工作。

转载地址:http://upusz.baihongyu.com/

你可能感兴趣的文章
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
nginx http配置说明,逐渐完善。
查看>>
Nginx keepalived一主一从高可用,手把手带你一步一步配置!
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>
Nginx Lua install
查看>>
nginx net::ERR_ABORTED 403 (Forbidden)
查看>>
vue中处理过内存泄露处理方法
查看>>
Nginx RTMP 模块使用指南
查看>>
Nginx SSL 性能调优
查看>>
Nginx SSL私有证书自签,且反代80端口
查看>>
Nginx upstream性能优化
查看>>
Nginx 中解决跨域问题
查看>>
nginx 代理解决跨域
查看>>
Nginx 做负载均衡的几种轮询策略分析
查看>>
Nginx 入门,一篇搞定!
查看>>
Nginx 利用代理转发请求示例
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
查看>>