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

你可能感兴趣的文章
python请求html_使用Python请求获取HTML?
查看>>
Python 之匿名函数和偏函数
查看>>
python 之栈的实现
查看>>
python语音播放
查看>>
python语言:装饰器原理
查看>>
Python 交互式数据可视化详解
查看>>
python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
查看>>
Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
查看>>
Python 从数据库中存储和检索密码的最安全方法
查看>>
Python语言及其应用 - 知识点遍历
查看>>
Python 优化提速的 8 个小技巧
查看>>
Python 余弦相似度与皮尔逊相关系数 计算
查看>>
python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
查看>>
python 使用filetype校验文件
查看>>
Python 使用flush函数将缓冲区数据立即写磁盘
查看>>
python 使用in判断不准确,in不好使
查看>>
Python 使用pandas 进行查询和统计详解
查看>>
Redis 配置文件redis.conf详细解释
查看>>
python网络爬虫(2)——scrapy框架的基础使用
查看>>
python网络爬虫实例教程试读_Python网络爬虫实战教程(全套完整版) - 学途无忧网 - 做技术的王者 - Powered By EduSoho...
查看>>