(1)开发环境搭建-持久层

使用Hibernate连接mysql数据库。
1.创建测试连接的数据库、表

create TABLE Elec_Text(
    textID varchar(50) not null primary key,
    textName varchar(50),
    textDate DateTime,
    textRemark varchar(500)
)

2.导入开发相应的包
3.持久层
根据数据库创建相应的实体类TestElec.java

public class TestElec 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;
    }

}

4.创建映射文件
在相应的目录下 建立TestElec.java对应的映射文件ElecText.hbm.xml(在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="com.itheima.elec.domain.TestElec" table="Elec_Text">
        <id name="textID" type="string" column="textID">
            <generator class="uuid"></generator>

        </id>
        <property name="textName" type="string" column="textName"></property>
        <property name="textDate" type="date" column="textDate"></property>
        <property name="textRemark" type="string" column="textRemark"></property>
    </class>
</hibernate-mapping>

5.创建配置文件
(5) 在src先创建hibernate.cfg.xml的配置文件(用于连接数据库和加载映射文件等)

<?xml version="1.0" encoding="UTF-8"?>
<!-- 下面的约束文件 可以从对应的hibernate包中找到 -->
<!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.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itheima1128elec?useUnicode=true&amp;characterEncoding=utf8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">674768166ws</property>

    <!-- 其他配置 -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.hbm2dd1.auto">update</property>
    <property name="hibernate.hbm2dd1.show_sql">true</property>

    <!-- 添加映射 -->
    <mapping resource="com/itheima/elec/domain/ElecText.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

6.测试
使用junit在test包中进行测试(可以在此时导入log4j的配置文件)

public class Testhibernate {
    // 进行测试
    @Test
    public void save() {

        Configuration config = new Configuration();
        config.configure();// 加载classpath下hibernate.cfg.xml
        SessionFactory sf = config.buildSessionFactory();// 工厂
        Session s = sf.openSession();
        Transaction tr = s.beginTransaction();// 开启一个事务
        TestElec et = new TestElec();
        et.setTextName("事业");
        et.setTextDate(new Date());
        et.setTextRemark("这是一个测试");
        s.save(et);

        tr.commit();
        s.close();
    }
}

本文包括该类目下的所有文章均为作者学习笔记。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值