今日内容:关于 mybatis概念、介绍、快速入门

Mybatis

1. 框架

   - 框架的概念

     框架是一个半成品软件,程序员在框架的基础上,开发出一套软件

     框架解决了技术通用的问题,提升了开发效率,提升了系统稳定性

     

2. Mybatis框架

   - Mybatis框架介绍

     ```

     mybatis是Apache软件基金会下的一个开源项目,前身是iBatis框架。2010年这个项目由apache 软件基金会迁移到google code下,改名为mybatis。2013年11月又迁移到了github(GitHub 是一个面向开源及私有 软件项目的托管平台)。

        MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射(多表)。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。它对 jdbc 的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建 connection、创建 statement、手动设置参数、结果集检索等 jdbc 繁杂的过程代码。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

        mybatis官网 mybatis – MyBatis 3 | Introduction

        源码和包下载地址:https://github.com/mybatis/mybatis-3/releases

     ```

     

3. mybatis快速入门

   - 准备mysql数据库 student  表info(id,name,pw,age)

   - mybatis查询数据库操作

     - 编写依赖包的坐标信息(pom.xml)

       junit测试包:junit4.12

       mybatis包:mybatis3.4.5

       数据库驱动包:mysql-connector-java8.0.28

       日志包:log4j1.2.12

       ```     

​
 <?xml version="1.0" encoding="UTF-8"?>

       <project xmlns="http://maven.apache.org/POM/4.0.0"

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

           <modelVersion>4.0.0</modelVersion>

       

           <groupId>com.xxgc</groupId>

           <artifactId>chapter03_mybatis</artifactId>

           <version>1.0-SNAPSHOT</version>

       

           <!--项目中指定jdk11-->

           <properties>

               <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

               <maven.compiler.source>11</maven.compiler.source>

               <maven.compiler.target>11</maven.compiler.target>

           </properties>

       

           <!--导入项目中需要的依赖坐标-->

           <dependencies>

               <!--junit测试包:junit4.12-->

               <dependency>

                   <groupId>junit</groupId>

                   <artifactId>junit</artifactId>

                   <version>4.12</version>

                   <scope>test</scope>

               </dependency>

       

               <!--mybatis包:mybatis3.4.5-->

               <dependency>

                   <groupId>org.mybatis</groupId>

                   <artifactId>mybatis</artifactId>

                   <version>3.4.5</version>

               </dependency>

       

               <!--数据库驱动包:mysql-connector-java8.0.28-->

               <dependency>

                   <groupId>mysql</groupId>

                   <artifactId>mysql-connector-java</artifactId>

                   <version>8.0.28</version>

               </dependency>

       

               <!--日志包:log4j1.2.12-->

               <dependency>

                   <groupId>log4j</groupId>

                   <artifactId>log4j</artifactId>

                   <version>1.2.12</version>

               </dependency>

       

           </dependencies>

       

           <!--指定阿里云镜像下载依赖包-->

           <repositories>

               <repository>

                   <id>nexus-aliyun</id>

                   <name>nexus-aliyun</name>

                   <url>http://maven.aliyun.com/nexus/content/groups/public/</url>

                   <releases>

                       <enabled>true</enabled>

                   </releases>

                   <snapshots>

                       <enabled>false</enabled>

                   </snapshots>

               </repository>

           </repositories>

       

           <pluginRepositories>

               <pluginRepository>

                   <id>public</id>

                   <name>aliyun nexus</name>

                   <url>http://maven.aliyun.com/nexus/content/groups/public/</url>

                   <releases>

                       <enabled>true</enabled>

                   </releases>

                   <snapshots>

                       <enabled>false</enabled>

                   </snapshots>

               </pluginRepository>

           </pluginRepositories>

       </project>    

       ```

​

       

     - 编写实体类Info(基本数据类型使用其包装类)     

  ```

       package com.xxgc.pojo;

       

       public class Info {

           private Integer id;

           private String name;

           private String pw;

           private Integer age;

       

           public Integer getId() {

               return id;

           }

       

           public void setId(Integer id) {

               this.id = id;

           }

       

           public String getName() {

               return name;

           }

       

           public void setName(String name) {

               this.name = name;

           }

       

           public String getPw() {

               return pw;

           }

       

           public void setPw(String pw) {

               this.pw = pw;

           }

       

           public Integer getAge() {

               return age;

           }

       

           public void setAge(Integer age) {

               this.age = age;

           }

       

           @Override

           public String toString() {

               return "Info{" +

                       "id=" + id +

                       ", name='" + name + '\'' +

                       ", pw='" + pw + '\'' +

                       ", age=" + age +

                       '}';

           }

       }

       ```

     - 编写InfoMapper.xml配置文件

       ```       

​
<?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="infoMapper">

          <select id="selectInfo" resultType="com.xxgc.pojo.Info">

              select * from info

          </select>

       </mapper>

       ```

     - 配置Mybatis的核心配置文件sqlMapConfig.xml

       ```

       <?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">

       

       <!--mybatis核心配置-->

       <configuration>

           <!--可配置多个环境,默认环境-->

           <environments default="development">

               <environment id="development">

                   <!--事务管理配置,使用JDBC事务管理-->

                   <transactionManager type="JDBC"></transactionManager>

                   <!--连接池配置-->

                   <!--

                    POOLED:首先先判断空闲连接池内有没有空闲连接,如果还有则给你返回一个空闲连接。

                           如果没有空闲连接,则去活动连接池内看看还有没有位置,如果还有,则new一个连接给你返回

                           如果活动连接池没有位置了,则返回在活动连接池使用最久的连接。意思就是给你返回一个在活动连接池内待最久的连接。

                   -->

                   <dataSource type="POOLED">

                       <property name="driver" value="com.mysql.cj.jdbc.Driver"/>

                       <property name="url" value="jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai"/>

                       <property name="username" value="root"/>

                       <property name="password" value="root"/>

                   </dataSource>

               </environment>

           </environments>

           <!--配置映射文件infoMapper.xml-->

           <mappers>

               <mapper resource="infoMapper.xml"></mapper>

           </mappers>

       </configuration>

​

       ```     

- 测试

       ```

       package com.xxgc;

       

       import com.xxgc.pojo.Info;

       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 org.junit.Test;

       

       import java.io.IOException;

       import java.io.InputStream;

       import java.util.List;

       

       public class MybatisDemoTest {

           @Test

           public void findAllInfo() throws IOException {

               //1.读取mybatis核心配置文件sqlMapConfig.xml

               InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");

               SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);

               SqlSession sqlSession = factory.openSession();

               List<Info> list = sqlSession.selectList("infoMapper.selectInfo");

               System.out.println(list);

               sqlSession.close();

           }

       }

       ```

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值