Get the Sqlsession method in the Java MyBatis Framework Project _java

Source: Internet
Author: User

Building Sqlsessionfactory from XML
It is easy to build an instance of Sqlsessionfactory from an XML file. It is recommended 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 that gets the database connection instance and the transaction manager that determines the transaction scope and control. 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}"/>  
        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's a lot more to be configured in the XML configuration file, and the above example indicates the most critical part.

Get sqlsession from Sqlsessionfactory
Now that we know how to get the Sqlsessionfactory object, we can get an example of sqlsession based on the same revelation. The Sqlsession object fully contains all the methods that perform SQL operations in the context of a database. You can use the Sqlsession instance to execute the mapped SQL statement directly. For example:

sqlsession session = Sqlmapper.opensession ();  
try{  
  blog = (blog) session.selectone ("Org.mybatis.example.BlogMapper.selectBlog",);  
} finally{  
  session.close ();  
}  

Now there is a more concise approach. Interfaces that use reasonably descriptive parameters and return values from SQL statements, such as Blogmapper.class, are now simpler, more secure code, without the easy occurrence of string literals and conversion errors. For example:

sqlsession session = Sqlsessionfactory.opensession ();  
try {  
  Blogmapper mapper = Session.getmapper (blogmapper.class);  
  Blog blog = mapper.selectblog (a);  
finally{  
  session.close ();  

Explore the mapped SQL statements
Here is an example of an XML mapping statement that should be able to satisfy the invocation of the Sqlsession object in the example above.

<?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 called "Selectblog" so that it allows you to use the fully qualified name " Org.mybatis.example.BlogMapper.selectBlog "to invoke the mapping statement, as we have written in the following example.

Blog blog = (blog) session.selectone ("Org.mybatis.example.BlogMapper.selectBlog", 101);  

But the following calls are more advantageous:
The mapping interface corresponds to the command space for mapping an 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's not text-based, so it's more secure. Second, if your IDE has code completion, you can use it to manipulate the mapped SQL statements. Third, you do not need to force type conversions, while Blogmapper interfaces can be kept concise, and the return value type is safe (parameter types are also safe).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.