Detailed tutorial on the use of Mybatis
- 2020-05-19 04:56:47
- OfStack
jar package required by Mybatis:
You need to reference two jar packages, one is
mybatis
The other one is
MySQL-connector-Java
, if it is an maven project, it is ok to add dependencies in pom as follows.
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
Data preparation:
Build point data in MySQL for testing:
CREATE DATABASE mybatis_test;
CREATE TABLE user
(
age INTEGER NOT NULL,
name VARCHAR(64) NOT NULL DEFAULT ''
);
insert user values(18,'zhanjindong');
insert user values(20,'zhangsan');
Profile:
There are two types of configuration files required. The first type is mybatis-config.xml, which is a very simple configuration.
<?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>
<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<typeAlias alias="User" type="test.mybatis.User"/>
</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://192.168.71.38:3306/mybatis_test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/UserMapper.xml" />
</mappers>
</configuration>
The other class is the data access interface mapping file: UserMapper.xml in the example. This file can be found under src/main/resource or in the subdirectory mybatis.xml is specified by resource of the mappers/mapper node in mybatis-config.xml.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test.mybatis.UserMapper">
<!-- Here, namespace It must be UserMapper Interface path" -->
<insert id="insertUser" parameterType="User">
insert into user(name,age) values(#{name},#{age})
<!-- Here, sql No semicolons at the end, otherwise" ORA-00911 "Error -->
</insert>
<!-- Here, id Must and UserMapper The interface method name in the interface is the same -->
<select id="getUser" resultType="User" parameterType="java.lang.String">
select * from user where name=#{name}
</select>
</mapper>
The corresponding mapping file is the UserMapper interface under the namespace test.mybatis, which only defines the interface to access the data table:
package test.mybatis;
public interface UserMapper {
public void insertUser(User user);
public User getUser(String name);
}
You need 1 POJO: User.java
package test.mybatis;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public User() {
super();
}
}
Testing:
The database operation by MyBatis USES a class called SqlSession, which is generated by SqlSessionFactory. It is recommended to maintain 1 SqlSessionFactory globally.
TestMyBatis.java
package test.mybatis;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private final static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
System.out.println(e.getMessage());
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
The test code is as follows:
TestMyBatis.java
package test.mybatis;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class TestMyBatis {
static SqlSessionFactory sqlSessionFactory = null;
static {
sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
}
public static void main(String[] args) {
testAdd();
getUser();
}
public static void testAdd() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User("lisi", new Integer(25));
userMapper.insertUser(user);
sqlSession.commit();// Here, 1 Must submit, otherwise the data will not go into the database
} finally {
sqlSession.close();
}
}
public static void getUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUser("zhangsan");
System.out.println("name: " + user.getName() + "|age: "
+ user.getAge());
} finally {
sqlSession.close();
}
}
}
1. mybatis logs using log4j, but turning debug on seems to have a significant performance impact.
2. The query cache of mybatis has a great impact on performance. The difference between enabled and not enabled is very large
The above is the site for you to introduce the detailed use of Mybatis tutorial, I hope to help you, if you have any questions welcome to give me a message, this site will timely reply to you!