当前位置:网站首页>简化JDBC的MyBits框架
简化JDBC的MyBits框架
2022-07-27 05:01:00 【new一个对象_】
JDBC简介
eg:(通过代码实现数据库中数据的更新、、、、)
↓
JDBC快速入门

什么是MyBits
(可通过官网链接了解MyBits详细内容)
●MyBits是一款优秀的持久层框架 ,用于简化JDBC开发
●MyBatis 是一个一流的持久性框架,支持自定义 SQL、存储过程和高级映射。MyBatis消除了几乎所有的JDBC代码和手动设置参数以及结果检索。MyBatis可以使用简单的XML或注释进行配置,并将基元,映射接口和Java POJO(普通旧Java对象)映射到数据库记录。
●官网:http://mybatis.org/mybatis-3/zh/index.html
持久层:
●负责将数据保存到数据库的那一层代码
●JavaEE三层架构:表现层(用来做页面展示)、业务层(用来做逻辑处理)、持久层(对数据进行持久化)
框架:
●框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码模型
●在框架的基础之上构建软件编写更加高效,规范、通用、可扩展
JDBC为什么需要简化(JDBC的缺点)?
MyBits如何简化繁琐缺点?
mybatis核心配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!---->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--加载SQL映射文件-->
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
SQL映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--名称空间-->
<mapper namespace="test">
<select id="selectAll" resultType="user">
select * from user
</select>
</mapper>
案例:查询当前数据库中user表中的所有信息:
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class mybatis {
public static void main(String[] args) throws IOException {
//1.加载mybatis核心配置文件,获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行sql
SqlSession sqlSession=sqlSessionFactory.openSession();
//3.执行sql
List<user>use= sqlSession.selectList("test.selectAll");
System.out.println(use);
//4.释放资源
sqlSession.close();
}
}
运行结果:
边栏推荐
- What is the future development direction of software testing engineers?
- JVM Part 1: memory and garbage collection part 7 -- runtime data area heap
- A math problem cost the chip giant $500million
- JVM Part 1: memory and garbage collection part 10 - runtime data area - direct memory
- JVM上篇:内存与垃圾回收篇八--运行时数据区-方法区
- The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay
- Mysql表的约束
- 探寻通用奥特能平台安全、智能、性能的奥秘!
- Solution to Dlib installation failure
- SSM framework integration
猜你喜欢

How to store the startprocessinstancebykey method in acticiti in the variable table

Detailed description of polymorphism

集合框架的使用

Introduction to MySQL optimization

Inspiration from "flying man" Jordan! Another "arena" opened by O'Neill

Scientific Computing Library - numpy

Mysql表的约束

2021 OWASP top 6-10 collection

Introduction to dynamic memory functions (malloc free calloc realloc)

File dialog box
随机推荐
标准对话框 QMessageBox
Derivation and explanation of PBR physical illumination calculation formula
How to sinicize the JMeter interface?
A math problem cost the chip giant $500million
MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition
Create datasource using Druid connection pool
QT menu bar, toolbar and status bar
ssm框架整合
pyside2____ 1. Installation and case listing
JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
智慧展厅设计的优势及适用行业分析
文件处理(IO)
JVM Part 1: memory and garbage collection part 8 - runtime data area - Method area
事件(event)
JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
Slashes / and backslashes involved in writing code\
TypeScript 详解
Detailed description of binary search tree
35. Scroll
Counting Nodes in a Binary Search Tree