这片主要讲如何使用通用mapper从而简化CRUD操作,后面会有小实例。
一、导入依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
二、替换MapperScannerConfigurer
这里需要替换MapperScannerConfigurer,从而使通用mapper生效。org.mybatis.spring.mapper.MapperScannerConfigurer->tk.mybatis.spring.mapper.MapperScannerConfigurer
<bean id="mapperScannerConfigurer" class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.dao"></property>
</bean>
三、编写通用Mapper和数据接口
import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;
//根据功能还可以进行扩展
public interface Mapper<T>
extends
BaseMapper<T>,
ConditionMapper<T>,
IdsMapper<T>,
InsertListMapper<T> {
}
@Repository
public interface ISelectionMapper extends Mapper<TbSelection> {
}
@Repository
public interface ISubjectionMapper extends Mapper<TbSubjection> {
}
@Repository
public interface IOnlineMapper extends Mapper<TbOnline> {
}
四、实例:完成分卷功能
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class PaperService implements IPaperService {
@Autowired
private ISelectionMapper selectionMapper;
@Autowired
private ISubjectionMapper subjectionMapper;
@Autowired
private IOnlineMapper onlineMapper;
/**
* INFO: 完成组卷功能
*
* @param info 组卷题目数量信息 'selection' 'subjection' 'online'
* @param level 组卷等级信息
* @return
*/
public Map<String, Object> genPaper(Map<String, Integer> info, int level) {
//按题目等级进行分级
TbSelection selection = new TbSelection();
selection.setWeight(new Byte(String.valueOf(level)));
List<TbSelection> selections = selectionMapper.select(selection);
TbSubjection subjection = new TbSubjection();
subjection.setWeight(new Byte(String.valueOf(level)));
List<TbSubjection> subjections = subjectionMapper.select(subjection);
TbOnline online = new TbOnline();
online.setWeight(new Byte(String.valueOf(level)));
List<TbOnline> onlines = onlineMapper.select(online);
//得出不重复序列
Set<Integer> selectionIds = rand(selections.size(), info.get("selection"));
Set<Integer> subjectionIds = rand(subjections.size(), info.get("subjection"));
Set<Integer> onlineIds = rand(onlines.size(), info.get("online"));
//取得不重复题目列表
List<TbSelection> selectionList = new ArrayList<>();
List<TbSubjection> subjectionList = new ArrayList<>();
List<TbOnline> onlineList = new ArrayList<>();
for (Integer item : selectionIds) {
TbSelection tmp = selections.get(item);
selectionList.add(tmp);
}
for (Integer item : subjectionIds) {
TbSubjection tmp = subjections.get(item);
subjectionList.add(tmp);
}
for (Integer item : onlineIds) {
TbOnline tmp = onlines.get(item);
onlineList.add(tmp);
}
//对最终生成的题目信息进行封装s
Map<String, Object> map = new HashMap<>();
map.put("selection", selectionList);
map.put("subjection", subjectionList);
map.put("online", onlineList);
return map;
}
/**
* INFO: 利用Set的无序性生成随机不重复序列
* @param range 指定序列的范围
* @param n 指定序列中元素的数量
* @return
*/
public Set<Integer> rand(int range, int n){
Set<Integer> set = new LinkedHashSet(n);
Random random = new Random();
while (set.size() < n) {
int i = random.nextInt(range);
set.add(i);
}
return set;
}
}