Explore mapped SQL statements
Here you may want to know exactly what was done with the sqlsession and Mapper objects. The mapped SQL statement is a big topic, and this topic runs through most of this document. Here are some examples to give a macroscopic overview . Any of the examples mentioned above, statements are defined by XML or annotations. Let's look at XML first. the use of XML-based mapping languages has made MyBatis very popular in the last few years, and he has provided all the feature settings for MyBatis. If you've used MyBatis before, the concept should be familiar, but the XML mapping file also has a lot of improvements, which we'll look at in detail later. Here is an example of an XML-based mapping statement that should
This can satisfy the invocation 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>
This simple example looks like a lot of extra stuff, but it's pretty neat. You can define many mapping statements in a single XML mapping file, and you can get a lot of convenience in addition to the XML header and document type declarations . The remainder of the file is a good self-explanatory. In the namespace "Com.mybatis.example.BlogMapper", it defines a mapping statement named "Selectblog", which allows you to use the fully qualified name " Org.mybatis.example.BlogMapper.selectBlog "to invoke the mapping statement, we have all the same notation in the following example.
Blog blog = (blog) session.selectone (
"Org.mybatis.example.BlogMapper.selectBlog", 101);
Note that this method of invoking a Java object using a fully qualified name is similar, for a reason. This name can be given directly to the mapping class under the same namespace, using a name, the same method as the parameter and the return value and the mapped query statement. This allows you to easily invoke the method in the Mapper interface, which is the same as what you saw earlier , and it appears again in the following example.
Blogmapper mapper = Session.getmapper (Blogmapper.class);
Blog blog = mapper.selectblog (101);
The second way has a lot of a bit, first it is not based on text, then it is more secure. Second, if your IDE has code completion capabilities, you can use it to manipulate mapped SQL statements. Thirdly, there is no need to force type swapping, while the Blogmapper interface can be kept concise, the return value type is safe (the parameter type is also very safe).
Mybatis XML Mapping configuration file