小白mybatis学习(一)

一、介绍

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
需要的加入的依赖:

<dependencies>
 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.49</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.0</version>
    </dependency>
</dependencies>
  <build>
    <!--把Java目录中的xml文件打包到项目中-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
    </resources>
  </build>

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jb:mysql://localhost:3306/student?useUnicode=true&useSSL=true
jdbc.username=root
jdbc.password=123456

在这里插入图片描述

config.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">


<configuration>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <environments default="online">
        <!--唯一值 id  -->
        <environment id="mydev">
            <!-- 数据源 -->
            <transactionManager type="JDBC"/>
            <!-- 连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>


        <environment id="online">
            <!-- 数据源 -->
            <transactionManager type="JDBC"/>
            <!-- 连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/student?serverTimezone=UTC&amp; characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>

    </environments>

    <mappers>
        <mapper resource="com/sdyu/dao/StudentDao.xml"/>
    </mappers>

</configuration>

java主类

//访问mybatis读取student数据
//1.定义mybatis主配置文件名称,从类路径的根开始(target/classes)
String config = "mybatis.xml";
//2.读取这个config表示的文件
InputStream in = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactoryBuilder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//4.创建SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//5.获取SqlSession对象,从SqlSessionFactory中获取SqlSession
SqlSession sqlSession = factory.openSession();
//6.指定要执行的sql语句的标识,sql映射文件中的namespace + "."+标签的id值
String sqlId = "com.sdyu.dao.StudentDao"+"."+"findStudent";
//7.执行sql语句,通过sqlId找到语句
List<Student> studentList = sqlSession.selectList(sqlId,"2");
//8.输出结果
for (Student stu : studentList) {
    System.out.println("名字:"+stu.getName()+"年龄:"+stu.getAge());
}
//9.关闭SqlSession对象
sqlSession.close();

在这里插入图片描述

主要类的介绍

1)Resources:
mybatis中的一个类,负责读取主配置文件

​ InputStream in = Resources.getResourceAsStream(config);

2)SqlSessionFactoryBuilder:
创建SqlSessionFactory对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

​ SqlSessionFactory factory = builder.build(in);

3)SqlSessionFactory:

重量级对象,程序创建一个对象耗时比较长,使用资源多

​ 在整个项目里有一个就可以。

​ SqlSessionFactory:接口

​ 接口的实现类:DefaultSqlSessionFactory

​ SqlSessionFactory作用:获取SqlSession对象

​ SqlSession sqlSession = factory.openSession();

​ openSession()方法说明:

​ 1.openSession():无参数的,获取是非自动提交事物的SqlSession对象

​ 2.openSession(boolean):openSession(true) 获取自动提交事务的SqlSession对象。

​ openSession(false) 非自动提交事务的SqlSession对象。

4)SqlSession:

SqlSession接口:定义了操作数据的方法,例如 selectOne(),selectList(),insert(),update(),delete(),commit(),rollback(),

SqlSession接口的实现类:DefaultSqlSession

使用要求:SqlSession对象不是线程安全的,需要在方法内部使用,在执行sql语句之前,使用openSession()获取SqlSession

​在执行完sql语句后,需要关闭它,执行SqlSession.close(),这样能保证他的使用是线程安全的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值