• Mybatis02


    MyBatis02

    1.优化

    @Before 在代码执行之前
    @After  在代码执行之后
        对CRUD中的重复代码进行封装
    
    • 1
    • 2
    • 3

    2.配置

    MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。 配置文档的顶层结构如下:

    2.1已配置别名

    下面是一些为常见的 Java 类型内建的类型别名。它们都是不区分大小写的,注意,为了应对原始类型的命名重复,采取了特殊的命名风格。

    别名映射的类型
    _bytebyte
    _longlong
    _shortshort
    _intint
    _integerint
    _doubledouble
    _floatfloat
    _booleanboolean
    stringString
    byteByte
    longLong
    shortShort
    intInteger
    integerInteger
    doubleDouble
    floatFloat
    booleanBoolean
    dateDate
    decimalBigDecimal
    bigdecimalBigDecimal
    objectObject
    mapMap
    hashmapHashMap
    listList
    arraylistArrayList
    collectionCollection
    iteratorIterator

    2.2自定义别名

    类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。例如:

    <typeAliases>
      <typeAlias alias="Author" type="domain.blog.Author"/>
      <typeAlias alias="Blog" type="domain.blog.Blog"/>
      <typeAlias alias="Comment" type="domain.blog.Comment"/>
      <typeAlias alias="Post" type="domain.blog.Post"/>
      <typeAlias alias="Section" type="domain.blog.Section"/>
      <typeAlias alias="Tag" type="domain.blog.Tag"/>
    typeAliases>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,比如:

    <typeAliases>
      <package name="domain.blog"/>
    typeAliases>
    
    • 1
    • 2
    • 3

    每一个在包 domain.blog 中的 Java Bean,在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名。 比如 domain.blog.Author 的别名为 author;若有注解,则别名为其注解值。见下面的例子:

    @Alias("author")
    public class Author {
        ...
    }
    
    • 1
    • 2
    • 3
    • 4

    3.ORM映射

    ORM(对象关系映射)是一种编程技术,用于将关系型数据库中的表结构映射到面向对象的语言中的类和对象。ORM工具能够自动完成数据库查询、插入、更新和删除等操作,让开发人员能够使用面向对象的方式来操作数据库。
    
    在Java中,有多个流行的ORM框架可供选择,如HibernateMyBatisSpring Data JPA等。这些框架提供了不同的特性和使用方式,但它们的核心思想都是通过配置和映射来实现对象与数据库表之间的映射关系。
        
        
        如果数据库列的名字与实体类中成员变量名称不同时,则获取不到数据
       A 此时可以在sql语句中给每一列 起别名 让他们一一对应
       B 使用映射
        <result column="s_name" property="name"></result>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.mapper.xml中获取主键

    <insert id="saveStudent" parameterType="com.aaa.entity.Student" useGeneratedKeys="true" 		keyColumn="s_id" keyProperty="id">
            insert into student
            values (null, #{name}, #{age}, #{address}, 1)
     </insert>
        
        useGeneratedKeys:是否获取主键
        keyColumn:数据库主键列名称
        keyProperty:接收主键的成员变量    
            
        我们添加学生的时候 传递的参数是  parameterType="com.aaa.entity.Student"  
            此时获取主键会赋值给student的id属性
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    5.日志输出

    添加依赖

        <dependency>
          <groupId>log4jgroupId>
          <artifactId>log4jartifactId>
          <version>1.2.12version>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在resources文件夹下创建log4j.properties文件

    log4j.rootLogger=ERROR, stdout
    
    log4j.logger.com.aaa = TRACE
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6.#和$的区别

    MyBatis中KaTeX parse error: Expected 'EOF', got '#' at position 6: {} 和 #̲{} 有什么区别?_在 myb…{} 和 #{} 的区别是什么_蜀州凯哥的博客-CSDN博客

    7.接口绑定

    接口绑定四对应
        1.创建一个接口
        public interface  StudentMapper {
         }
    	2.添加接口函数
        public interface  StudentMapper {
        	int saveStudent(Student student);
        }
        3.创建mapper.xml 添加对应标签
            1.<mapper namespace="com.aaa.mapper.StudentMapper"> 对应接口的全限定名
            2.<insert id="saveStudent"> 标签id对应函数名
            3.<select  parameterType="int" > 对应函数参数类型
            4.<select  resultType="com.aaa.entity.Student"> 返回值类型对应
    
    
    接口绑定之后 接口类型就不用写了
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    8.多参数传递

        List<Student> queryStudent(@Param("haha") int id,@Param("hehe") int age);
    
    
        <select id="queryStudent" resultType="com.aaa.entity.Student">
            select *
            from student
            where id = #{haha}
              and age = #{hehe}
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    9.动态sql

    动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

    使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显著地提升了这一特性的易用性。

    如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。

    • if
    • choose (when, otherwise)
    • trim (where, set)
    • foreach

    -----待补充

    10.注解配置sql

    mybatis支持 只写接口  在接口方法上 使用注解写sql
        先在配置文件中加入<package name="com.aaa.mapper"/>
    
        @Select("select * from student where id = #{id}")
        Student getOne(Integer id);
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    Linux服务器使用Nginx和Tomcat配置Https请求
    【SQL】其它查询优化策略
    Windows安装Elasticsearch8.x保姆级教程
    主板损坏时间不自动更新,如何解决
    锁降级 ReentrantReadWriteLock
    PMP 11.27 考试倒计时13天!冲刺啦!
    【力扣周赛】第 363 场周赛(完全平方数和质因数分解)
    具有柔性结构的孤岛直流微电网的分级控制(Malab代码实现)
    安装ubuntu20.04, CUDA11.4, cnDNN, tensorflow, pytorch
    红外小目标:DNANet网络结构与模型搭建
  • 原文地址:https://blog.csdn.net/g877835148/article/details/132855706