使用hibernate进行CURD操作

gradle配置相关jar包

dependencies {
    // https://mvnrepository.com/artifact/org.testng/testng
    compile group: 'org.testng', name: 'testng', version: '6.9.10'
    // https://mvnrepository.com/artifact/org.hibernate/hibernate-core
    compile group: 'org.hibernate', name: 'hibernate-core', version: '4.2.3.Final'
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'

}


编写HibernateUtil工具

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	private static Session session;
	private static Transaction transaction;
	
	//static静态代码在类加载时初始化,在构造方法之前初始化
	static{
		//创建配置对象
		Configuration config=new Configuration().configure();
		//创建服务注册对象
		ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
		//创建会话工厂对象
		sessionFactory=config.buildSessionFactory(serviceRegistry);
		//会话对象
		session=sessionFactory.openSession();
		//创建事务
		transaction=session.beginTransaction();
		
	}
	
	public static Session getSession(){
		return session;
	}
	
	public static void close(Session session){
		transaction.commit();//提交事务
		if(session!=null) {
			session.close();//关闭会话
			sessionFactory.close();//关闭会话工厂
		}
		
	}
}


hibernate.cfg.xml配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- DB schema will be updated if needed -->
        <property name="show_sql">true</property><!--展示sql语句-->
        <property name="format_sql">true</property><!--格式化sql语句-->
        <property name="hbm2ddl.auto">update</property><!--帮助java代码生成数据库脚本,进而生成具体的表结构-->

        <mapping resource="hbm/students.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

student.hbm.xml 映射配置文件

<?xml version="1.0" encoding='UTF-8'?>

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.sun.dto">
    <class name="Student" table="t_secondhand_pic">
        <id name="iAutoID" type="int">
            <column name="iAutoID"/>
            <generator class="increment"/>
        </id>
        <property name="iSecondhandID" type="int">
            <column name="iSecondhandID"/>
        </property>
        <property name="sImgKey" type="string">
            <column name="sImgKey"/>
        </property>
        <property name="sExt" type="string">
            <column name="sExt"/>
        </property>
        <property name="iDefault" type="int">
            <column name="iDefault"/>
        </property>
        <property name="iStatus" type="int">
            <column name="iStatus"/>
        </property>
        <property name="iCreateTime" type="int">
            <column name="iCreateTime"/>
        </property>
        <property name="iUpdateTime" type="int">
            <column name="iUpdateTime"/>
        </property>
        <property name="iImgType" type="int">
            <column name="iImgType"/>
        </property>
        <property name="iRank" type="int">
            <column name="iRank"/>
        </property>

        <!--配置单向的一对多关联关系-->
        <!--<set name="sts" table="t_secondahnd_pic">
            <!–指定关联的外键列–>
            <key column="iAutoID"/>
            <one-to-many class="com.sun.dto.Student"/>
        </set>-->
    </class>


</hibernate-mapping>

创建Student学生类

public class Student {
	private int iAutoID;
	private int iSecondhandID;
	private String sImgKey;
	private String sExt;
	private int iDefault;
	private int iStatus;
	private int iCreateTime;
	private int iUpdateTime;
	private int iImgType;
	private int iRank;
	
	public Student() {
		
	}
	
	public Student(int iSecondhandID, String sImgKey, String sExt, int iDefault, int iStatus, int iCreateTime, int iUpdateTime, int iImgType, int iRank) {
		this.iSecondhandID = iSecondhandID;
		this.sImgKey = sImgKey;
		this.sExt = sExt;
		this.iDefault = iDefault;
		this.iStatus = iStatus;
		this.iCreateTime = iCreateTime;
		this.iUpdateTime = iUpdateTime;
		this.iImgType = iImgType;
		this.iRank = iRank;
	}
	
	public int getiAutoID() {
		return iAutoID;
	}
	
	public void setiAutoID(int iAutoID) {
		this.iAutoID = iAutoID;
	}
	
	public int getiSecondhandID() {
		return iSecondhandID;
	}
	
	public void setiSecondhandID(int iSecondhandID) {
		this.iSecondhandID = iSecondhandID;
	}
	
	public String getsImgKey() {
		return sImgKey;
	}
	
	public void setsImgKey(String sImgKey) {
		this.sImgKey = sImgKey;
	}
	
	public String getsExt() {
		return sExt;
	}
	
	public void setsExt(String sExt) {
		this.sExt = sExt;
	}
	
	public int getiDefault() {
		return iDefault;
	}
	
	public void setiDefault(int iDefault) {
		this.iDefault = iDefault;
	}
	
	public int getiStatus() {
		return iStatus;
	}
	
	public void setiStatus(int iStatus) {
		this.iStatus = iStatus;
	}
	
	public int getiCreateTime() {
		return iCreateTime;
	}
	
	public void setiCreateTime(int iCreateTime) {
		this.iCreateTime = iCreateTime;
	}
	
	public int getiUpdateTime() {
		return iUpdateTime;
	}
	
	public void setiUpdateTime(int iUpdateTime) {
		this.iUpdateTime = iUpdateTime;
	}
	
	public int getiImgType() {
		return iImgType;
	}
	
	public void setiImgType(int iImgType) {
		this.iImgType = iImgType;
	}
	
	public int getiRank() {
		return iRank;
	}
	
	public void setiRank(int iRank) {
		this.iRank = iRank;
	}
	
	@Override
	public String toString() {
		return "Student{" +
				"iAutoID=" + iAutoID +
				", iSecondhandID=" + iSecondhandID +
				", sImgKey='" + sImgKey + '\'' +
				", sExt='" + sExt + '\'' +
				", iDefault=" + iDefault +
				", iStatus=" + iStatus +
				", iCreateTime=" + iCreateTime +
				", iUpdateTime=" + iUpdateTime +
				", iImgType=" + iImgType +
				", iRank=" + iRank +
				'}';
	}
}

编写Student测试类

public class StudentTest {
	private Session session;
	
	@BeforeClass
	public void init() {
		session= HibernateUtil.getSession();
	}
	
	@Test
	public void run() {
		Student student=new Student();
		student.setiDefault(2);
		student.setsImgKey("222");
		student.setsExt("jpg");
		//session.save(student);
		//student= (Student) session.get(Student.class,17699669);
		//System.out.println(student);
		Query query=session.createQuery("from Student where iAutoID in :ids");
		List list=new ArrayList();
		list.add(1);
		list.add(3);
		list.add(5);
		list.add(6);
		list.add(10);
		
		query.setParameterList("ids",list);
		
		query.list().forEach(s -> System.out.println(s));
	}
	
	@AfterClass
	public void destory() {
		HibernateUtil.close(session);
	}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值