Mybatis学习过程记录

一、Mybatis

1、mybatis-config.xml


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="db.properties"/>
    <settings>


        <setting name="logImpl" value="log4j"/>
        
        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


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>
              mappers>
      

7、自定义缓存:ehcache

8、mybatis 其他工具

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

二、MAVEN资源导出错误解决

<build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>

三、常用依赖导入

    
    <dependencies>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.49version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.2version>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.20version>
        dependency>

        <dependency>
            <groupId>org.mortbay.jettygroupId>
            <artifactId>servlet-api-2.5artifactId>
            <version>6.1.7version>
        dependency>
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.1version>
        dependency>
     
        <dependency>
            <groupId>org.apache.karaf.assemblies.featuresgroupId>
            <artifactId>standardartifactId>
            <version>2.4.4version>
        dependency>
        <dependency>
            <groupId>taglibsgroupId>
            <artifactId>standardartifactId>
            <version>1.1.2version>
        dependency>
       
        <dependency>
            <groupId>javax.servlet.jsp.jstlgroupId>
            <artifactId>jstl-apiartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>org.glassfish.webgroupId>
            <artifactId>jstl-implartifactId>
            <version>1.2version>
        dependency>
     
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.76version>
        dependency>
        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
    dependencies>

四、LOG4J

1、log4j.properties 配置文件

# priority  :debug> 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=%d{yyyy-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=%d{yyyy-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 = %d{yyyy-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,javaweb)