Official Mybatis documentation - getting started
introduction
First Mybatis program
First, the HelloWorld of mybatis. To use mybatis, you need to do the following steps:
1. To import jar, pom XML, i.e. maven for import
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency>
2. There are two ways to build SqlSessionFactory. One is through xml files, and the other is using Java classes. The first kind of xml files are mostly used. The official website provides a template
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>
It should be noted that:
-
First, the ${url} here is read from the properties file, so it needs to be configured
<properties resource="db.properties"></properties>
The db Properties, also under the resources directory.
One of my configurations here is:
<?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> <properties resource="db.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${m-driver}"/> <property name="url" value="${m-url}"/> <property name="username" value="${m-username}"/> <property name="password" value="${m-password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/BlogMapper.xml"/> </mappers> </configuration>
The directory structure is:
Now that we have configured the xml file, the next step is to create an example of SqlSessionFactory.
String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
It refers to the resources get..... This class and method comes with mybatis.
The third step is to obtain SqlSession from SqlSessionFactory.
try (SqlSession session = sqlSessionFactory.openSession()) { Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101); }
This is the first method to execute sql, and the second method is
try (SqlSession session = sqlSessionFactory.openSession()) { BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = mapper.selectBlog(101); }
The codes involved are as follows:
-
Blog
@Data @ToString public class Blog { private String id; private String title; private String author; private Date createTime; private int views; }
-
BlogMapper
public interface BlogMapper { Blog selectBlog(String id); }
-
BlogMapper.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="com.lvgj.mapper.BlogMapper"> <select id="selectBlog" resultType="com.lvgj.pojo.Blog"> select * from blog where id = #{id} </select> </mapper>
-
Create database
CREATE TABLE `blog` ( `id` varchar(50) NOT NULL COMMENT 'Blog id', `title` varchar(100) NOT NULL COMMENT 'Blog title', `author` varchar(30) NOT NULL COMMENT 'Blogger', `create_time` datetime NOT NULL COMMENT 'Creation time', `views` int(30) NOT NULL COMMENT 'Views' ) ENGINE=InnoDB DEFAULT CHARSET=utf8
Answers you should get:
Explore mapped SQL statements
File:
<?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="org.mybatis.example.BlogMapper"> <select id="selectBlog" resultType="Blog"> select * from Blog where id = #{id} </select> </mapper>
Namespace function: one is to use a longer fully qualified name to separate different statements. At the same time, it also implements the interface binding you saw above. In the long run, as long as the namespace is placed in the appropriate Java package namespace, your code will become cleaner and easier for you to use MyBatis.
Finally, the above file can also use comments instead of:
package org.mybatis.example; public interface BlogMapper { @Select("SELECT * FROM blog WHERE id = #{id}") Blog selectBlog(int id); }
Scope and lifecycle
SqlSessionFactoryBuilder
Scope: once the SqlSessionFactory is created, it can be destroyed, so it should be a local method scope
SqlSessionFactory
Once created, it should always exist during the running of the application. There is no reason to abandon it, so it should be a single instance. The best scope for SqlSessionFactory is the application scope
SqlSession
Each thread should have its own SqlSession instance. The best scope is the request or method scope. It is absolutely forbidden to put the reference of SqlSession instance in the static field of a class, or even the instance variable of a class. Be sure to close it after use
Mapper example: getMapper()
Mapper instances should be obtained in the methods that call them, and can be discarded after use.