hibernate学习笔记1
来源:互联网 发布:程序员出差多吗 编辑:程序博客网 时间:2024/05/01 21:43
首先要先加入hibernate.cfg.xml配置文件
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="connection.url">jdbc:mysql://127.0.0.1:3306/s31</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.username">root</property><property name="connection.password">admin</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><mapping resource="com/pojo/student.hbm.xml" /></session-factory></hibernate-configuration>如果要查看日志,则可以加入log4j;然后加入jar包。基本的配置就这样
二、
编写pojo类并创建*****.hbm.xml映射。例如pojo--student类为
package com.pojo;import java.io.Serializable;import java.util.Date;public class Student implements Serializable{private Integer id;private String name;private Date birthday;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}
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> <!-- 类和表的映射 --> <class name="com.pojo.Student" table="stu"> <!-- 主键和属性映射 --> <id name="id" column="sid"> <generator class="identity"></generator> </id> <!-- 其它字段和属性映射 --> <property name="name" column="sname" length="32"></property> <property name="birthday" column="sbirthday"></property> </class></hibernate-mapping>
三、编写测试类
此方法可以使用hibernate自动创建表
package com.test;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.jboss.mx.util.SchedulableRunnable;public class 创建 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub//读取总的配置文件Configuration cfg = new Configuration().configure("hibernate.cfg.xml");//创建数据表SchemaExport export = new SchemaExport(cfg);//export.create(true, true);}}用hibernate对数据进行增删改查。
package com.test;import java.util.Date;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.pojo.Student;public class 总结 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubConfiguration configuration = new Configuration().configure();SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();// 查询不需要开启事物,提交事物// 查询// String hql = " from Student where name like ? ";// String hql =" from Student ";//查询所有的学生// Query query = session.createQuery(hql);// //解释hql语句中的?是什么值// query.setParameter(0, "小%");// // List<Student> list = query.list();// for (Student student : list) {// System.out.println(student.getId()+"\t"+student.getName());// }/** * 除了查询外,其他的增删改都要开启事物; */// 3创建事物Transaction tx = session.beginTransaction();// 增加// Student student = new Student();// student.setName("全zai");// student.setBirthday(new Date());// //5保存对象// session.save(student);/* * 修改 */// Student student = (Student) session.get(Student.class, 1);//// if (student==null)// System.out.println("查无此人");// else// {// Transaction transaction = session.beginTransaction();////// student.setName("小红");//// session.update(student);//// transaction.commit();////// }///** * 删除 */// String hql = "delete from Student where name like ? ";// Query query= session.createQuery(hql);// query.setParameter(0, "小%");// query.executeUpdate();// 6事物提交tx.commit();// 7关闭sessionsession.close();}}
0 0
- hibernate学习笔记1
- Hibernate学习笔记(1)
- Hibernate 学习笔记 1
- hibernate学习笔记1
- Hibernate学习笔记1
- Hibernate学习笔记1
- Hibernate学习笔记1
- hibernate学习笔记-1
- hibernate学习笔记(1)
- hibernate学习笔记 1
- Hibernate学习笔记(1)
- Hibernate学习笔记1
- hibernate学习笔记1
- Hibernate学习笔记1
- Hibernate学习笔记1
- hibernate学习笔记1
- hibernate学习笔记(1)
- Hibernate学习笔记1
- 历届试题 连号区间数
- cocos2d-x项目移植
- Codeforces Round #176 (Div. 2)
- 各种排序算法的分析及java实现
- 历届试题 翻硬币
- hibernate学习笔记1
- Blender编译
- 史上最全-第三方应用连接亚马逊云存储AWS S3的研究总结
- C# 系统应用之EM安全卫士总结及源码分享
- 历届试题 错误票据
- python module--poplib
- Codeforces Round #181 (Div. 2)
- gdb调试时的问题Missing separate debuginfos, use: debuginfo-install glibc-XXX(转载)
- 多Webview并发执行缓存数据在2.3版本上出现的SQLiteDiskIOException 备忘