使用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&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();
}
}
本文包括该类目下的所有文章均为作者学习笔记。