MyBatis工具类SqlSessionUtil的封装

SqlSessionUtil

package com.wsd;

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;

/**
 * @program: spring_learn
 * @description: Utility class for mybatis
 * @author: Mr.Wang
 * @create: 2023-06-17 17:38
 **/
public class SqlSessionUtil {

    private SqlSessionUtil(){}

    private static SqlSessionFactory sqlSessionFactory;

    /**
     * 类加载时初始化sqlSessionFactory对象
     */
    static {
        try {
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 每调用一次openSession()可获取一个新的会话。
     *
     * @return 新的会话对象
     */
    public static SqlSession openSession() {
        return sqlSessionFactory.openSession();
    }
}

 

pom.xml




    4.0.0

    com.wsd
    testmybatis
    1.0

    
        
        
            org.mybatis
            mybatis
            3.5.10
        

        
        
            mysql
            mysql-connector-java
            8.0.30
        

        
        
            junit
            junit
            4.13.2
            test
        
        
        
            ch.qos.logback
            logback-classic
            1.2.11
            test
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.1
                
                    17
                    17
                
            
        
    

mybatis-config.xml:


        

    


    
        
        
            
            
            
            
        
    


    

CarMapper.xml:




    
    
        insert into t_car
            (id,car_num,brand,guide_price,produce_time,car_type)
        values
            (null,'102','霸图',50.30,'2024-11-05','新能源')
    

 

 logback.xml:




    
    
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
    
    
    
        
            
            ${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log
            
            30
        
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
        
        
            100MB
        
    

    
    
    
    
    

    
    
        
        
    

 

test:

package com.wsd;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

/**
 * @program: spring_learn
 * @description: test SqlSessionUtil
 * @author: Mr.Wang
 * @create: 2023-06-17 17:56
 **/
public class TestSqlSessionUtil {

    @Test
    public void testSqlsessionUtil(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        // 执行SQL
        int count = sqlSession.insert("insertCar");
        System.out.println("插入了几条记录:" + count);
        sqlSession.commit();
        sqlSession.close();
    }
}

result:

"C:\Program Files\Java\jdk-17\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -Didea.launcher.port=57530 "-Didea.launcher.bin.path=C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\lib\idea_rt.jar;C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\plugins\junit\lib\junit-rt.jar;C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\plugins\junit\lib\junit5-rt.jar;C:\Minecloud\IDEA_workspace\spring_learn\testmybatis\target\test-classes;C:\Minecloud\IDEA_workspace\spring_learn\testmybatis\target\classes;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\mybatis\mybatis\3.5.10\mybatis-3.5.10.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\mysql\mysql-connector-java\8.0.30\mysql-connector-java-8.0.30.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\com\google\protobuf\protobuf-java\3.19.4\protobuf-java-3.19.4.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\junit\junit\4.13.2\junit-4.13.2.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar" com.intellij.rt.execution.application.AppMainV2 com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.wsd.TestSqlSessionUtil,testSqlsessionUtil
2023-06-17 18:04:29.270 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2023-06-17 18:04:29.285 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2023-06-17 18:04:29.285 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2023-06-17 18:04:29.285 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2023-06-17 18:04:29.285 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2023-06-17 18:04:29.333 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
2023-06-17 18:04:29.569 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 549496397.
2023-06-17 18:04:29.569 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@20c0a64d]
2023-06-17 18:04:29.569 [main] DEBUG car.insertCar - ==>  Preparing: insert into t_car (id,car_num,brand,guide_price,produce_time,car_type) values (null,'102','霸图',50.30,'2024-11-05','新能源')
2023-06-17 18:04:29.600 [main] DEBUG car.insertCar - ==> Parameters: 
2023-06-17 18:04:29.621 [main] DEBUG car.insertCar - <==    Updates: 1
插入了几条记录:1

2023-06-17 18:04:29.621 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@20c0a64d]
2023-06-17 18:04:29.621 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@20c0a64d]
2023-06-17 18:04:29.621 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@20c0a64d]
2023-06-17 18:04:29.621 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 549496397 to pool.

Process finished with exit code 0
 

 MyBatis工具类SqlSessionUtil的封装_第1张图片

 

你可能感兴趣的:(mybatis,java,spring)