Mybatis超详细学习笔记(一)小白入门HelloWorld

网友投稿 623 2022-05-30

1、简介

2、入门HelloWorld

2.1、基础环境搭建

2.2、测试HelloWorld

2.3、如何再xml文件中有提示(eclipse)

1、简介

2、入门HelloWorld

2.1、基础环境搭建

2.2、测试HelloWorld

2.3、如何再xml文件中有提示(eclipse)

1、简介

Mybatis官方文档

Mybatis下载地址

什么是 MyBatis?

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

2、入门HelloWorld

2.1、基础环境搭建

1.导入jar包

2.创建数据库,数据表

建表语句

CREATE TABLE `t_employee` ( `id` int(11) NOT NULL, `empname` varchar(50) NOT NULL, `gender` int(11) DEFAULT NULL, `email` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

3.写出对应的实体类及dao

Employee

public class Employee { private Integer id; private String empName; private String email; private Integer gender; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } }

EmployeeDao

public interface EmployeeDao { //按照员工id查询员工 public Employee getEmpById(Integer id); }

4.写配置

4.1: mybatis的全局配置文件,指导mybatis如何正确运行,比如丽娜姐向那个数据库

4.2: 编写每一个方法都如何向数据库发送sql语句,如何正确执行。。像防御接口的实现类

Mybatis超详细学习笔记(一)小白入门HelloWorld

5.项目整体结构

2.2、测试HelloWorld

测试

@Test publicvoid test() throws IOException { //1、根据全局配置文件创建出一个SqlSessionFactory //SqlSessionFactory:是SqlSession工厂,负责创建SqlSession对象 //SqlSession:sql会话(代表和数据库的一次会话) String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); Employee empById; //获取和数据库的一次会话:getConnection() SqlSession openSession = sqlSessionFactory.openSession(); try { //使用SqlSession操作数据库,获取dao接口的实现 EmployeeDao mapper = openSession.getMapper(EmployeeDao.class); empById = mapper.getEmpById(1); } finally { //关闭连接 openSession.close(); } System.out.println(empById); }

2.3、如何再xml文件中有提示(eclipse)

下载好Mybatis后使用压缩软件打开里面的mybatis的jar包,如下图,解压出方框的文件

在xml文件按住Alt+/即可看见提示

觉得博主写的不错的读者大大们,可以关注和哦,谢谢各位!

MyBatis 数据库

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:2019数字中国创新大赛:文化传承汉字书法多场景识别赛题解读
下一篇:python列表高级操作——快来看看你会多少!
相关文章