Hibernate学习笔记(一)【基础配置篇】
一、首先找hibernate.properties文件
?
hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.connection.driver_class=com.mysql.jdbc.Driverhibernate.connection.url=jdbc:mysql://192.168.18.184:3306/SAMPLEDBhibernate.connection.username=roothibernate.connection.password=hibernate.show_sql=true
二、其次找类配置文件 例如:Monkey.hbm.xml
?
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="mypack.Monkey" table="MONKEYS"> <id name="id" column="ID" type="long"> <generator column="NAME" type="string" not-null="true"/> <property name="age" column="AGE" type="int"/> <property name="gender" column="GENDER" type="character"/> </class></hibernate-mapping>
?
三、使用Service类 备注:经过改造后笔记
?
public static SessionFactory sessionFactory; static { try { Configurationconfig = new Configuration(); config.addClass(Monkey.class); sessionFactory =config.buildSessionFactory(); } catch (RuntimeException e) { // TODO: handle exception e.printStackTrace(); throw e; } } public void findAllMonkeys() { Session session = sessionFactory.openSession(); Transaction tx = null; try { tx =session.beginTransaction(); // Query query = session .createQuery("from Monkey as morder by m.id asc") @SuppressWarnings("unchecked") List<Monkey> monkeys= query.list(); // s.save(m); // Monkey m = (Monkey)session.get(Monkey.class, monkey_id); m.setAge(age); // session.delete(monkey); tx.commit(); } catch (RuntimeException e) { // TODO: handle exception if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } }
?
四、升级配置文件 Monkey.hbm.xml
?
1.dynamic-insert="true" dynamic-update="true" 动态版插入更新2.access="field" 不是要get set直接采用属性3.formula 无属性也可以直接查数据库组装数据4.在class中声明mutable=”false” 或 @Immutable 这意味着对该类的更新将会被忽略,不过不会抛出异常,只允许有增加和删除操作。 在class中声明mutable=”false”:insert=允许,delete=允许,update=不允许 在集合中声明mutable=”false” 或 @Immutable 这意味着在这个集合中插入记录或删除孤行是不允许的,否则会抛出异常。只允许更新操作。 不过,如果启用级联删除的话,当父类被删除时,其所有子类也将被删除,即使它是mutable的。 在集合中声明mutable=”false”:insert=不允许,孤行删除=不允许,delete=允许,update=允许
?
?
<?xml version="1.0"?><!DOCTYPE hibernate-mappingPUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="mypack.Monkey" table="MONKEYS" dynamic-insert="true" dynamic-update="true" > <!-- mutable="ture" --> <id name="id"> <generator column="NAME" /> <property name="gender" column="GENDER" access="field" /> <property name="age" column="AGE" /> <property name="avgAge" formula="(select avg(m.AGE) from MONKEYS m)" /> <property name="description" type="text" column="`MONKEY DESCRIPTION`"/> </class></hibernate-mapping>
?
?
五、升级配置hibernate.properties --》hibernate.cfg.xml 不过需显示调用
Service类
public static SessionFactory sessionFactory; static { try { Configuration config = newConfiguration(); config.configure(); sessionFactory = config.buildSessionFactory(); } catch (RuntimeException e) { // TODO: handle exception e.printStackTrace(); throw e; } }
?
?
<?xml version="1.0"encoding="utf-8" ?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/HibernateConfiguration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory ><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/sampledb</property><property name="connection.username">root</property><property name="connection.password">1234</property><property name="show_sql">true</property><mapping resource="mypack/Monkey.hbm.xml" /></session-factory></hibernate-configuration>
?
六、高级综合版 Spring中dao-config.xml xml引用sqlResource.properties
?
//配置文件配置 <bean id="propertyConfigurer" name="code">//直接配置 <bean id="dataSource" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.18.184:3306/moniter?characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="" /> </bean>
?
?
?