JUnit是专门做单元测试的组件
junit
junit
4.13.2
test
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
org.apache.maven.plugins
maven-compiler-plugin
3.1
17
test:
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 org.junit.Test;
import java.io.InputStream;
/**
* @program: spring_learn
* @description: test
* @author: Mr.Wang
* @create: 2023-06-13 07:42
**/
public class CarMapperTest {
@Test
public void testInsertCar(){
SqlSession sqlSession = null;
try {
// 1.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 2.创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
// 3.创建SqlSession对象
sqlSession = sqlSessionFactory.openSession();
// 4.执行SQL
int count = sqlSession.insert("insertCar");
System.out.println("更新了几条记录:" + count);
// 5.提交
sqlSession.commit();
} catch (Exception e) {
// 回滚
if (sqlSession != null) {
sqlSession.rollback();
}
e.printStackTrace();
} finally {
// 6.关闭
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
引入日志框架logback
引入日志框架的目的是为了看清楚mybatis执行的具体sql
mybatis – MyBatis 3 | 配置
指定 MyBatis 所用日志的具体实现
STDOUT_LOGGING 标准日志,由mybatis实现
mybatis-congif.xml:
test:
SLF4J (一种日志标准)
logbak日志框架 (实现了SLF4J)
引入依赖
ch.qos.logback
logback-classic
1.2.11
test
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
logback 需要配置文件
文件名: logback.xml OR logback-test.xml
位置: 类路径根目录下
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
CarMapper.xml:
insert into t_car
(id,car_num,brand,guide_price,produce_time,car_type)
values
(null,'102','皇风',40.30,'2024-10-05','新能源')
mybatis-config.xml
test:
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 org.junit.Test;
import java.io.InputStream;
/**
* @program: spring_learn
* @description: test
* @author: Mr.Wang
* @create: 2023-06-13 07:42
**/
public class CarMapperTest {
@Test
public void testInsertCar(){
SqlSession sqlSession = null;
try {
// 1.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 2.创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
// 3.创建SqlSession对象
sqlSession = sqlSessionFactory.openSession();
// 4.执行SQL
int count = sqlSession.insert("insertCar");
System.out.println("更新了几条记录:" + count);
// 5.提交
sqlSession.commit();
} catch (Exception e) {
// 回滚
if (sqlSession != null) {
sqlSession.rollback();
}
e.printStackTrace();
} finally {
// 6.关闭
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
result:
"C:\Program Files\Java\jdk-17\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -Didea.launcher.port=53550 "-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.CarMapperTest,testInsertCar
2023-06-17 14:03:32.877 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2023-06-17 14:03:32.877 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2023-06-17 14:03:32.893 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2023-06-17 14:03:32.893 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2023-06-17 14:03:32.893 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2023-06-17 14:03:32.893 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2023-06-17 14:03:32.940 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
2023-06-17 14:03:33.169 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 252738640.
2023-06-17 14:03:33.171 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f107c50]
2023-06-17 14:03:33.176 [main] DEBUG car.insertCar - ==> Preparing: insert into t_car (id,car_num,brand,guide_price,produce_time,car_type) values (null,'102','皇风',40.30,'2024-10-05','新能源')
2023-06-17 14:03:33.211 [main] DEBUG car.insertCar - ==> Parameters:
2023-06-17 14:03:33.227 [main] DEBUG car.insertCar - <== Updates: 1
更新了几条记录:1
2023-06-17 14:03:33.227 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f107c50]
2023-06-17 14:03:33.227 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f107c50]
2023-06-17 14:03:33.227 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f107c50]
2023-06-17 14:03:33.227 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 252738640 to pool.Process finished with exit code 0
${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log LOG_HOME 没有指定,所以生成的 folder name is LOG_HOME_IS_UNDEFINED