为什么要学习MyBatis

1.传统的JDBC存在硬编码问题(驱动地址、密码、sql语句很麻烦)

2.访问数据的步骤繁琐(set和get)

MyBatis是什么

1.MyBatis是一款优秀的持久层框架

2.MyBatis免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作

3.MyBatis可以通过简单的XML或注解来配置和映射

4.POJO(简单的JavaBean)

MyBatis的入门

1.建立数据库和表。并初始化表中的记录

DROP DATABASE
IF EXISTS mybatis;CREATE DATABASE mybatis;USE mybatis;CREATE TABLE student (sid INT PRIMARY KEY auto_increment,sname VARCHAR (64) NOT NULL,note VARCHAR (255) NOT NULL
);INSERT INTO student (sname, note)
VALUES
('颜回','一箪食,一瓢饮,在陋巷,人不堪其忧,回也不改其乐;不迁怒不贰过');INSERT INTO student (sname, note)
VALUES('闵损','二十四孝之芦衣顺母;母在一子寒,母去三子单');INSERT INTO student (sname, note)
VALUES('冉伯牛','不幸患麻风病,端正正派,善于待人接物');INSERT INTO student (sname, note)
VALUES('仲弓','冉雍出身贫贱,但气量宽宏,沉默厚重');INSERT INTO student (sname, note)
VALUES('冉求','冉求个性谨慎,做事畏缩;力不足者,中道而废。今女画');INSERT INTO student (sname, note)
VALUES('子路','生性豪爽,为人耿直,有勇力才艺,闻过则喜');INSERT INTO student (sname, note)
VALUES('子贡','子贡与子路一文一武,犹如孔子的左右手,尤善经商,首富,但不是君子,因为君子不器');INSERT INTO student (sname, note)
VALUES('子我','口齿伶俐,能说善辩,被孔子骂朽木不可雕也');INSERT INTO student (sname, note)
VALUES('子游','割鸡焉用牛刀典故主角,割鸡焉用牛刀;君子学道则爱人,小人学道则易使也');INSERT INTO student (sname, note)
VALUES('子夏','学而优则仕提出者;巧笑倩兮,美目盼兮,素以为绚兮’,何谓也?”子曰:“绘事后素');SELECT*
FROMstudent;

2.搭建开发环境(基于Maven)

1.四个依赖包:junit、mysql-connector-java、mybatis、log4j

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.3</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency></dependencies>

2.编写数据库的配置文件:mybatis-config.xml(写在resources目录下)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//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.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><mappers><mapper resource="StudentMapper.xml"/></mappers>
</configuration>

3.编写sql语句的映射文件(StudentMapper.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.lanqiao.StudentMapper"><select id="selectStudent" resultType="org.lanqiao.Student">select * from student where sid = #{id}</select>
</mapper>

4.编写接口(对应的是映射文件中的namespace属性的值)

package org.lanqiao;public interface StudentMapper {Student selectStudent(Integer sid);
}

5.编写POJO(javaBean)

package org.lanqiao;public class Student {private Integer sid;private String sname;private String note;@Overridepublic String toString() {return "Student{" +"sid=" + sid +", sname='" + sname + '\'' +", note='" + note + '\'' +'}';}public Student() {}public Student(Integer sid, String sname, String note) {this.sid = sid;this.sname = sname;this.note = note;}public Integer getSid() {return sid;}public void setSid(Integer sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public String getNote() {return note;}public void setNote(String note) {this.note = note;}
}

6.编写测试文件

package org.lanqiao;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;/*** Hello world!**/
public class App
{public static void main( String[] args ) throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);try (SqlSession session = sqlSessionFactory.openSession()) {Student stu = (Student) session.selectOne("org.lanqiao.StudentMapper.selectStudent", 2);System.out.println(stu);}}
}

7.效果如下

MyBatis入门配置(2022.7.16)相关推荐

  1. mybatis入门配置——基于xml配置

    1.下载mybatis配置文件,以及数据库驱动文件,最好配置日志文件log4j可以方便测试查看日志: mybatis配置文件以及log4j日志配置下载链接: http://download.csdn. ...

  2. MyBatis入门——分页

    MyBatis入门--分页 一.使用limit分页 二.RowBounds Tips:请先阅读: 一.MyBatis入门--第一个MyBatis项目搭建文章 https://blog.csdn.net ...

  3. mybatis实战:一、mybatis入门(配置、一些问题的解决)

    出自<MyBatis从入门到精通>刘增辉,精简 1.pom.xml 1.设置源码编码方式为 UTF -8 2.设置编译源代码的 JDK 版本 3.添加mybatis依赖 4.还需要添加会用 ...

  4. mybatis入门之XML配置 idea版(含目录结构与sql语句)

    mybatis入门之XML配置 maven目录结构 1.创建一个maven项目 2. 配置pom文件 3.创建数据库及数据库表 4.java类和接口 4.1 User.java实体类 4.2.User ...

  5. Mybatis快速配置-不适合入门

    Mybatis 文章目录 Mybatis 1.什么是Mybatis 2.快速入门 2.1.db.properties 2.2. log4j.properties 2.3.MybatisConfig.x ...

  6. Spring Boot入门系列(六)Spring Boot如何使用Mybatis XML 配置版【附详细步骤】

    前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...

  7. MyBatis(1):MyBatis入门

    MyBatis是什么 MyBatis是什么,MyBatis的jar包中有它的官方文档,文档是这么描述MyBatis的: MyBatis is a first class persistence fra ...

  8. 01.MyBatis入门

    MyBatis入门: 第一天接触Mybatis,总结一下入门案例的流程: 首先导入Mybatis的jar包和数据库的驱动包 1.创建数据表和实体类 2.创建一个表和实体类映射的xml配置文件,具体配置 ...

  9. Spring Boot Mybatis入门示例

    Spring Boot Mybatis 入门示例 基于Spring Boot 2.3.4,Junit5 步骤说明     整个工程的最终目录结构如下,添加文件或者新建的目录的参考: └─src├─ma ...

最新文章

  1. Java IO: 网络
  2. java io 文件是否存在,代码实例Java IO判断目录和文件是否存在
  3. QT实现带有阴影的渲染3D场景
  4. python中parse是什么_python中的configparse学习笔记
  5. C#下的两种加密方式MD5和DEC
  6. 不止代码:合唱队列(动态规划)
  7. jboss入门_JBoss Forge NetBeans集成–入门
  8. leetcode面试题 10.05. 稀疏数组搜索(二分法)
  9. jQuery插件Label Effect制作个性化的文字特效
  10. 英特尔FPGA技术大会: 加快塑造边缘、网络和云端的未来
  11. ASP.NET的ADO(ActiveX Data Objects)
  12. linux下 java 压缩文件夹,java压缩文件夹linux下乱码问题
  13. qt禁止拖动_[Qt]QMdiArea,无框架窗口的拖动
  14. F#基础教程 unit类型
  15. 我的时时在线电脑(千脑)
  16. 计算机网络基础ios指令,IOS快捷指令制作真正的贴吧每天全自动签到
  17. android svg 线条动画教程,简单的SVG线条动画
  18. Spring Boot 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统...
  19. cache和register的区别
  20. 关于Kurento 和 WebRTC-Kurento学习(一)

热门文章

  1. 数字工厂管理系统的应用领域有哪些
  2. 开源优测-积微速成计划历次任务及总结
  3. C# 模拟鼠标移动和点击(转载)
  4. DNS查询的命令行工具
  5. 使用Linux训练LoRA模型
  6. 人体感应(红外感应)
  7. form表单如何不直接提交?
  8. php pdo 事物类,一个基于PDO的数据库操作类(新) 一个PDO事务实例
  9. linux- 日志管理
  10. 灵活替换、无惧缺芯,ARM工控板中的模块化设计