The use of a framework is inevitably inseparable from the component support within it. After we download the mybatis framework, because most of the internal structures have not been started yet, we need to configure it manually. As mentioned before, the role of the mybatis framework is in the database, so this article brings the configuration methods of database and sql. Let's take a look at the specific operations below.
1. Configure the database
Create a mybatis configuration file and configure database information. We can configure multiple databases, but only one can be used by default.
<?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> <!-- Load the properties file under the class path --> <properties resource="db.properties"/> <!--Set a default connection environment information --> <environments default="mysql_developer"> <!-- Connect environment information, give an arbitrary unique name --> <environment id="mysql_developer"> <!-- mybatis uses jdbc transaction management method --> <transactionManager type="jdbc"/> <!-- mybatis uses connection pooling to obtain connections --> <dataSource type="pooled"> <!-- Configure 4 necessary attributes for interacting with the database --> <property name="driver" value="${mysql.driver}"/> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> </dataSource> </environment> <!-- Connect environment information, give an arbitrary unique name --> <environment id="oracle_developer"> <!-- mybatis uses jdbc transaction management method --> <transactionManager type="jdbc"/> <!-- mybatis uses connection pooling to obtain connections --> <dataSource type="pooled"> <!-- Configure 4 necessary attributes for interacting with the database --> <property name="driver" value="${oracle.driver}"/> <property name="url" value="${oracle.url}"/> <property name="username" value="${oracle.username}"/> <property name="password" value="${oracle.password}"/> </dataSource> </environment> </environments> </configuration>
2. Configure SqlSessionFactory
In addition to being created using XML-based configuration, the SqlSessionFactory interface of MyBatis can also be created programmatically through the Java API. Every element configured in XML can be created programmatically.
Use Java API to create SqlSessionFactory, the code is as follows:
public static SqlSessionFactory getSqlSessionFactoryUsingJavaAPI() { if (javaSqlSessionFactory == null) { try { DataSource dataSource = DataSourceFactory.getDataSource(); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.getTypeAliasRegistry().registerAlias("student", Student.class); configuration.getTypeHandlerRegistry().register(PhoneTypeHandler.class); configuration.addMapper(StudentMapper.class); javaSqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); } catch (Exception e) { throw new RuntimeException(e); } } return javaSqlSessionFactory; }
In this configuration, a mapping class is loaded. Mapping classes are Java classes that contain SQL mapping annotations and can be used to replace XML. However, due to some limitations of Java annotations and the complexity of MyBatis mapping, some advanced mappings still need to be configured using XML, such as nested mappings. For this reason, MyBatis will automatically find and load existing XML.
The above is the configuration of the mybatis framework in Java. It can be said that after assembling the database and sql, the mybatis framework can play a role in this aspect. After everyone downloads it, please hurry up and make relevant configurations.