编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

泛型简介,泛型类及使用「实例」「持续更新」

wxchong 2024-07-26 22:33:10 开源技术 14 ℃ 0 评论

泛型简介,泛型类及使用

实例180929

UserServiceImpl.java

/**
 *
 * User 表数据服务层接口实现类
 *
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService {
	@Autowired
	private IRoleService roleService;
	@Log("登录")
	@Override
	public User selectByLoginName(String loginName) {
		User user = new User();
		user.setLoginName(loginName);
		return super.selectOne(new EntityWrapper<User>(user));
	}
	@Log("删除用户")
	@Override
	public void deleteUser(Long userId) {
		// 删除用户角色,再删除用户
		roleService.deleteByUserId(userId);
		super.deleteById(userId);
	}
	@Log("添加用户")
	public boolean insert(User entity) {
		System.err.println(" 覆盖父类方法 ");
		return super.insert(entity);
	}
}

B1.BaseServiceImpl<M extends BaseMapper<T>, T>

public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {
}

B2.略

U1.UserMapper

public interface UserMapper extends BaseMapper<User> {
}

U2.BaseMapper<T>

/**
 * <p>
 * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
 * </p>
 * <p>
 * 这个 Mapper 支持 id 泛型
 * </p>
 * 
 * @author hubin
 * @Date 2016-01-23
 */
public interface BaseMapper<T> {
	/**
	 * <p>
	 * 插入一条记录
	 * </p>
	 * 
	 * @param entity
	 * 实体对象
	 * @return int
	 */
	Integer insert(T entity);
	/**
	 * <p>
	 * 根据 ID 删除
	 * </p>
	 * 
	 * @param id
	 * 主键ID
	 * @return int
	 */
	Integer deleteById(Serializable id);
	/**
	 * <p>
	 * 根据 columnMap 条件,删除记录
	 * </p>
	 * 
	 * @param columnMap
	 * 表字段 map 对象
	 * @return int
	 */
	Integer deleteByMap(@Param("cm") Map<String, Object> columnMap);
	/**
	 * <p>
	 * 根据 entity 条件,删除记录
	 * </p>
	 * 
	 * @param wrapper
	 * 实体对象封装操作类(可以为 null)
	 * @return int
	 */
	Integer delete(@Param("ew") Wrapper<T> wrapper);
	/**
	 * <p>
	 * 删除(根据ID 批量删除)
	 * </p>
	 * 
	 * @param idList
	 * 主键ID列表
	 * @return int
	 */
	Integer deleteBatchIds(List<? extends Serializable> idList);
	/**
	 * <p>
	 * 根据 ID 修改
	 * </p>
	 * 
	 * @param entity
	 * 实体对象
	 * @return int
	 */
	Integer updateById(T entity);
	/**
	 * <p>
	 * 根据 whereEntity 条件,更新记录
	 * </p>
	 * 
	 * @param entity
	 * 实体对象
	 * @param wrapper
	 * 实体对象封装操作类(可以为 null)
	 * @return
	 */
	Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> wrapper);
	/**
	 * <p>
	 * 根据 ID 查询
	 * </p>
	 * 
	 * @param id
	 * 主键ID
	 * @return T
	 */
	T selectById(Serializable id);
	/**
	 * <p>
	 * 查询(根据ID 批量查询)
	 * </p>
	 * 
	 * @param idList
	 * 主键ID列表
	 * @return List<T>
	 */
	List<T> selectBatchIds(List<? extends Serializable> idList);
	/**
	 * <p>
	 * 查询(根据 columnMap 条件)
	 * </p>
	 * 
	 * @param columnMap
	 * 表字段 map 对象
	 * @return List<T>
	 */
	List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
	/**
	 * <p>
	 * 根据 entity 条件,查询一条记录
	 * </p>
	 * 
	 * @param entity
	 * 实体对象
	 * @return T
	 */
	T selectOne(@Param("ew") T entity);
	/**
	 * <p>
	 * 根据 Wrapper 条件,查询总记录数
	 * </p>
	 * 
	 * @param wrapper
	 * 实体对象
	 * @return int
	 */
	Integer selectCount(@Param("ew") Wrapper<T> wrapper);
	/**
	 * <p>
	 * 根据 entity 条件,查询全部记录
	 * </p>
	 * 
	 * @param wrapper
	 * 实体对象封装操作类(可以为 null)
	 * @return List<T>
	 */
	List<T> selectList(@Param("ew") Wrapper<T> wrapper);
	/**
	 * <p>
	 * 根据 Wrapper 条件,查询全部记录
	 * </p>
	 *
	 * @param wrapper
	 * 实体对象封装操作类(可以为 null)
	 * @return List<T>
	 */
	List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> wrapper);
	/**
	 * <p>
	 * 根据 Wrapper 条件,查询全部记录
	 * </p>
	 *
	 * @param wrapper
	 * 实体对象封装操作类(可以为 null)
	 * @return List<Object>
	 */
	List<Object> selectObjs(@Param("ew") Wrapper<T> wrapper);
	/**
	 * <p>
	 * 根据 entity 条件,查询全部记录(并翻页)
	 * </p>
	 * 
	 * @param rowBounds
	 * 分页查询条件(可以为 RowBounds.DEFAULT)
	 * @param wrapper
	 * 实体对象封装操作类(可以为 null)
	 * @return List<T>
	 */
	List<T> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);
	/**
	 * <p>
	 * 根据 Wrapper 条件,查询全部记录(并翻页)
	 * </p>
	 *
	 * @param rowBounds
	 * 分页查询条件(可以为 RowBounds.DEFAULT)
	 * @param wrapper
	 * 实体对象封装操作类
	 * @return List<Map<String, Object>>
	 */
	List<Map<String, Object>> selectMapsPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表