Mybatis注解方式基本用法

在Mybatis注解SQL中,最基本的就是@Select、@Insert、@Update、@Delete

  • @Select注解

RoleMapper接口:

package tk.mybatis.simple.mapper;

import org.apache.ibatis.annotations.Select;

import tk.mybatis.simple.model.SysRole;

public interface RoleMapper {
	//通过角色id查询角色
	@Select({"select id, role_name roleName, enabled, create_by createBy, create_time createTime from "
			+ "sys_role where id = #{id}"})
	SysRole selectById(Long id);
}

测试代码:

@Test
public void testSelectById(){
	SqlSession sqlSession = getSqlSession();
	try{
		//获取UserMapper接口
		RoleMapper userMapper = sqlSession.getMapper(RoleMapper.class);
		//调用selectById方法  查询id = 1的角色
		SysRole role = userMapper.selectById(1L);
		//user不为空
		Assert.assertNotNull(role);
		//roleName = 管理员
		Assert.assertEquals("管理员", role.getRoleName());
	}finally {
		sqlSession.close();
	}
}

你可能感兴趣的:(Mybatis)