Building Sqlsessionfactory from XML
MyBatis-based applications are centered on an instance of Sqlsessionfactory. Examples of sqlsessionfactory can be obtained by Sqlsessionfactorybuilder. Sqlsessionfactorybuilder, however, can build an instance of Sqlsessionfactory from an XML configuration file or an instance of a pre-customized configuration.
It is very simple to build an instance of Sqlsessionfactory from an XML file, which is recommended to be configured using a resource file under the classpath. However, you can also use any of the input stream (InputStream) instances, including file paths in the form of strings or file paths in the form of file://URLs. MyBatis contains a tool class called resources, which contains practical methods to make it easier to load resource files from Classpath or other locations.
String resource = "Mybatis.xml"=new Sqlsessionfactorybuilder (). Build (InputStream);
The XML configuration file (Mybatis.xml) contains the core settings for the MyBatis system, and here is a simple 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> <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <TransactionManagertype= "JDBC" /> <!--Configure database connection information - <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver" /> < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/mybatis" /> < Propertyname= "username"value= "root" /> < Propertyname= "Password"value= "root" /> </DataSource> </Environment> </Environments> <mappers> <MapperResource= "Com/oumyye/mapping/usermapping.xml"/> </mappers></Configuration>
Of course, there are many more that can be configured in an XML file, and the above example indicates the most critical part. Note the declaration of the XML header to verify the correctness of the XML document. The environment element body contains the configuration of transaction management and connection pooling. The mappers element consists of a set of mapper mappers (these mapper XML files contain SQL code and mapping definition information).
Build sqlsessionfactory without using XML
If you prefer to create a configuration directly from a Java program rather than an XML file, or create your own configuration builder, MyBatis also provides a complete configuration class that provides all the same configuration items as the XML file.
DataSource DataSource = Blogdatasourcefactory.getblogdatasource (); Transactionfactory transactionfactory = new Jdbctransactionfactory (); Environment Environment = new Environment ("Development" , transactionfactory, DataSource); Configuration Configuration = new class ); Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (configuration);
Note In this example, the configuration adds a mapper class (Mapper classes). Mapper classes are Java classes that contain annotations to SQL mapping statements to avoid dependency on XML files. However, due to some limitations of Java annotations combined with the complexity of some MyBatis mappings, XML mappings are still necessary for most advanced mappings, such as nested Join mappings. In view of this, if there is a peer-to XML configuration file, MyBatis will automatically find and load it (in this case, Blogmapper.xml will be loaded based on the Classpath and Blogmapper.class class name).
Get sqlsession from Sqlsessionfactory
Now that we have sqlsessionfactory, as the name implies, we can get an example of sqlsession from it. Sqlsession completely contains all the methods required to execute SQL commands against the database. You can execute the mapped SQL statement directly from the sqlsession instance. For example:
sqlsession session = sqlsessionfactory.opensession (); Try { = (Blog) session.selectone ("Org.mybatis.example.BlogMapper.selectBlog", 101finally { session.close ();}
It is true that this approach works and is familiar to users who use the older version of MyBatis, but now has a more straightforward approach. With interfaces that can reasonably describe parameters and return values for a given statement (for example, Blogmapper.class), you can now execute more clear and type-safe code without worrying about the error-prone string literals and coercion of type conversions. For example:
sqlsession session = sqlsessionfactory.opensession (); Try { = Session.getmapper (blogmapper. Class); = Mapper.selectblog (101finally { session.close ();}
Above transfer from: http://mybatis.github.io/mybatis-3/zh/getting-started.html
Java Framework Chapter---Mybatis Build sqlsessionfactory