What is the method for configuring XML in MyBatis?
The methods for configuring XML in MyBatis include:
- Tags can be used to define reusable SQL snippets, which can then be referenced in other SQL statements.
“Could you please explain that again?”
Could you repeat that please?
<!-- 定义SQL语句 -->
<select id="getUserById" parameterType="int" resultType="User">
SELECT * FROM user WHERE id = #{id}
</select>
<!-- 定义参数映射 -->
<insert id="insertUser" parameterType="User">
INSERT INTO user (id, username, password) VALUES (#{id}, #{username}, #{password})
</insert>
<!-- 定义结果映射 -->
<resultMap id="userMap" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>
- Configuration file: XML file is used to configure MyBatis global configuration information, such as data sources, plugins, type handlers, etc.
“我想去购物。”
Option: I want to go shopping.
<!-- 配置数据源 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 配置插件 -->
<plugins>
<plugin interceptor="com.example.MyPlugin">
<property name="property1" value="value1"/>
</plugin>
</plugins>
<!-- 配置类型处理器 -->
<typeHandlers>
<typeHandler handler="com.example.MyTypeHandler"/>
</typeHandlers>
The above are two common methods of XML configuration in MyBatis. By using these methods, the mapping relationship between SQL statements and Java objects can be defined in XML files, achieving more flexible and maintainable data access.