博客
关于我
(二)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/

你可能感兴趣的文章
Mysql添加用户并授予只能查询权限
查看>>
mysql添加用户权限报1064 - You have an error in your SQL syntax问题解决
查看>>
mysql添加索引
查看>>
mysql添加表注释、字段注释、查看与修改注释
查看>>
mysql清理undo线程_MySQL后台线程的清理工作
查看>>
mysql清空带外键的表
查看>>
MySQL清空表数据
查看>>
mysql源码安装
查看>>
Mysql源码安装过程中可能碰到的问题
查看>>
MySQL灵魂16问,你能撑到第几问?
查看>>
MySQL灵魂拷问:36题带你面试通关
查看>>
mysql状态分析之show global status
查看>>
mysql状态查看 QPS/TPS/缓存命中率查看
查看>>
mysql生成树形数据_mysql 实现树形的遍历
查看>>
mysql用于检索的关键字_Mysql全文搜索match...against的用法
查看>>
MySQL用得好好的,为什么要转ES?
查看>>
MySql用户以及权限的管理。
查看>>
MySQL用户权限配置:精细控制和远程访问的艺术!------文章最后有惊喜哦。
查看>>
mysql用户管理、常用语句、数据分备份恢复
查看>>
MySQL留疑问:left join时选on还是where?
查看>>