1. Configure the database
Create the mybatis configuration file and configure the 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> <!-- 加载类路径下的属性文件 --> <properties resource="db.properties"/> <!-- 设置一个默认的连接环境信息 --> <environments default="mysql_developer"> <!-- 连接环境信息,取一个任意唯一的名字 --> <environment id="mysql_developer"> <!-- mybatis使用jdbc事务管理方式 --> <transactionManager type="jdbc"/> <!-- mybatis使用连接池方式来获取连接 --> <dataSource type="pooled"> <!-- 配置与数据库交互的4个必要属性 --> <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> <!-- 连接环境信息,取一个任意唯一的名字 --> <environment id="oracle_developer"> <!-- mybatis使用jdbc事务管理方式 --> <transactionManager type="jdbc"/> <!-- mybatis使用连接池方式来获取连接 --> <dataSource type="pooled"> <!-- 配置与数据库交互的4个必要属性 --> <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
MyBatis's SqlSessionFactory interface can also be created programmatically through the Java API in addition to using XML-based configuration creation. 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 already existing XML.
The above is the detailed content of How to configure the java mybatis framework. For more information, please follow other related articles on the PHP Chinese website!