Building SqlSessionFactory from XML
Building an instance of SqlSessionFactory from an XML file is very simple. Here we recommend that you use the resource file under the classpath to configure it.
String resource = "org/mybatis/example/Configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlMapper = new SqlSessionFactoryBuilder().build(reader);
The XML configuration file contains the core settings for the MyBatis system, including the data source for obtaining the database connection instance and the transaction manager that determines the scope and control of the transaction. As an example:
<?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>Of course, there are many configurable things in the XML configuration file, and the example above points out the most critical part.
Get SqlSession from SqlSessionFactory
Now that we already know how to obtain the SqlSessionFactory object, based on the same revelation, we can obtain an instance of SqlSession. The SqlSession object completely contains all methods for performing SQL operations with a database as the background. You can use the SqlSession instance to directly execute mapped SQL statements. For example:
SqlSession session = sqlMapper.openSession(); try{ Blog blog = (Blog)session.selectOne("org.mybatis.example.BlogMapper.selectBlog",101); } finally{ session.close(); }Now there is a simpler way. Use interfaces that reasonably describe parameters and SQL statements to return values (such as BlogMapper.class), so that now it is simpler and safer code without prone to string literals and conversion errors. For example:
SqlSession session = sqlSessionFactory.openSession(); try { BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = mapper.selectBlog(101); } finally{ session.close(); } Explore mapped SQL statements
Here is an example of XML mapping statements that should satisfy the call of the SqlSession object in the above example.
<?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" parameterType="int" resultType="Blog"> select * from Blog where id = #{id} </select> </mapper>In the namespace "com.mybatis.example.BlogMapper", it defines a mapping statement named "selectBlog", which allows you to call the mapping statement using the fully qualified name "org.mybatis.example.BlogMapper.selectBlog", which is what we write in the following example.
Blog blog = (Blog)session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101); But the following calls have more advantages:
The mapping interface corresponds to the command space for mapping the xml file, and the interface method corresponds to the ID of the SQL map defined in the mapping xml file. ?????????????????
BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = mapper.selectBlog(101);
First of all, it is not based on text, so it is safer. Second, if your IDE has code completion function, you can use it to manipulate mapped SQL statements. Third, there is no need to cast type conversion. At the same time, the BlogMapper interface can be kept simple and the return value type is very safe (the parameter type is also very safe).