SSH框架-持久层搭建
SSH框架-持久层搭建
1、首先编写javabean对象(也就是配置数据库字段)
package com.caicai.elec.doamin; import java.util.Date; public class ElecText implements java.io.Serializable { //定义数据库字段 private String textID; private String textName; private Date textDate; private String textRemark; //配置get set 方法 public String getTextID() { return textID; } public String getTextName() { return textName; } public Date getTextDate() { return textDate; } public String getTextRemark() { return textRemark; } public void setTextID(String textID) { this.textID = textID; } public void setTextName(String textName) { this.textName = textName; } public void setTextDate(Date textDate) { this.textDate = textDate; } public void setTextRemark(String textRemark) { this.textRemark = textRemark; } }
2、编写数据库映射配置文件(*.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.caicai.elec.doamin.ElecText" table="Elec_Text"> <id name="textID" type="java.lang.String"> <column name="textID" not-null="true" sql-type="varchar(50)"/> <generator class="uuid"/> </id> <property name="textName" type="java.lang.String"> <column name="textName" sql-type="varchar(50)"/> </property> <property name="TextDate" type="java.util.Date"> <column name="TextDate" length="50"/> </property> <property name="textRemark" type="java.lang.String"> <column name="textRemark" sql-type="varchar(500)"/> </property> </class> </hibernate-mapping>
3、编写Hibernate连接数据库配置(hibetnate.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="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/caicai2021elec</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="com/caicai/elec/doamin/EcecText.hbm.xml"/> </session-factory> </hibernate-configuration>
4、编写测试用例
package junit; import com.caicai.elec.doamin.ElecText; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import java.util.Date; public class TestHibernate { @Test public void testElecText(){
//加载配置文件 Configuration config = new Configuration();
//加载hibernate.cfg.xml 默认在跟目录下 config.configure(); //创建seessionFactory SessionFactory sf = config.buildSessionFactory(); //打开session 操作数据库 Session session = sf.openSession(); //开启事务 Transaction tr = session.beginTransaction(); //实例化对象 ElecText elecText = new ElecText(); elecText.setTextName("Hibernate_test_name111"); elecText.setTextRemark("Hibernate success insert!11111"); elecText.setTextDate(new Date()); //保存 session.save(elecText); //提交事务 tr.commit(); //关闭会话 session.close(); } }
文件架构
开开心心,上班!
快快乐乐,游玩!
及时行乐!