Hibernate配置

Hibernate基础配置

#测试表
CREATE TABLE Elec_Text(
 textID varchar(50) not null,
 textName varchar(50),
 textDate datetime,
 textRemark varchar(500)
)

ElecText类

package cn.itcast.elec.domain;
import java.util.Date;
/**
 *po持久层对象,对应数据库表elec_text
 *
 */
public class ElecText implements java.io.Serializable{
 private String textID;
 private String textName;
 private Date textDate;
 private String textRemark;
 public String getTextID() {
  return textID;
 }
 public void setTextID(String textID) {
  this.textID = textID;
 }
 public String getTextName() {
  return textName;
 }
 public void setTextName(String textName) {
  this.textName = textName;
 }
 public Date getTextDate() {
  return textDate;
 }
 public void setTextDate(Date textDate) {
  this.textDate = textDate;
 }
 public String getTextRemark() {
  return textRemark;
 }
 public void setTextRemark(String textRemark) {
  this.textRemark = textRemark;
 }
 
}


ElecText.hbm.xml

Hibernate配置
/**
 *Hibernate映射文件,表与持久层的映射关系
 *
 */

<?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="cn.itcast.elec.domain.ElecText" table="elec_text">
  <id name="textID" type="string">
   <column name="textID" sql-type="varchar(50)" not-null="true"/>
   <generator class="uuid"/>
  </id>
  <property name="textName" type="string">
   <column name="textName" sql-type="varchar(50)"/>
  </property>
  <property name="textDate" type="date">
   <column name="textDate" length="50"/>
  </property>
  <property name="textRemark" type="string">
   <column name="textRemark" sql-type="varchar(500)"/>
  </property>
 </class>
</hibernate-mapping>

hibernate.cfg.xml

Hibernate配置
/**
 *Hibernate配置文件,数据库的连接信息
 *
 */
<?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="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/litcast20170801?useUnicode=true&amp;characterEncoding=utf-8</property>
  <property name="hibernate.connection.autocommit">true</property>
 
 
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <property name="hibernate.show_sql">true</property>
  <mapping resource="cn/itcast/elec/domain/ElecText.hbm.xml"/>
 
 
  </session-factory>

</hibernate-configuration>