67,515
社区成员
发帖
与我相关
我的任务
分享
//测试类
public class test extends BaseDaoImpl<Question> {
@Test
public void fun1(){
Question q=new Question();
q.setQst_id(5);
q.setQst_A("dasdsadas");
System.out.println(q.toString());
super.save(q);
}
}
public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T> {
//代表的是某个实体的类型
private Class<T> entityClass;
@Resource//根据类型注入spring工厂中的会话工厂对象sessionFactory
public void setMySessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
//在父类(BaseDaoImpl)的构造方法中动态获得entityClass
public BaseDaoImpl() {
ParameterizedType superclass = (ParameterizedType) this.getClass().getGenericSuperclass();
//获得父类上声明的泛型数组
Type[] actualTypeArguments = superclass.getActualTypeArguments();
entityClass = (Class<T>) actualTypeArguments[0];
}
public void save(T entity) {
this.getHibernateTemplate().save(entity);
}
public void delete(T entity) {
this.getHibernateTemplate().delete(entity);
}
<!-- 配置LocalSessionFactoryBean,spring提供的用于整合hibernate的工厂bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 注入hibernate相关的属性配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 注入hibernate的映射文件 -->
<property name="mappingLocations">
<list>
<value>classpath:com/itheima/bos/domain/*.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
package cn.itcast.domain;
import java.util.HashSet;
import java.util.Set;
public class Question {
private int qst_id;
private String qst_name;//题目名称
private String qst_type;//题目类型
private String qst_score;//题目分值
private String qst_A;//A选项
private String qst_B;//B选项
private String qst_C;//C选项
private String qst_D;//D选项
private String qst_answer;//答案
private Set<Paper> paper=new HashSet<>();
@Override
public String toString() {
return "Question [qst_id=" + qst_id + ", qst_name=" + qst_name + ", qst_type=" + qst_type + ", qst_score="
+ qst_score + ", qst_A=" + qst_A + ", qst_B=" + qst_B + ", qst_C=" + qst_C + ", qst_D=" + qst_D
+ ", qst_answer=" + qst_answer + ", paper=" + paper + "]";
}
public Question() {
super();
// TODO Auto-generated constructor stub
}
public Question( String qst_name, String qst_type, String qst_score, String qst_A, String qst_B,
String qst_C, String qst_D, String qst_answer) {
super();
this.qst_name = qst_name;
this.qst_type = qst_type;
this.qst_score = qst_score;
this.qst_A = qst_A;
this.qst_B = qst_B;
this.qst_C = qst_C;
this.qst_D = qst_D;
this.qst_answer = qst_answer;
}
public int getQst_id() {
return qst_id;
}
public void setQst_id(int qst_id) {
this.qst_id = qst_id;
}
public String getQst_name() {
return qst_name;
}
public void setQst_name(String qst_name) {
this.qst_name = qst_name;
}
public String getQst_type() {
return qst_type;
}
public void setQst_type(String qst_type) {
this.qst_type = qst_type;
}
public String getQst_score() {
return qst_score;
}
public void setQst_score(String qst_score) {
this.qst_score = qst_score;
}
public String getQst_A() {
return qst_A;
}
public void setQst_A(String qst_A) {
this.qst_A = qst_A;
}
public String getQst_B() {
return qst_B;
}
public void setQst_B(String qst_B) {
this.qst_B = qst_B;
}
public String getQst_C() {
return qst_C;
}
public void setQst_C(String qst_C) {
this.qst_C = qst_C;
}
public String getQst_D() {
return qst_D;
}
public void setQst_D(String qst_D) {
this.qst_D = qst_D;
}
public String getQst_answer() {
return qst_answer;
}
public void setQst_answer(String qst_answer) {
this.qst_answer = qst_answer;
}
public Set<Paper> getPaper() {
return paper;
}
public void setPaper(Set<Paper> paper) {
this.paper = paper;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.domain" >
<class name="Question" table="exam_question" >
<id name="qst_id" type="java.lang.Integer">
<!-- generator:主键生成策略(明天讲) -->
<generator class="native"></generator>
</id>
<property name="qst_name" >
</property>
<property name="qst_type" ></property>
<property name="qst_score" ></property>
<property name="qst_A" ></property>
<property name="qst_B" ></property>
<property name="qst_C" ></property>
<property name="qst_D" ></property>
<property name="qst_answer" ></property>
<!-- 多对多 -->
<set name="paper" table="exam_paper_question">
<key column="qst_id"></key>
<many-to-many class="Paper" column="pap_id"></many-to-many>
</set>
</class>
</hibernate-mapping>