欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

hibernate初使用,

来源: javaer 分享于  点击 39740 次 点评:153

hibernate初使用,


src下新建com.hibernate:

package com.hibernate;

public class Tb_emp1 {
private Integer id;
private String name;
private Integer deptId;
private float salary;

public Tb_emp1(){}

public Integer getId(){
return id;
}
public void setId(Integer id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public Integer getDeptId(){
return deptId;
}
public void setDeptId(Integer deptId){
this.deptId=deptId;
}
public float getSalary(){
return salary;
}
public void setSalary(float salary){
this.salary = salary;
}

}

 然后:

Tb_emp.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>
<!-- name代表的是类名,table代表的是表名 -->
<class name="com.hibernate.Tb_emp1" table="tb_emp1">
<!-- name代表的是User类中的id属性,column代表的是user表中的主键id -->
<id name="id" column="id">
<!-- 主键生成策略 -->
<generator class="native" />
</id>
<!-- 其他属性使用property标签映射 -->
<property name="name" column="name" type="java.lang.String" />

<property name="deptId" type="integer" column="deptId" />   //column:对应数据库中的字段
<property name="salary" type="float" column="salary" />       //name对应bean中的属性
</class>
</hibernate-mapping>

在src新建:

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="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<!-- 链接数据库url -->
<property name="connection.url">
<![CDATA[jdbc:mysql://localhost:3306/db?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=utf-8]]>  //mysql连接url
</property>
<!-- 连接数据库的用户名 -->
<property name="connection.username">
root
</property>
<!-- 数据库的密码 -->
<property name="connection.password">
admin
</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">
com.mysql.cj.jdbc.Driver
</property>
<!-- 显示sql语句 -->
<property name="show_sql">
true
</property>
<!-- 格式化sql语句 -->
<property name="format_sql">true</property>
<!-- 映射文件配置 -->
<mapping resource="com/hibernate/Tb_emp1.hbm.xml" />   //Tb_emp1.hbm.xml
</session-factory>
</hibernate-configuration>

转到TestAction :

调用select方法:


public String select()throws Exception{

Configuration config= new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session= sessionFactory.openSession();
Transaction transaction= session.beginTransaction();
String hql = "select * from Tb_emp1";
Query query = session.createSQLQuery(hql).addEntity(Tb_emp1.class);



// t1=query.list();
List resultList= query.list();

Iterator iterator = resultList.iterator();

while(iterator.hasNext()){

t1.add((Tb_emp1)iterator.next());

/*Object [] result= (Object[])iterator.next();
System.out.println("and "+result[1]+"and"+result[0]+"and"+result[2]);*/

}

/*Tb_emp1 tb_emp1 = (Tb_emp1) session.get(Tb_emp1.class, 1);
System.out.println(tb_emp1.getId()+""+tb_emp1.getName()+""+tb_emp1.getDeptId()+""+tb_emp1.getSalary());*/
transaction.commit();

session.close();
sessionFactory.close();
return "select";
}

 

struts.xml:

<action name="selectselct" class="com.test.TestAction" method="select">

  <result name="select">/front/selectresult.jsp</result>

  <result  >

</action>

 

相关文章

    暂无相关文章
相关栏目:

用户点评