码迷,mamicode.com
首页 > 其他好文 > 详细

搭建ssh框架项目(一)

时间:2018-10-25 00:14:59      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:关系   url   ble   对象   oid   res   root   技术分享   数据库   

一、创建web项目

技术分享图片

二、导入jar包

技术分享图片

三、创建数据库(MySQL)

技术分享图片

四、建立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框架项目(一)

标签:关系   url   ble   对象   oid   res   root   技术分享   数据库   

原文地址:https://www.cnblogs.com/cppdy/p/9846517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!