博客
关于我
(二)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:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>