【Java】MyBatis应用(二)Mapper代理自定义接口

1.自定义接口

package com.mybatistest.repository;
import com.mybatistest.entity.Account;
import java.util.List;
public interface AccountRepository {
public int sava(Account account); //增
public int update(Account account); //修改
public int delete(long id); //删除
public List<Account> fundAll(); //查询
public Account findById(long id); //根据ID 查询
}

2.创建接口对应的Mapper.xml,定义接口方法对应的SQL语句
statement标签可根据SQL执行的业务选择INSERT、DELETE、UPDATE、SELECT。
MyBatis框架会根据规则来自动创建接口的实现类的代理对象
规则:
· Mapper.xml中namespa为接口的全类名。
· Mapper.xml中statement的id为接口的方法名。
· Mapper.xml中statement的paramentrType和接口追踪对应的方法的参数类型一致。
· Mapper.xml中statementdresultType和接口中对应方法的返回类型一致。
<?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.mybatistest.repository.AccountRepository">
<insert id="sava" parameterType="com.mybatistest.entity.Account">
insert into t_accout(username,password,age) values (#{username},#{password},#{age})
</insert>
<update id="update" parameterType="com.mybatistest.entity.Account">
update t_accout set username = #{username},password = #{password},age = #{age} where id = #{id}
</update>
<delete id="deleteById" parameterType="long">
delete from t_accout where id = #{id}
</delete>
<select id="findAll" resultType="com.mybatistest.entity.Account">
select * from t_accout;
</select>
<select id="findById" parameterType="long" resultType="com.mybatistest.entity.Account">
select * from t_accout where id = #{id}
</select>
</mapper>

3. 在config.xml中注册AccountRepository.xml
<!-- 注册AccountMapper.xml -->
<mappers>
<mapper resource="com/mybatistest/mapper/AccountMapper.xml"></mapper>
<mapper resource="com/mybatistest/repository/AccountRepository.xml"></mapper>
</mappers>

4. 调用接口的代理对象来完成相关的业务
package com.mybatistest.test;
import com.mybatistest.entity.Account;
import com.mybatistest.repository.AccountRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
//加载配置文件
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取实现接口的代理对象
AccountRepository accountRepository = sqlSession.getMapper(AccountRepository.class);
//添加对象
// Account account = new Account(3L,"王五","123123",44);
// accountRepository.sava(account);
// sqlSession.commit();
//查询全部对象
// List<Account> list = accountRepository.findAll();
// for (Account account:list) {
// System.out.println(account);
// }
//根据ID查询对象
// Account account = accountRepository.findById(3L);
// System.out.println(account);
//修改
// Account account = accountRepository.findById(3L);
// account.setUsername("二哈");
// account.setAge(15);
// account.setPassword("12345");
// int result = accountRepository.update(account); //看影响的行数
// sqlSession.commit();
// System.out.println(result);
//通过ID删除对象
// int result = accountRepository.deleteById(3L);
// sqlSession.commit();
// System.out.println(result);
sqlSession.close();
}
}

blank