美文网首页
Mybatis框架--优化过程

Mybatis框架--优化过程

作者: 欧子有话说_ | 来源:发表于2022-08-19 09:34 被阅读0次

0. 原代码预览

简单实现在数据库中插入数据

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public void testInsert() throws IOException {
//获取核心配置文件的输入流
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//获取SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//获取sql的回话对象sqlSession,是Mybatis提供的操作数据库的对象
SqlSession sqlSession = sqlSessionFactory.openSession();

    //获取UserMapper的代理实现类对象
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    //调用mapper接口中的方法,实现添加用户信息的功能
    int result = mapper.insertUser();
    System.out.println("结果:"+result);

    //提交事务
    sqlSession.commit();

    //关闭sqlSession对象
    sqlSession.close();
}</pre>
  • SqlSession:代表Java程序和数据库之间的会话。(HttpSession是Java程序和浏览器之间的会话)
  • SqlSessionFactory:是“生产”SqlSession的“工厂”。

1. 简化代码

根据两个一致性,可以将 获取UserMapper的代理实现类对象调用mapper接口中的方法 两部分代码写为:

<pre class="prettyprint hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//例如我的代码:
int result = sqlSession.insert("com.atguigu.mybatis.insertUser");//参数是:全类名+方法名。也就是映射文件中sql的id位置</pre>

2. 自动提交sql事务

每次都需要手动提交SQL事务 sqlSession.commit() 。可以在 获取sql的回话对象sqlSession 的代码: sqlSessionFactory.openSession() 加一个参数:

<pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SqlSession sqlSession = sqlSessionFactory.openSession(true);</pre>

3. 加入log4j日志功能

  • 在pom.xml中加入依赖

    <pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
    </dependency></pre>

  • 加入log4j的配置文件

    log4j的配置文件名必须为log4j.xml,存放的位置是src/main/resources目录下

    <pre class="prettyprint hljs dust" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"><?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <param name="Encoding" value="UTF-8" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
    </layout>
    </appender>
    <logger name="java.sql">
    <level value="debug" />
    </logger>
    <logger name="org.apache.ibatis">
    <level value="info" />
    </logger>
    <root>
    <level value="debug" />
    <appender-ref ref="STDOUT" />
    </root>
    </log4j:configuration></pre>

    重新运行代码,查看日志功能:

    image.png

相关文章

  • Mybatis框架--优化过程

    0. 原代码预览 简单实现在数据库中插入数据 public void testInsert() throws IO...

  • 第六章 初识Mybatis

    什么是Mybatis Mybatis是一个支持普通SQL查询、存储过程、高级映射的持久层框架 Mybatis框架也...

  • Mybatis

    Mybatis 介绍MyBatis是支持普通 SQL 查询,存储过程和高级映射的优秀持久 层框架。MyBatis ...

  • MyBatis实战总结

    MyBatis的前身是iBatis,它是一个数据持久层框架。封装优化了普通JDBC的过程,如数据库连接的创建、设置...

  • mybatis实战教程(一)环境配置及简单入门

    mybatis简介 MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis ...

  • SpringBoot基础教程(五) | Mybatis篇

    Mybatis介绍 MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis ...

  • JavaEE基础知识学习----MyBatis(一)简介

    MyBatis简介 MyBatis概述 MyBatis 是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架...

  • 学习笔记——MyBatis入门

    MyBatis简介 MyBatis 是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避...

  • SSM面试

    Mybatis 简介 mybatis支持普通sql查询,存储过程和高级映射的优秀持久层框架,Mybatis消除了几...

  • mybatis--特性,特点,ORM思想

    MyBatis特性:① MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架②MyBatis避...

网友评论

      本文标题:Mybatis框架--优化过程

      本文链接:https://www.haomeiwen.com/subject/qxaygrtx.html