Mybatis02_通过Mapper代理实现自定义接口

Mybatis02_通过Mapper代理实现自定义接口

  1. 在Mybatis01_使用原生接口开发工程基础上创建⾃定义接⼝AccountRepository,编写增删改查方法:
package com.blu.repository;

import java.util.List;
import com.blu.entity.Account;

public interface AccountRepository {
	
	public int save(Account account);
	public int update(Account account);
	public int deleteById(long id);
	public List<Account> findAll();
	public Account findById(long id);
	public Account findByNameAndAge(String name, int age);
}
  1. 创建接⼝对应的mapper.xml,编写接⼝⽅法对应的 SQL 语句:

AccountRepository.xml:



<mapper namespace="com.blu.repository.AccountRepository">
	<insert id="save" parameterType="com.blu.entity.Account">
		insert into t_account(username,password,age) values(#{username},#{password},#{age})
	insert>
	<update id="update" parameterType="com.blu.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.blu.entity.Account">
		select * from t_account
	select>
	<select id="findById" parameterType="java.lang.Long" resultType="com.blu.entity.Account">
		select * from t_account where id = #{id}
	select>
	<select id="findByNameAndAge" resultType="com.blu.entity.Account">
 		select * from t_account where username = #{arg0} and age = #{arg1}
	select>
mapper>

mapper编写规则:

namespace用于指定接口的全类名
insert, delete, update, select四个标签分别对应增删改查
id用于指定接口中的方法名

parameterType用于指定接口中的方法参数类型
注:parameterType可以写基本数据类型也可以写成包装类,区别是基本数据类型不接受null值
当参数不为1时,不需写parameterType,在sql语句中通过索引值取参。
可以写成#{arg0},#{arg1},也可以写成#{param1},#{param2}
arg的下标从0开始,param的下标从1开始

resultType用于指定接口中的方法的返回值类型
添加、修改和删除方法有默认的int类型的resultType(影响的行数)
注:findAll方法的resultType是List中的泛型类型,而不是List类型
  1. 在全局配置⽂件 config.xml 中注册AccountRepository.xml:
<mappers>
	<mapper resource="com/blu/mapper/AccountRepository.xml">mapper>
mappers>
  1. save测试方法:
public class Testsave {
	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(2L,"李四","111111",24);
		int result = accountRepository.save(account);
		//涉及到对数据库进行修改的操作必须要提交事务
		sqlSession.commit();
		//关闭连接
		sqlSession.close();
	}
}

运行结果:数据库新增了一条记录
在这里插入图片描述

  1. findById测试方法:
public class TestfindById {
	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 = accountRepository.findById(2L);
		System.out.println(account);
		sqlSession.close();
	}
}

结果:

Account(id=2, username=李四, password=111111, age=24)
  1. findAll测试方法:
public class TestfindAll {
	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);
		List<Account> list = accountRepository.findAll();
		for (Account account : list){
		    System.out.println(account);
		}
		sqlSession.close();
	}
}

结果:

Account(id=1, username=张三, password=123123, age=22)
Account(id=2, username=李四, password=111111, age=24)
  1. update测试方法:
public class Testupdate {
	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 = accountRepository.findById(2L);
		account.setUsername("⼩明");
		account.setPassword("123456");
		account.setAge(18);
		int result = accountRepository.update(account);
		sqlSession.commit();
		System.out.println(result);
		sqlSession.close();
	}
}

运行结果:数据库中id为2的数据被修改
在这里插入图片描述
8. deleteById测试方法:

public class TestdeleteById {
	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);
		int result = accountRepository.deleteById(2L);
		sqlSession.commit();
		System.out.println(result);
		sqlSession.close();
	}
}

运行结果:数据库中id为2的数据被删除
在这里插入图片描述
9. findByNameAndAge测试方法:

public class TestfindByNameAndAge {
	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 = accountRepository.findByNameAndAge("张三", 22);
		System.out.println(account);
		sqlSession.close();
	}
}

结果:

Account(id=1, username=张三, password=123123, age=22)

你可能感兴趣的:(Mybatis)