Mybatis series (18) – related concepts – transaction management

Time:2020-10-27

Step 1: run first, see the effect, and then learn
Step 2: imitation and troubleshooting
Step 3: JDBC mode
Step 4: test the code
Step 5: MySQL table must be of type InnoDB

Step 1: run first, see the effect, and then learn

As the old rule, first download the executable items in the download area (click to enter), configure and run them, and then learn what steps to do to achieve this effect.

Step 2: imitation and troubleshooting

After making sure that the runnable project runs correctly, follow the steps of the tutorial and imitate the code again.
It is inevitable that there is code discrepancy in the simulation process, which leads to the failure to get the expected running resultsright key(can run the project) and your own code to locate the problem.
In this way,Learning is effective and troubleshooting is efficientIt can obviously improve the learning speed and cross the threshold of learning.

It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my runnable project folder.
This software is very powerful, you can know which two files in the folder are wrong, and mark them out clearly
Here is a green installation and use tutorial: diffmerge download and use tutorial

Step 3: JDBC mode

Make sure line 12 is

<transactionManager type=`”JDBC”`/>

Another value is managed, which is managed by the container, such as Weblogic, which is not discussed in this knowledge point.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.how2java.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="admin"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/how2java/pojo/Category.xml"/>
        <mapper class="com.how2java.mapper.CategoryMapper"/>
    </mappers>
</configuration>

Step 4: test the code

38 lines of code commit the transaction.

session.commit();

Inserting the second data cannot be inserted into the database because it is longer than 32.
Because it is in the same transaction, the first data cannot be inserted successfully.

Mybatis series (18) - related concepts - transaction management

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

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.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
public class TestMybatis {
    public static void main(String[] args) throws IOException, SQLException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        CategoryMapper mapper = session.getMapper(CategoryMapper.class);

        Category c1 = new Category();
        C1. Setname ("short enough name");
        mapper.add(c1);
        Category c2 = new Category();
        C2. Setname ("names exceeding the maximum length of 30 names exceeding the maximum length of 30 names exceeding the maximum length of 30 names exceeding the maximum length of 30 names exceeding the maximum length of 30");
        mapper.add(c2);
        listAll(mapper);
        session.commit();
        session.close();
    }
    private static void listAll(CategoryMapper mapper) {
        List<Category> cs = mapper.list();
        for (Category c : cs) {
            System.out.println(c.getName());
        }
    }
}

Step 5: the MySQL table must be of type InnoDB

In mysql, transactions are only supported when the table type is InnoDB, so you need to classify the table_ The type of is set to InnoDB, otherwise the transaction cannot be observed

Modify the SQL of table type InnoDB:

alter table category_ ENGINE  = innodb;

SQL to view the type of table

show table status from how2java;

However, there is a premise that the current MySQL server itself should support InnoDB. If not, please see the method to enable MySQL InnoDB

Mybatis series (18) - related concepts - transaction management

For more information, click to learn: https://how2j.cn/k/mybatis/mybatis-annotation-transaction/1097.html