mybatis entry (b)

mybatis entry (b)

Explore the mapping process sql statement

Small examples to explore the process of sql execution, look at a simple

<?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>

The above is related to a mapper xml file, the method corresponding to the class corresponding to BlogMapper may be configured, the above example is

selectBlog this method to find according to id.

We can be called using the fully qualified name based on the namespace, as follows:

Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);

Mapping the specified class or, more convenient to look

BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);

In addition to xml way, we also can be configured with a comment, looks very clear, but slightly more complicated sql of such an approach may be even more complicated is not intuitive. But since there are many ways natural advantages and disadvantages, everything is not seeking a balance

package org.mybatis.example;
public interface BlogMapper {
  @Select("SELECT * FROM blog WHERE id = #{id}")
  Blog selectBlog(int id);
}

Scope and Life Span

For the life cycle, most of us will inject initialization mybatis with mybatis and spring and is dependent on the use of thread-safe, so we do not need a lot of time to manage his life cycle

SqlSessionFactoryBuilder

This class can be instantiated, and disposal, SqlSessionFactory once created, it is no longer needed. Thus preferred examples of the scope is a method SqlSessionFactoryBuilder scope (i.e. a local variable method). You can reuse SqlSessionFactoryBuilder SqlSessionFactory to create multiple instances, but it is best not to have been allowed to exist, to ensure that all of the XML parsing resources can be freed to more important things.

SqlSessionFactory

Once created SqlSessionFactory should have been present during the execution of the application, there is no reason to discard it or re-create another instance. The best practice is to use SqlSessionFactory during application runtime do not create duplicate many times rebuilt many times SqlSessionFactory be seen as a code for "bad taste (bad smell)". Therefore the best scope SqlSessionFactory is the application scope. There are many ways to do this, the easiest is to use the Singleton pattern or Static Singleton pattern.

SqlSession

Each thread should have its own SqlSession instance. Examples of SqlSession not thread-safe, thus it can not be shared, so it is best request or method scope scope. Never keep references to SqlSession instance of a static field in class, or even a class instance variables does not work. Never keep references to a SqlSession hosted in any type of scope, such as Servlet framework HttpSession. If you are currently using one Web framework, and to consider the SqlSession a HTTP request object similar scope. In other words, HTTP requests received each time, one can open the SqlSession, returns a response, it is closed. The closing operation is very important, you should put this in close operation in the finally block to ensure that every time a shutdown. The following example is a reference pattern to ensure close SqlSession

Guess you like

Origin www.cnblogs.com/yixinli/p/11622357.html
Recommended