Mybatis study notes 2 Detailed explanation of additions, deletions, modifications and core configuration files

Mybatis study notes 1 Getting started with Mybatis_biubiubiu0706's blog-CSDN blog

 Encapsulate Mybatis

SqlSessionUtil tool class

package com.example.util;

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;

/**
 * @author hrui
 * @date 2023/9/8 14:55
 */
public class SqlSessionUtil {

    //工具类的构造方法一般都是私有化
    //方法都是静态的
    //为了防止new对象,构造方法私有化
    private SqlSessionUtil(){

    }
    private static SqlSessionFactory sqlSessionFactory;

    //类加载时候执行
    //SqlSessionUtil工具类在被加载的时候,解析mybatis-config1.xml.创建sqlSessionFactory对象
    static{
        try {
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
            //一个sqlSessionFactory对应一个数据库
            sqlSessionFactory= sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config1.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取会话对象 返回会话对象
    public static SqlSession openSession(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}

This makes testing much more convenient

Create a new module mybatis-02-crud

maven project

Introduce dependencies

<!--引入mybatis依赖和mysql依赖-->
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>
        <!--引入logback依赖-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Copy the tool class and mybatis-config1.xml, logback.xml (log configuration file), xxxMapper.xml

The following writing method does not exist in actual development, and the values ​​​​are hard-coded.

The object passed in is to encapsulate data and form a mapping relationship with #{xxx} in sql.

Try encapsulating it with Map. Write the key of the Map collection in #{}. What if the key does not exist? #{key does not exist} will be a null value.

Inserted successfully

Generally speaking, we will use the key as the corresponding relationship

The above is to pass the value through Map

The following provides set get, no parameters, full parameters, toString equal hashCode through value passing in the POJO class.

At this time, #{} is the attribute name of POJO (strictly speaking, XXX is converted to lowercase after the get method? I think it ignores the case and forms a mapping relationship. If the attribute name is written incorrectly, Map will insert null, but pojo If so, an error will be reported and the message There is no getter for property named 'xxxx' in class xxxxxxxx

It reported an error saying that the get method of the attribute could not be found.

Complete delete operation

If you pass a constant value directly #{You can write whatever you want}

Complete update operation

Query Check one

The id in the picture below is wrong. selectCarById

Note here that there is no need to commit when querying

Solution

1. Add an alias

2. Use resultMap

3. Configure to use camel case identifier

1. Add alias method:

2. Use resultMap

3. Use CamelCase

In the mybatis core configuration file

.XML

test

select search all

About the role of namespace in mapping files

Originally, the above tests only had one xxxMapper.xml mapping file.

When I have two .XML mapping files and the ids in them are the same

This way something will go wrong

test

In this situation

Multi-environment configuration in Mybatis core configuration file

About transaction management in Mybatis core configuration file

About DataSource------->DataSource is the specification of JDK. DataSource is simple to understand. It puts database connection.

Different properties under different types of DataSource

The role of JNDI: Let Mybatis use the connection pool of the container. The container implements the JNDI specification.

About the data source type UNPOOLED-->Not applicable to database connection pool--->Create a new Connection object each time

and POOLED---->use the database connection pool implemented by Mybatis itself

What are the specific differences regarding the above lights?

Create new libraries and tables in your local database

Core configuration file The default database uses connection pooling. Connection pooling is not applicable locally. See the specific differences.

If the test uses a connection pool, the connection object will be reused.

Test another database without connection pooling

Some configurations about database connection pool

What will happen when the connection is exhausted???????

It will wait for 20 seconds. After 20 seconds, it will force the connection to continue execution.

About the maximum number of free

About the preoperties tag in the core configuration file

Another use of properties

There is another way to write properties. The way to write url is also possible.

Guess you like

Origin blog.csdn.net/tiantiantbtb/article/details/132760315