jboss数据源配置

添加一个mysql的数据源配置如下
1、导入mysql的jdbc驱动包到jboss-5.1.0.GA\server\default\lib这个目录中

2、放置在jboss-5.1.0.GA\server\default\deploy\mysql-ds.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
    <jndi-name>victopDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password>liyh</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>
3、ejb3中,实体bean项目中,配置持久化文件persistence.xml为
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="victopDS" transaction-type="JTA">
<jta-data-source>java:victopDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="showSql" value="true" />
</properties>
</persistence-unit>
</persistence>

4、编写 实体 User
5、接口
6、实现类
7、jndi配置jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost\:1099
java.naming.factory.url.pkgs=org.jboss.naming\:org.jnp.interfaces
7、测试方法
public class TestUser {
@Test
public void save(){
InitialContext context;
try {
context = new InitialContext();
IUserDao userDao=(IUserDao)context.lookup("UserDaoImpl/remote");
User user = new User();
user.setName("liyh");
user.setAge(25);
user.setSex(true);
userDao.save(user);
} catch (NamingException e) {
e.printStackTrace();
}


}
}

你可能感兴趣的:(java,mysql,xml,jdbc,jboss)