本文以一个jboss_hibernate为例介绍怎么把hibernate部署成一个jboss的service,然后用hibernate 去操作数据库。
配置开发环境
在进行开发之前,需要首先获得JBoss、MySQL、相应数据库的JDBC驱动类库。JBoss可以从www.jboss.org中下载,MySQL 从www.mysql.org 下载
解压jboss到一个文件夹,我们把mysql的jdbc驱动复制到jboss-4.2.0.CR1\server\default\lib下
然后我们配置一个mysql的数据源,从jboss-4.2.0.CR1\docs\examples\jca目录下复制一份mysql-ds.xml到jboss-4.2.0.CR1\server\default\deploy目录下,接着修改它的内容,我修改的如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <datasources>
- <local-tx-datasource>
- <jndi-name>MySqlDS</jndi-name> //jndi名字
- <use-java-context>false</use-java-context>
- <connection-url>jdbc:mysql://10.16.175.137:3306/test</connection-url> //URL地址
- <driver-class>com.mysql.jdbc.Driver</driver-class> //驱动
- <user-name>root</user-name> //用户名
- <password>123456</password> //密码
- <exception-sorter-class-name>
- org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
- </exception-sorter-class-name>
- <metadata>
- <type-mapping>mySQL</type-mapping>
- </metadata>
- </local-tx-datasource>
- </datasources>
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name> //jndi名字
<use-java-context>false</use-java-context>
<connection-url>jdbc:mysql://10.16.175.137:3306/test</connection-url> //URL地址
<driver-class>com.mysql.jdbc.Driver</driver-class> //驱动
<user-name>root</user-name> //用户名
<password>123456</password> //密码
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
这样我们的mysql数据源就配置好了,接下来我们来做一个jboss_hibernate.har文件
进入到jboss-4.2.0.CR1\jboss-4.2.0.CR1\server\default\deploy 新建一个jboss_hibernate.har文件夹,该目录下放的将是你的POJO 文件和对应的.hbm.xml文件以及jboss-service.xml文件。
在jboss_hibernate.har目录新建一个文件夹META-INF,在该目录下我们来写一个jboss-service.xml文件,代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <server>
- <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate">
- <attribute name="DatasourceName">MySqlDS</attribute> //数据源名字
- <attribute name="Dialect">org.hibernate.dialect.MySQLDialect</attribute> //对应hibernate的Dialect
- <attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute> //sessionFactory的jndi名字
- <attribute name="ShowSqlEnabled">true</attribute> //在控制台输出sql语句
- <attribute name="ScanForMappingsEnabled">true</attribute> //自动搜索mapping文件
- </mbean>
- </server>
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate">
<attribute name="DatasourceName">MySqlDS</attribute> //数据源名字
<attribute name="Dialect">org.hibernate.dialect.MySQLDialect</attribute> //对应hibernate的Dialect
<attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute> //sessionFactory的jndi名字
<attribute name="ShowSqlEnabled">true</attribute> //在控制台输出sql语句
<attribute name="ScanForMappingsEnabled">true</attribute> //自动搜索mapping文件
</mbean>
</server>
如果有多个数据库,你可以建多个数据源,然后在jboss-service.xml文件里写上多个mbean,我是这样做的,呵呵,不知道有没有更好的办法
接着我们来写我们的hibernate 文件
在jboss-4.2.0.CR1\jboss-4.2.0.CR1\server\default\deploy\jboss_hibernae.har\目录下新建文件夹org.xredleaf,我们写一个对应数据库的User.java文件:
- package org.xredleaf;
- import java.io.Serializable;
- public class User implements Serializable
- {
- private String id;
- private String name;
- private int age;
- public User(){
- }
- public void setId(String id){
- this.id=id;
- }
- public String getId(){
- return id;
- }
- public void setName(String name){
- this.name=name;
- }
- public String getName(){
- return name;
- }
- public void setAge(int age){
- this.age=age;
- }
- public int getAge(){
- return age;
- }
- }
package org.xredleaf;
import java.io.Serializable;
public class User implements Serializable
{
private String id;
private String name;
private int age;
public User(){
}
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
}
然后将它编译成User.class文件
接着我们写XML映射文件User.hbm.xml 文件:
- <?xml version='1.0' encoding='utf-8'?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="org.xredleaf.User" table="user" >
- <id name="id" column="id" type="java.lang.String" >
- <generator class="uuid.hex"/>
- </id>
- <property name="name" column="name" type="java.lang.String" />
- <property name="age" column="age" type="java.lang.Integer" />
- </class>
- </hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.xredleaf.User" table="user" >
<id name="id" column="id" type="java.lang.String" >
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" type="java.lang.String" />
<property name="age" column="age" type="java.lang.Integer" />
</class>
</hibernate-mapping>
好了,这样我们的环境就搭好了!呵呵,喝口茶!
二、写一个登录页面
进入到jboss-4.2.0.CR1\server\default\deploy\jboss-web.deployer\ROOT.war目录下,新建一个login.jsp,文件如下:
- <%@page contentType="text/html;charset=GB2312" %>
- <%
- javax.naming.InitialContext ctx = new javax.naming.InitialContext();
- org.hibernate.SessionFactory factory = (org.hibernate.SessionFactory)ctx.lookup("java:/hibernate/SessionFactory"); //之前配置的sessionFactory的名字
- org.hibernate.Session se = factory.openSession();
- org.hibernate.Query query=se.createQuery("from org.xredleaf.User user where user.id='111' ");
- java.util.List results=query.list();
- java.util.ListIterator iterator=results.listIterator();
- if(iterator.hasNext()){
- org.xredleaf.User user=(org.xredleaf.User)iterator.next();
- session.setAttribute("user",user);
- out.print("Login success");
- }
- %>
<%@page contentType="text/html;charset=GB2312" %>
<%
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
org.hibernate.SessionFactory factory = (org.hibernate.SessionFactory)ctx.lookup("java:/hibernate/SessionFactory"); //之前配置的sessionFactory的名字
org.hibernate.Session se = factory.openSession();
org.hibernate.Query query=se.createQuery("from org.xredleaf.User user where user.id='111' ");
java.util.List results=query.list();
java.util.ListIterator iterator=results.listIterator();
if(iterator.hasNext()){
org.xredleaf.User user=(org.xredleaf.User)iterator.next();
session.setAttribute("user",user);
out.print("Login success");
}
%>
好了大功告成,我们来启动一下jboss,运行run.bat
打开http://localhost:8080/login.jsp看一下,是不是显示Login success 啊