01 Hibernate测试
来源:互联网 发布:淘宝店铺认证复核2017 编辑:程序博客网 时间:2024/05/01 07:28
创建数据库
- 创建名为powersystem的数据库,使用utf-8编码
- 创建名为elec_text的表,字段如下
//测试表CREATE TABLE Elec_Text( textID VARCHAR(50) NOT NULL PRIMARY KEY, textName VARCHAR(50), textDate DATETIME, textRemark VARCHAR(500))
添加lib
- hibernate3.jar
- dom4j-1.6.1.jar —dom4j读取xml文件包
- mysql-connector-java-3.1.13-bin.jar
- log4j-1.2.11.jar
- commons-logging-1.0.4.jar
- commons-collections-2.1.1jar
- cglib-2.1.3.jar
- asm.jar — cglib需要依赖的jar,ASM字节码库
编辑配置文件
- 创建名为Electric的Web项目
- 拷入所需jar包
- 创建ElecTest类
- 创建同名xml文件(ElecTest.hbm.xml)
- 在src目录下创建hibernate.cfg.xml文件并编辑
public class ElecTest implements Serializable { private static final long serialVersionUID = 1L; 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; }}
<!--ElecTest.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.test.elec.domain.ElecTest" 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>
<!--hiberbate.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.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/powersystem?useUnicode=true&characterEncoding=utf8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">adfds</property> <!-- 设置事务自动提交 <property name="hibernate.connection.autocommit">true</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/test/elec/domain/ElecText.hbm.xml"/> </session-factory></hibernate-configuration>
测试
创建TestHibernate类,使用junit进行测试
@Testpublic void save(){ Configuration configuration=new Configuration(); configuration.configure(); SessionFactory sessionFactory=configuration.buildSessionFactory(); Session s=sessionFactory.openSession(); Transaction tr=s.beginTransaction(); ElecTest elecTest=new ElecTest(); elecTest.setTextName("测试hibernate名称"); elecTest.setTextDate(new Date()); elecTest.setTextRemark("测试hibernate备注"); s.save(elecTest); tr.commit(); s.close();}
运行结果
阅读全文
0 0
- 01 Hibernate测试
- hibernate测试
- Hibernate------测试
- Hibernate测试
- Hibernate测试
- hibernate测试
- Hibernate性能测试
- Hibernate性能测试考证
- Junit4 测试 hibernate dao
- 一个first Hibernate 测试
- Hibernate 测试代码
- org.hibernate.jdbc测试
- Hibernate环境测试
- hibernate 一级缓存测试
- hibernate二级缓存测试
- hibernate测试项目
- hibernate+MySQL性能测试
- hibernate测试环境搭建
- 超级字符串
- html03-盒子模式
- 如何用位运算符(~)和数据运算符(-)来计算表示n + 1和n
- 点九图的制作方法
- java作用域public protected private,以及不写时的区别
- 01 Hibernate测试
- linux中class_create和class_register说明
- Kaggle竞赛:泰坦尼克号灾难数据分析简单案例
- LeetCode
- 虚拟机文件共享
- EDA与VHDL作业(2)
- 火星车开发板”SDR Receiver分析说明
- 黑客丛林之旅
- Linux 下 C/C++ 静态库、动态库的区别