博客
关于我
(二)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 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>
MySQL 常见的 9 种优化方法
查看>>
MySQL 常见的开放性问题
查看>>
Mysql 常见错误
查看>>
MYSQL 幻读(Phantom Problem)不可重复读
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>