题目、题干模块
**这个模块中 有两个与外界相关联的id,也是一对多,而不是多对多 **
1. 题目、题干模块的实体类代码
public class Question {
private String id; //题目id
private String companyId; //所属企业id
private String catalogId; //题目所属目录id
private String remark; //题目简介
private String subject; //题干
private String picture; //图片
private String analysis; //题目分析
private String type; //题目类型 1。单选 2.多选 3.解答
private String difficulty; //难易程度 1极易 2容易 3普通 4困难 5极难
private String isClassic; //是否经典面试题 0:否 1:是
private String state; // 题目状态 0:不可用 1:可用(只有审核通过的题目才可以设置)
private String reviewStatus; //审核状态 -1 审核不通过 0 审核中 1 审核通过
private Date createTime; // 创建日期
private Company company; //企业对象
private Catalog catalog; //目录
2.dao层
public interface QuestionDao {
//保存
int save(Question question);
//删除
int delete(Question question);
//修改
int update(Question question);
//根据ID查询
Question findById(String id);
//查询全部
List<Question> findAll();
}
3.mybatis 映射配置文件
<?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.itheima.dao.store.QuestionDao">
<!--配置实体类属性与数据库列的对应关系-->
<resultMap id="BaseResultMap" type="com.itheima.domain.store.Question">
<id column="id" jdbcType="VARCHAR" property="id"/>
<result column="company_id" jdbcType="VARCHAR" property="companyId"/>
<result column="catalog_id" jdbcType="VARCHAR" property="catalodId"/>
<result column="remark" jdbcType="VARCHAR" property="remark"/>
<result column="subject" jdbcType="VARCHAR" property="subject"/>
<result column="analysis" jdbcType="VARCHAR" property="analysis"/>
<result column="type" jdbcType="VARCHAR" property="type"/>
<result column="difficulty" jdbcType="VARCHAR" property="difficulty" />
<result column="is_classic" jdbcType="VARCHAR" property="isClassic"/>
<result column="state" jdbcType="VARCHAR" property="state" />
<result column="review_status" jdbcType="VARCHAR" property="reviewStatus"/>
<result column="create_time" jdbcType="VARCHAR" property="createTime"/>
<result column="picture" jdbcType="VARCHAR" property="picture"/>
<!-- 这里javaType="com.itheima.domain.store.Course",找的是学科模块中的 company_id 和 catalog_id;在学科模块中也关联着 部门模块。因此可以通过学科模块来关联上两个模块 -->
<association
property="company"
column="company_id"
javaType="com.itheima.domain.store.Course"
select="com.itheima.dao.store.CompanyDao.findById"/>
<association
property="catalog"
column="catalog_id"
javaType="com.itheima.domain.store.Course"
select="com.itheima.dao.store.CatalogDao.findById"/>
</resultMap>
<!--配置查询的列名公共SQL语句-->
<sql id="Base_Column_List">
id,catalod_id,company_id,remark,subject,analysis,type,
difficulty,is_classic,state,review_status,create_time,picture
</sql>
<!--配置查询所有条件-->
<select id="findAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from st_question
order by create_time desc
</select>
<!--配置根据ID查询-->
<select id="findById" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from st_question
where id = #{id,jdbcType=VARCHAR}
</select>
<!--配置根据id删除-->
<delete id="delect" parameterType="java.lang.String">
delete from st_question where id=#{id,jdbcType=VARCHAR}
</delete>
<!--配置全字段添加、保存、插入,某个字段没有值为null-->
<insert id="save" parameterType="com.itheima.domain.store.Question">
insert into st_question(
id,company_id,catalog_id,remark,subject,analysis,type,
difficulty,is_classic,state,review_status,create_time, picture
) values (
#{id,jdbcType=VARCHAR},#{companyId,jdbcType=VARCHAR},
#{catalogId,jdbcType=VARCHAR},#{remark,jdbcType=VARCHAR},
#{subject,jdbcType=VARCHAR},#{analysis,jdbcType=VARCHAR},
#{type,jdbcType=VARCHAR},#{difficulty,jdbcType=VARCHAR},
#{isClassic,jdbcType=VARCHAR},#{state,jdbcType=VARCHAR},
#{reviewStatus,jdbcType=VARCHAR},#{createTime,jdbcType=VARCHAR},
#{picture,jdbcType=VARCHAR}
)
</insert>
<!--配置全字段更新,修改;当提供的数据为null时,数据库数据会被更新为null-->
<update id="update" parameterType="com.itheima.domain.store.Question">
update
st_question
set
company_id = #{companyId,jdbcType=VARCHAR},
catalog_id = #{catalogId,jdbcType=VARCHAR},
remark = #{remark,jdbcType=VARCHAR},
subject = #{subject,jdbcType=VARCHAR},
analysis = #{analysis,jdbcType=VARCHAR},
difficulty = #{difficulty,jdbcType=VARCHAR},
is_classic = #{isClassic,jdbcType=VARCHAR},
picture = #{picture,jdbcType=VARCHAR},
state = #{state,jdbcType=VARCHAR}
where
id = #{id,jdbcType=VARCHAR}
</update>
</mapper>
4. service层接口
public interface QuestionService {
/*问题业务层接口*/
/*
* 添加
* @param question
* @return
* */
String save(Question question, boolean flag);
/*
* 删除
* @param question
* @return
* */
void delete(Question question);
/*
* 修改
* @param question
* @return
* */
void update(Question question, boolean flag);
/*
* 查询单个 根据id查询
* @param 查询条件 id
* @return 查询结果是单个对象 并返回
* */
Question findById(String id);
/*
* 查询全部
* @param
* @return 获取全部数据列表对象
* */
List<Question> findAll( );
/*
* 分页查询
* @param page页码
* @param size 每页显示条数条数总量
* @return
* */
PageInfo findAll(int page, int size);
}
5.service层 实现类
public class QuestionServiceImpl implements QuestionService {
@Override
public String save(Question question, boolean flag) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
//id使用UUID的生成策略来获取
String id = UUID.randomUUID().toString();
question.setId(id);
//设置新创建的题目默认的审核状态为未审核(0)
question.setReviewStatus("0");
question.setCreateTime(new Date());
//检测到前端上传文件了,记录文件名,否则不记录
if(flag){
//设置当前存储的图片名称为id值
question.setPicture(id);
}
//3.调用Dao层操作
questionDao.save(question);
//4.提交事务
TransactionUtil.commit(sqlSession);
return id;
}catch(Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
}finally {
//记录日志
try {
TransactionUtil.close(sqlSession);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void delete(Question question) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
//3.调用Dao层操作
questionDao.delete(question);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch(Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
}finally {
//记录日志
try {
TransactionUtil.close(sqlSession);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void update(Question question, boolean flag) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
//3.调用Dao层操作
questionDao.update(question);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch(Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
}finally {
//记录日志
try {
TransactionUtil.close(sqlSession);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public Question findById(String id) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
//3.调用Dao层操作
return questionDao.findById(id);
}catch(Exception e){
throw new RuntimeException(e);
}finally {
//记录日志
try {
TransactionUtil.close(sqlSession);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public List<Question> findAll() {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
//3.调用Dao层操作
return questionDao.findAll();
}catch(Exception e){
throw new RuntimeException(e);
}finally {
//记录日志
try {
TransactionUtil.close(sqlSession);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public PageInfo findAll(int page, int size) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
//3.调用Dao层操作
PageHelper.startPage(page,size);
List<Question> all = questionDao.findAll();
PageInfo pageInfo = new PageInfo(all);
return pageInfo;
}catch(Exception e){
throw new RuntimeException(e);
}finally {
//记录日志
try {
TransactionUtil.close(sqlSession);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}