1、建库建表
CREATE DATABASE `mybatis-example`; USE `mybatis-example`; CREATE TABLE `t_emp`( emp_id INT AUTO_INCREMENT, emp_name CHAR(100), emp_salary DOUBLE(10,5), PRIMARY KEY(emp_id) ); INSERT INTO `t_emp`(emp_name,emp_salary) VALUES("tom",200.33); INSERT INTO `t_emp`(emp_name,emp_salary) VALUES("jerry",666.66); INSERT INTO `t_emp`(emp_name,emp_salary) VALUES("andy",777.77);
2、pom.xml
org.mybatis mybatis mysql mysql-connector-java org.junit.jupiter junit-jupiter-api org.projectlombok lombok 3、Employee.java(pojo)
package com.atguigu.mybatis.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Employee { private Integer empId; private String empName; private Double empSalary; }
4、EmpMapper.java
package com.atguigu.mybatis.mapper; import com.atguigu.mybatis.pojo.Employee; public interface EmpMapper { void addEmp(Employee employee); }
5、mybatis-config.xml(mybatis的总配置文件)
6、EmpMapper.xml
insert into t_emp values(0,#{empName},#{empSalary}) 7、MybatisTest3.java
package com.atguigu.mybatis; import com.atguigu.mybatis.mapper.EmpMapper; import com.atguigu.mybatis.pojo.Employee; 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.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; import java.io.InputStream; public class MybatisTest3 { SqlSessionFactory sqlSessionFactory; SqlSession sqlSession; EmpMapper empMapper; @BeforeEach public void setup() throws IOException { // 获取资源流,读取"mybatis-config.xml"文件 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); // 使用资源流创建SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 使用SqlSessionFactory打开一个Session sqlSession = sqlSessionFactory.openSession(); // 使用Session获取EmpMapper的Mapper对象 empMapper = sqlSession.getMapper(EmpMapper.class); } // 在每个测试用例之后执行的清理方法 @AfterEach public void teardown() { sqlSession.commit(); // 提交事务 sqlSession.close(); // 关闭SqlSession } @Test public void addEmpTest() { Employee employee = new Employee(0, "鸠摩智", 8080.0); empMapper.addEmp(employee); } }
8、logback
D:\Java\jdk-17\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=54009:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\develop\maven\repository\org\junit\platform\junit-platform-launcher\1.3.1\junit-platform-launcher-1.3.1.jar;D:\develop\maven\repository\org\apiguardian\apiguardian-api\1.0.0\apiguardian-api-1.0.0.jar;D:\develop\maven\repository\org\junit\platform\junit-platform-engine\1.3.1\junit-platform-engine-1.3.1.jar;D:\develop\maven\repository\org\junit\platform\junit-platform-commons\1.3.1\junit-platform-commons-1.3.1.jar;D:\develop\maven\repository\org\opentest4j\opentest4j\1.1.1\opentest4j-1.1.1.jar;D:\develop\maven\repository\org\junit\jupiter\junit-jupiter-engine\5.3.1\junit-jupiter-engine-5.3.1.jar;D:\develop\maven\repository\org\junit\jupiter\junit-jupiter-api\5.3.1\junit-jupiter-api-5.3.1.jar;D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar;D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\plugins\junit\lib\junit5-rt.jar;D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\plugins\junit\lib\junit-rt.jar;F:\IdeaProjects\workspace\pro-ssm\pro18-mybatis-begin\target\test-classes;F:\IdeaProjects\workspace\pro-ssm\pro18-mybatis-begin\target\classes;F:\my_java\maven_repository\org\mybatis\mybatis\3.5.11\mybatis-3.5.11.jar;F:\my_java\maven_repository\mysql\mysql-connector-java\8.0.25\mysql-connector-java-8.0.25.jar;F:\my_java\maven_repository\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar;F:\my_java\maven_repository\org\junit\jupiter\junit-jupiter-api\5.3.1\junit-jupiter-api-5.3.1.jar;F:\my_java\maven_repository\org\apiguardian\apiguardian-api\1.0.0\apiguardian-api-1.0.0.jar;F:\my_java\maven_repository\org\opentest4j\opentest4j\1.1.1\opentest4j-1.1.1.jar;F:\my_java\maven_repository\org\junit\platform\junit-platform-commons\1.3.1\junit-platform-commons-1.3.1.jar;F:\my_java\maven_repository\org\projectlombok\lombok\1.18.20\lombok-1.18.20.jar;F:\my_java\maven_repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\my_java\maven_repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;F:\my_java\maven_repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.atguigu.mybatis.MybatisTest3,addEmpTest 09:51:08,942 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 09:51:08,943 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 09:51:08,943 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/F:/IdeaProjects/workspace/pro-ssm/pro18-mybatis-begin/target/classes/logback.xml] 09:51:09,020 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 09:51:09,023 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT] 09:51:09,029 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property 09:51:09,058 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG 09:51:09,058 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 09:51:09,060 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.atguigu.mybatis] to DEBUG 09:51:09,060 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 09:51:09,061 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@3d1cfad4 - Registering current configuration as safe fallback point [09:51:09.067] [DEBUG] [main] [org.apache.ibatis.logging.LogFactory] [Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.] [09:51:09.085] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [09:51:09.085] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [09:51:09.085] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [09:51:09.085] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [09:51:09.168] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Opening JDBC Connection] [09:51:09.674] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [Created connection 494953170.] [09:51:09.675] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1d8062d2]] [09:51:09.679] [DEBUG] [main] [com.atguigu.mybatis.mapper.EmpMapper.addEmp] [==> Preparing: insert into t_emp values(0,?,?)] [09:51:09.723] [DEBUG] [main] [com.atguigu.mybatis.mapper.EmpMapper.addEmp] [==> Parameters: 鸠摩智(String), 8080.0(Double)] [09:51:09.726] [DEBUG] [main] [com.atguigu.mybatis.mapper.EmpMapper.addEmp] [<== Updates: 1] [09:51:09.730] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1d8062d2]] [09:51:09.733] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1d8062d2]] [09:51:09.736] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1d8062d2]] [09:51:09.736] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [Returned connection 494953170 to pool.] Process finished with exit code 0
9、
在MyBatis的配置文件中,
元素用于指定使用JDBC事务管理器来管理数据库事务。
JDBC事务管理器通过JDBC连接进行事务管理,它支持普通的数据库连接事务。当使用JDBC事务管理器时,MyBatis将使用JDBC的提交和回滚来控制事务的提交和回滚。
在MyBatis的配置文件中,使用
元素可以指定使用不同的事务管理器。除了JDBC事务管理器,MyBatis还支持其他的的事务管理器,例如Spring事务管理器。
使用
元素告诉MyBatis在事务管理方面使用JDBC事务管理器,而不是其他的事务管理器。这对于与数据库交互的Java应用程序非常有用,因为它们通常使用JDBC连接来执行数据库操作。
在 MyBatis 中,
用于指定事务管理器的类型是 JDBC,也就是采用 JDBC 的方式来管理事务。 JDBC 事务管理器是 MyBatis 默认的事务管理器类型,它会在每个 SQL 会话中使用 JDBC 的自动提交(auto-commit)特性来实现事务的提交和回滚。也就是说,当一个 SQL 语句执行成功时,它会自动提交事务,如果执行失败,则会自动回滚事务。
需要注意的是,如果我们想要对事务进行更加细粒度的控制,比如手动提交或回滚事务,那么就需要使用其他类型的事务管理器,比如 JTA 或 Spring 管理的事务管理器。