搭建ssh框架项目(一)

一、创建web项目

搭建ssh框架项目(一)

二、导入jar包

搭建ssh框架项目(一)

三、创建数据库(MySQL)

搭建ssh框架项目(一)

四、建立javaBean对象(ElecText.java),属于持久层对象(PO对象)

package com.cppdy.ssh.domain;

import java.util.Date;

/**
*
* PO持久层对象,对应数据库表Elec_Text
*
*/ @SuppressWarnings("serial")
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.java

五、创建映射文件(ElecText.hbm.xml),建立PO对象与数据库表Elec_Text的关联关系

<?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.cppdy.ssh.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>

ElecText.hbm.xml

六、创建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="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/ssh</property>
<!-- 配置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 无表自动创建,有表自动追加数据 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 映射文件 -->
<mapping resource="com/cppdy/ssh/domain/ElecText.hbm.xml"/>
</session-factory>
</hibernate-configuration>

hibernate.cfg.xml

七、创建测试类(TestHibernate.java)

package junit;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; import com.cppdy.ssh.domain.ElecText; public class TestHibernate { @Test
public void testElecText(){
Configuration config = new Configuration();
config.configure();
//创建sessionFactory对象
SessionFactory sf = config.buildSessionFactory();
//打开session,操作数据库
Session session = sf.openSession();
//开启事务
Transaction tr = session.beginTransaction();
//实例化ElecText对象,添加数据,执行保存操作
ElecText elecText = new ElecText();
elecText.setTextName("测试Hibernate名称");
elecText.setTextDate(new Date());
elecText.setTextRemark("测试Hibernate备注");
//保存
session.save(elecText);
//提交事务
tr.commit();
session.close();
} }

TestHibernate.java

八、数据库自动创建表并添加数据

搭建ssh框架项目(一)

项目结构:

搭建ssh框架项目(一)

上一篇:Hide C# winform App Window When Started by Task Scheduler


下一篇:Swing-JCheckBox用法-入门