mybatis学习过程记录(代码片段)

wangweizu99 wangweizu99     2023-02-26     421

关键词:

一、Mybatis

1、mybatis-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>
<!--    外部引入db-->
    <properties resource="db.properties"/>
    <settings>
<!--        配置日志文件-->
<!--        <setting name="logImpl" value="STDOUT_LOGGING"/>-->
        <setting name="logImpl" value="log4j"/>
<!--        开启A_NAME==>aName-->        
        <setting name="mapUnderscoreToCamelCase" valie="true"
    </settings>
<!--   减少类型-->
    <typeAliases>
        <typeAlias type="com.wang.pojo.User" alias="User"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <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>
    </environments>
    <mappers>
        <mapper resource="com/wang/dao/UserMapper.xml"/>
    </mappers>
</configuration>

2、Mapper.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="com.wang.dao.UserDao">
    <select id="getUserList" resultType="User">
        select * from mybatis.user
    </select>
</mapper>

3、db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=123456

4、MybatisUtils工具类

package com.wang.utils;

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;
public class MybatisUtils 
    private static SqlSessionFactory sqlSessionFactory;
    static 
        InputStream inputStream = null;
        try 
            String resource = "mybatis-config.xml";
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
         catch (IOException e) 
            e.printStackTrace();
        
    
    public static SqlSession getsqlsession() 
        return sqlSessionFactory.openSession();
    

5、多对一AND多对一:

在Mapper.xml中配置

  • association 对象 多对一
  • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A1UuYsbK-1623762886965)(C:\\Users\\HP\\AppData\\Roaming\\Typora\\typora-user-images\\image-20210614215609352.png)]
  • collection 集合 一对多
  • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4d3XQiVc-1623762886966)(C:\\Users\\HP\\AppData\\Roaming\\Typora\\typora-user-images\\image-20210614220615814.png)]

从用参数解释:

  • javaType java类型
  • ofType 泛型约束类型

6、使用注解开发

  • 可在接口中使用注解进行开发,省去Mapper.xml

    • @Select("select * from mybatis.User")
      List<User> getUserList();
      
    • 在mybatis-config.xml中配置
      <mappers>
              <mapper class="com.wang.Dao.UserMapper"
      </mappers>
      

7、自定义缓存:ehcache

8、mybatis 其他工具

  • mybatis Hepler:https://pagehelper.github.io/

二、MAVEN资源导出错误解决

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

三、常用依赖导入

    <!--导入依赖-->
    <dependencies>
        <!--    mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <!--        mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--        junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
<!--        lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
<!--        servlet and jsp-->
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>servlet-api-2.5</artifactId>
            <version>6.1.7</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>
<!--        standard-->     
        <dependency>
            <groupId>org.apache.karaf.assemblies.features</groupId>
            <artifactId>standard</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
<!--        jstl-->       
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
        </dependency>
 <!--        fastjson-->    
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <!--Log4j-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

四、LOG4J

1、log4j.properties 配置文件

# priority  :debug<info<warn<error
#you cannot specify every priority with different file for log4j
log4j.rootLogger=debug,stdout,info,debug,warn,error 

#console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern= [%dyyyy-MM-dd HH:mm:ss a]:%p %l%m%n
#info log
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.info.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.info.File=./src/com/hp/log/info.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.layout=org.apache.log4j.PatternLayout 
log4j.appender.info.layout.ConversionPattern=%dyyyy-MM-dd HH:mm:ss a [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#debug log
log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.debug.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.debug.File=./src/com/hp/log/debug.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.layout=org.apache.log4j.PatternLayout 
log4j.appender.debug.layout.ConversionPattern=%dyyyy-MM-dd HH:mm:ss a [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#warn log
log4j.logger.warn=warn
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.warn.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.warn.File=./src/com/hp/log/warn.log
log4j.appender.warn.Append=true
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.layout=org.apache.log4j.PatternLayout 
log4j.appender.warn.layout.ConversionPattern=%dyyyy-MM-dd HH:mm:ss a [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#error
log4j.logger.error=error
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.error.File = ./src/com/hp/log/error.log 
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR 
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %dyyyy-MM-dd HH:mm:ss a [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n

2、log4j 在mybatis-config.xml的配置

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

3、使用场景

  • 在具体类中引入

    Logger logger = Logger.getLogger(UserDaoTest.class);
    logger.error();
    logger.debug();
    

好用的插件:

1、lombok

常用注解:

  • @Data 自动生成geter seter
  • @AllArgsConstructor 生成有参构造函数
  • @NoArgsConstructor 生成无参构造函数
    =“logImpl” value=“LOG4J”/>

## 3、使用场景

- 在具体类中引入

  ```java
  Logger logger = Logger.getLogger(UserDaoTest.class);
  logger.error();
  logger.debug();

好用的插件:

1、lombok

常用注解:

  • @Data 自动生成geter seter
  • @AllArgsConstructor 生成有参构造函数
  • @NoArgsConstructor 生成无参构造函数

mybatis原理分析学习记录(代码片段)

以下个人学习笔记,仅供参考,欢迎指正。MyBatis是支持定制化SQL、存储过程以及高级映射的持久层框架,其主要就完成2件事情:封装JDBC操作利用反射打通Java类与SQL语句之间的相互转换MyBatis的主要设计目的就是让我们对执行SQL... 查看详情

mybatis学习(代码片段)

Mybatis简介MyBatis是一款优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射。MyBatis免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作。MyBatis可以通过简单的XML或注解来配置和映射原始类型、接口和JavaPOJO&#x... 查看详情

mybatis学习随笔(代码片段)

一、Mybatis的特性MyBatis支持定制化SQL、存储过程以及高级映射Mybatis避免了几乎所有的JDBC代码和手动设置参数以及结果集解析操作Mybatis可以使用简单的XML或注解实现配置和原始映射,将接口和Java的POJO(普通的Java对象)映射成数据... 查看详情

mybatis结果映射与mybatis缓存初探学习记录(代码片段)

MyBatis高级结果映射(一对一、一对多、多对多的映射),延迟加载,查询缓存(一级缓存),二级缓存的学习记录;1、学习中所使用到的例子,数据库基础分析2、高级结果映射3、延迟加载4、... 查看详情

mybatis源码过程学习梳理(代码片段)

一、mybatis配置文件详情在Spring中,是使用xml的配置文件或使用java代码对mybatis的连接属性,环境等进行配置的,我们先看一下mybatis中对配置文件中节点的要求。通过mybatis官方网站中XML配置一栏,可以得出相应需... 查看详情

mybatis学习1:从零开始搭建mybatis环境(代码片段)

Mybatis简介MyBatis是一款优秀的持久层框架MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集的过程MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的实体类【PlainOldJavaObjects,普通的Java对象】映... 查看详情

mybatis学习总结——调用存储过程(代码片段)

一、返回select结果集1、创建存储过程DELIMITER//DROPPROCEDUREIFEXISTSproc_queryUser;CREATEPROCEDUREproc_queryUser(INuser_nameVARCHAR(50)CHARACTERSETutf8)BEGINSET@exeSql=CONCAT(‘SELECTid,username,sex,birthday,address‘,‘fromt_userwhereusernamelike‘‘,user_name,‘%‘orderbyid‘)... 查看详情

mybatis系统性详解(学习笔记)(代码片段)

目录mybatis知识传统JDBC不足mybatis基础mybatis核心应用配置与原理解析mybatis核心概念整体认识mybatis源码包mybatis基本流程类调用mybatis流程记录mybatis处理流程图mybatis*之sessionmybatis之mappermybatis之sqlmybatis之executormybatis之Cache一级缓存二... 查看详情

学习017mybatis框架(代码片段)

一、目标Mybatis介绍Mybatis增删改查SQL注入问题介绍Mybatisxml与注解实现Mybatis分页二、Mybatis快速入门2.1Mybatis介绍MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以... 查看详情

mybatis源码过程学习梳理(代码片段)

一、mybatis配置文件详情在Spring中,是使用xml的配置文件或使用java代码对mybatis的连接属性,环境等进行配置的,我们先看一下mybatis中对配置文件中节点的要求。通过mybatis官方网站中XML配置一栏,可以得出相应需... 查看详情

mybatis学习笔记(代码片段)

...:配置文件的。最好的方式:看官方文档1、简介Mybatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。Mybatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。Mybatis可以使用简单的XML或... 查看详情

mybatis存储过程(代码片段)

  MyBatis还能对存储过程进行完全支持,这节开始学习存储过程。在讲解之前,我们需要对存储过程有一个基本的认识,首先存储过程是数据库的一个概念,它是数据库预先编译好,放在数据库内存中的一个程序片段,所以具... 查看详情

mybatis全面详解——上(学习总结)(代码片段)

...址:https://blog.csdn.net/ITITII/article/details/79969447一、什么是Mybatis这里借用官网的一句话介绍什么是mybatis:MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数... 查看详情

jvm学习记录-类加载的过程(代码片段)

类的整个生命周期的7个阶段是:加载(Loading)、验证(Verification)、准备(Preparation)、解析(Resolution)、初始化(Initialization)、使用(Using)、卸载(Unloading)。类加载的全过程主要包括:加载、验证、准备、解析、初始化这5个阶段的内容... 查看详情

spring框架学习----spring整合mybatis框架(代码片段)

Spring框架学习(九)----整合Mybatis框架一、整合Mybatis的过程不管在整合前还是整合后,sqlSessionFactory都是非常重要的(mybatis的核心)整合前mybatis-config.xml配置文件->sqlSessionFactory->sqlSession整合后sqlSession 查看详情

mybatis学习笔记(代码片段)

...:配置文件的。最好的方式:看官方文档1、简介Mybatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。Mybatis避免了几乎所有的 查看详情

mybatis拦截器学习(代码片段)

...;后期的扩展和维护也会很麻烦,因此想到之前用过的mybatis拦截器,这里学习记录一番。MyBatis拦截器介绍官方介绍从官网我们介绍我们可知:MyBatis提供了一种插件(plugin)的功能,虽然叫做插件,但其实这是拦... 查看详情

mybatis学习

什么是MyBatis?MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的POJ... 查看详情