当前位置: 移动技术网 > IT编程>开发语言>Java > hibernate初使用

hibernate初使用

2020年03月11日  | 移动技术网IT编程  | 我要评论

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>

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网