statement 标签可根据 SQL 执的业务选择 insert、delete、update、select。
MyBatis 框架会根据规则动创建接实现类的代对象。
规则:
Mapper.xml 中 namespace 为接的全类名。
Mapper.xml 中 statement 的 id 为接中对应的法名。
Mapper.xml 中 statement 的 parameterType 和接中对应法的参数类型致。
Mapper.xml 中 statement 的 resultType 和接中对应法的返回值类型致。
3、在 config.xml 中注册 AccountRepository.xml
4、调接的代对象完成相关的业务操作
<?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="com.southwind.repository.AccountRepository">
<insert id="save" parameterType="com.southwind.entity.Account">
insert into t_account(username,password,age) values(#{username},#
{password},#{age})
</insert>
<update id="update" parameterType="com.southwind.entity.Account">
update t_account set username = #{username},password = #{password},age
= #{age} where id = #{id}
</update>
<delete id="deleteById" parameterType="long">
delete from t_account where id = #{id}
</delete>
<select id="findAll" resultType="com.southwind.entity.Account">
select * from t_account
</select>
<select id="findById" parameterType="long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
</mapper>
<!-- 注册AccountMapper.xml -->
<mappers>
<mapper resource="com/southwind/mapper/AccountMapper.xml"></mapper>
<mapper resource="com/southwind/repository/AccountRepository.xml"></mapper>
</mappers>
package com.southwind.test;
import com.southwind.entity.Account;
import com.southwind.repository.AccountRepository;