JBoss Hibernate 配置与应用

1 配置一个mysql的数据源

从%JBOSS_HOME%\docs\examples\jca目录下复制一份mysql-ds.xml到server\default\deploy目录下,修改其中的内容。

code list 1

<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: mysql-ds.xml,v 1.3.2.3 2006/02/07 14:23:00 acoliver Exp $ -->
<!--  Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->

<datasources>
  <local-tx-datasource>
    <jndi-name>MySqlDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK</connection-url>
    <driver-class>org.gjt.mm.mysql.Driver</driver-class>
    <user-name>root</user-name>
    <password></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>

 

2 做一个jboss_hibernate.har目录放到%JBOSS_HOME%\server\default\deploy下

该目录下放的将是你的POJO 文件和对应的.hbm.xml文件以及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>

 3 接着写hibernate文件

在%JBOSS_HOME%\server\default\deploy\jboss_hibernae.har\目录下新建文件夹com.dhgate,我们写一个对应数据库的User.java文件:

package com.dhgate;   
  
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.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="com.dhgate.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>  

 4 写一个登录页面
进入到%JBOSS_HOME%\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");      
}   
  
%>  

 好了大功告成,我们来启动一下jboss,运行run.bat
打开http://localhost:8080/login.jsp看一下,是不是显示Login success 啊

你可能感兴趣的:(java,Hibernate,mysql,jboss,SQL Server)