java excel实现导入到数据库的功能 整合springmvc mybatis 处理
项目需求分析
EXCEL导入功能开发要求
一 、功能概述
本系统是一个在线考试系统,其中在试题录入时需要使用Excel导入题库,题库在导入时需要用户上传Excel文
件,其文件表头如下:
1. 序号
2. 试题类型:单选 、多选 、填空 、简答
3. 试题分数:
4. 试题选项:
5. 试题答案:
6. 难易程度:1/2/3/4/5
7. 试题分类:试题所属分类
二 、相关数据表
2.1 、试题分类表
CREATE TABLE courseClass
(
cid int PRIMARY KEY NOT NULL, 主键 cname varchar(100), 分类名称
pid int DEFAULT ((0)) 上级分类id
)
2.2 、题库列表
CREATE TABLE questionbank
(
examID int PRIMARY KEY NOT NULL, 主键
courseID int, | 课程id,请留空 | |
examtypeID int, | 分类id | |
examscore int, | 分数 |
|
examvalue varchar(500), 选项 | ||
ecamvalue varchar(500), | 答案 | |
examdifficulty int, 难易程度 | ||
userid int, | 用户id | |
examcontent varchar(8000), 试题内容 | ||
EXAMCLASS varchar(20) | 类别:EXAMCLASS.JIANDA,EXAMCLASS.DANXUAN |
,EXAMCLASS.DUOXUAN ,EXAMCLASS.TIANKONG
)
spring 整合mybatis配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!-- spring mybitis的配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/proplem?useUnicode=true&characterEncoding=utf-8"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- mybatis的sessionFactoty -->
<bean id="sqlSessionFactorya" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:coursemapper.xml"></property>
</bean>
<bean id="sqlSessionFactoryb" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:questionbankmapper.xml"></property>
</bean>
<bean id="courseClassDaoMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.xiong.excelimport.dao.courseClassDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactorya" />
</bean>
<bean id="questionbankMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.xiong.excelimport.dao.questionbankDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactoryb" />
</bean>
<bean id="excelParser" class="com.xiong.excelimport.services.impl.ExcelParser">
<property name="courseClassDao" ref="courseClassDaoMapper"></property>
</bean>
<bean id="excalSave" class="com.xiong.excelimport.services.impl.ExcelSaver">
<property name="questionbankDao" ref="questionbankMapper"></property>
</bean>
<bean id="excelimport" class="com.xiong.excelimport.ExcelImporter">
<property name="excelSaver" ref="excalSave"></property>
<property name="excelParser" ref="excelParser"></property>
</bean>
</beans>
<?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.xiong.excelimport.dao.courseClassDao">
<select id="getcidBycname" parameterType="java.lang.String" resultType="int">
SELECT pid FROM courserclass WHERE cname=#{cname}
</select>
</mapper>
<?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.xiong.excelimport.dao.questionbankDao">
<insert id="save" parameterType="java.util.HashMap">
<selectKey order="BEFORE" keyProperty="examID" resultType="int">
SELECT IFNULL(MAX(examID),0)+1 from qustionbank
</selectKey>
INSERT INTO qustionbank(examID,courseID,examTypeID,examscore,examvalue,ecamvalue,userid,examcontent,examclass,defficulty)
VALUES(#{examID},#{courseID},#{examtypeid},#{examscore},#{examvalue} ,#{excmvalue},#{userid},#{examcontent},#{examclass},#{defficulty});
</insert>
</mapper>
package com.xiong.excelimport.bean;
/**
* Created by Administrator on 2016/11/18.
*/
public class CourseFenlei {
private int cid;
private String fenleiname;
private int pid;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getFenleiname() {
return fenleiname;
}
public void setFenleiname(String fenleiname) {
this.fenleiname = fenleiname;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
}
package com.xiong.excelimport.bean;
/**
* Created by Administrator on 2016/11/18.
*/
public class QustionBank {
private int examID;
private int courseID;
private int examTypeID;
private String fenlei;
private int examScore;
private String examValue;
private String excmValue;
private int examDifficulty;
private int userid;
private String examContent;
public static final String JIANDA="JIANDA";
public static final String DANXUAN="DANXUAN";
public static final String DUOXUAN="DUOXUAN";
public static final String TIANKONG="TIANKONG";
public int getExamID() {
return examID;
}
public void setExamID(int examID) {
this.examID = examID;
}
public int getCourseID() {
return courseID;
}
public void setCourseID(int courseID) {
this.courseID = courseID;
}
public int getExamTypeID() {
return examTypeID;
}
public void setExamTypeID(int examTypeID) {
this.examTypeID = examTypeID;
}
public int getExamScore() {
return examScore;
}
public void setExamScore(int examScore) {
this.examScore = examScore;
}
public String getExamValue() {
return examValue;
}
public void setExamValue(String examValue) {
this.examValue = examValue;
}
public String getExcmValue() {
return excmValue;
}
public void setExcmValue(String excmValue) {
this.excmValue = excmValue;
}
public int getExamDifficulty() {
return examDifficulty;
}
public void setExamDifficulty(int examDifficulty) {
this.examDifficulty = examDifficulty;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getExamContent() {
return examContent;
}
public void setExamContent(String examContent) {
this.examContent = examContent;
}
public String getFenlei() {
return fenlei;
}
public void setFenlei(String fenlei) {
this.fenlei = fenlei;
}
}
package com.xiong.excelimport.dao;
/**
*课程分类的dao
*
*/
public interface courseClassDao {
/**
*根据分类名称获取分类id
*
*/
int getcidBycname(String cname);
}
package com.xiong.excelimport.dao;
import java.util.Map;
public interface questionbankDao {
/**
*保存题库到数据库
*/
int save(Map arow);
}
package com.xiong.excelimport.services.impl;
import com.xiong.excelimport.bean.QustionBank;
import com.xiong.excelimport.dao.courseClassDao;
import com.xiong.excelimport.services.ExcelParserInterface;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
/**
*实现类
*/
public class ExcelParser implements ExcelParserInterface {
private courseClassDao courseClassDao;
/**
*@see com.xiong.excelimport.services.ExcelParserInterface#parseExcel(String)
*/
public List parseExcel(String fileName){
List list=new ArrayList();
QustionBank qustionBank=null;
try{
HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(fileName));
HSSFSheet sheet = workbook.getSheet("题库");
int rows=sheet.getPhysicalNumberOfRows();
for(int i=1;i<rows;i++){
qustionBank=new QustionBank();
List listqustion=new ArrayList();
HSSFRow row = sheet.getRow(i);
if(row!=null){
int cells=row.getPhysicalNumberOfCells();
for(int c=0;c<cells;c++){
HSSFCell cell = row.getCell(c);
if(cell!=null){
if(cell.getCellType()== HSSFCell.CELL_TYPE_STRING){
listqustion.add(cell.getStringCellValue());
}else if(cell.getCellType()== HSSFCell.CELL_TYPE_NUMERIC){
Double num = cell.getNumericCellValue();
listqustion.add(num.intValue());
}
}
}
}else {
System.out.println("表中没有数据请插入");
}
if(listqustion.get(1).toString().equals("单选")){
listqustion.set(1,QustionBank.DANXUAN);
}else if(listqustion.get(1).toString().equals("多选")){
listqustion.set(1,QustionBank.DUOXUAN);
}else if(listqustion.get(1).toString().equals("简答")){
listqustion.set(1,QustionBank.JIANDA);
}else if(listqustion.get(1).toString().equals("填空")){
listqustion.set(1,QustionBank.TIANKONG);
}
qustionBank.setExamID((Integer) listqustion.get(0));
qustionBank.setFenlei((String) listqustion.get(1));
qustionBank.setExamScore((Integer) listqustion.get(2));
if(listqustion.size()==8){
qustionBank.setExamValue( (String) listqustion.get(3));
qustionBank.setExcmValue((String) listqustion.get(4));
qustionBank.setExamDifficulty((Integer) listqustion.get(5));
qustionBank.setExamContent((String) listqustion.get(7));
qustionBank.setExamTypeID(courseClassDao.getcidBycname((String)listqustion.get(6)));
}else if(listqustion.size()==7){
qustionBank.setExamValue(null);
qustionBank.setExcmValue((String) listqustion.get(3));
qustionBank.setExamDifficulty((Integer) listqustion.get(4));
qustionBank.setExamContent((String) listqustion.get(6));
qustionBank.setExamTypeID(courseClassDao.getcidBycname((String)listqustion.get(5)));
}
list.add(qustionBank);
}
}catch (Exception e){
e.printStackTrace();
}
return list;
}
public com.xiong.excelimport.dao.courseClassDao getCourseClassDao() {
return courseClassDao;
}
public void setCourseClassDao(com.xiong.excelimport.dao.courseClassDao courseClassDao) {
this.courseClassDao = courseClassDao;
}
}
package com.xiong.excelimport.services.impl;
import com.xiong.excelimport.bean.QustionBank;
import com.xiong.excelimport.dao.questionbankDao;
import com.xiong.excelimport.services.ExcelParserInterface;
import com.xiong.excelimport.services.ExcelSaverInterface;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*Excel保存的实现类
*/
public class ExcelSaver implements ExcelSaverInterface {
private questionbankDao questionbankDao;
/**
*@see com.xiong.excelimport.services.ExcelSaverInterface#saveToDB(List)
*/
public boolean saveToDB(List<QustionBank> exceldata){
int result=0;
Map<String,Object> map=null;
for(QustionBank qustionBank:exceldata){
map=new HashMap<String, Object>();
map.put("examID",qustionBank.getExamID());
map.put("courseID",qustionBank.getCourseID());
map.put("userid",qustionBank.getUserid());
map.put("examvalue",qustionBank.getExamValue());
map.put("excmvalue",qustionBank.getExcmValue());
map.put("examtypeid",qustionBank.getExamTypeID());
map.put("examclass",qustionBank.getFenlei());
map.put("defficulty",qustionBank.getExamDifficulty());
map.put("examcontent",qustionBank.getExamContent());
map.put("examscore",qustionBank.getExamScore());
result=questionbankDao.save(map);
}
return result==0;
}
public com.xiong.excelimport.dao.questionbankDao getQuestionbankDao() {
return questionbankDao;
}
public void setQuestionbankDao(com.xiong.excelimport.dao.questionbankDao questionbankDao) {
this.questionbankDao = questionbankDao;
}
}
package com.xiong.excelimport.services.utils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringUtils {
private static SpringUtils instance;
private ApplicationContext context;
private SpringUtils() {
context = new ClassPathXmlApplicationContext("applicationcontext.xml");
}
public static SpringUtils getInstance() {
if (instance == null) {
return new SpringUtils();
}
return instance;
}
public BeanFactory getFactory() {
return context;
}
}
package com.xiong.excelimport.services.test;
import java.util.*;
/**
* Created by Administrator on 2016/11/24.
*/
public class TestGnerator {
public static void main(String []args){
Map<String,String> map=new HashMap();
Map map1=new LinkedHashMap();
Set ap2=new HashSet();
Map s=new TreeMap();
map.put("aaa","aaa");
for(Map.Entry <String,String>tntry:map.entrySet()){
System.out.println(tntry.getKey());
System.out.println(tntry.getValue());
}
System.out.println(map);
}
}
package com.xiong.excelimport;
import com.xiong.excelimport.bean.QustionBank;
import com.xiong.excelimport.services.ExcelParserInterface;
import com.xiong.excelimport.services.ExcelSaverInterface;
import java.util.ArrayList;
import java.util.List;
/**
*Excel导入器
*
*/
public class ExcelImporter {
/**
*excel解析器
*
*/
private ExcelParserInterface excelParser;
/**
*Excel保存工具
*/
private ExcelSaverInterface excelSaver;
/**
*保存Excel到数据库
*并返回导入结果
*/
public boolean importExcelToDB(String fileName) {
boolean result=false;
List list=excelParser.parseExcel(fileName);
result=excelSaver.saveToDB(list);
return result;
}
public void setExcelParser(ExcelParserInterface excelParser) {
this.excelParser = excelParser;
}
public void setExcelSaver(ExcelSaverInterface excelSaver) {
this.excelSaver = excelSaver;
}
}
package com.xiong.excelimport.services;
import com.xiong.excelimport.ExcelImporter;
import com.xiong.excelimport.bean.QustionBank;
import java.util.List;
/**
*Excel解析器
*
*/
public interface ExcelParserInterface {
/**
*从Excel文件解析Excel
*返回一个List<Map>对象
*/
List parseExcel(String fileName);
}
package com.xiong.excelimport.services;
import com.xiong.excelimport.ExcelImporter;
import com.xiong.excelimport.bean.QustionBank;
import java.util.List;
/**
*Excel保存器
*将解析器的结果保存至数据库
*
*/
public interface ExcelSaverInterface {
boolean saveToDB(List<QustionBank> exceldata);
}