关闭

Struts2+Hibernate4整合

标签: hibernatestruts
330人阅读 评论(0) 收藏 举报
分类:

编译环境:MyEclipse
工具:Maven
数据库:MySQL5.X

一、新建Maven Web项目

File——New——WebProject——勾选Add maven support

二、在Maven中配置依赖包

在pom.xml中添加下列代码

        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- 数据库连接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.8</version>
        </dependency>
        <!-- hibernate 配置 -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>4.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.15.0-GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.1.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.1_spec</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <!-- hibernate 配置结束 -->

        <!-- struts2 配置 -->
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-commons</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-tree</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.19</version>
        </dependency>
        <!-- struts2 配置结束 -->

三、配置struts和hibernate

在src/main/java下添加struts.xml和hibernate.cfg.xml
struts.xml配置如下,将数据库用户名、密码以及数据库的链接修改为本机所使用的

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
    </package>
</struts>

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="connection.username">root</property>
        <!-- 数据库密码 -->

        <property name="connection.password">123456</property>
        <!--驱动程序-->

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- JDBC URL -->

        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/shop</property>
        <!-- SQL方言,这边设定的是MySQL -->

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="show_sql">true</property>

        <property name="format_sql">true</property>

        <property name="hbm2ddl.auto">update</property>

        <property name="current_session_context_class">thread</property>
    <!--映射文件添加处-->
    </session-factory>
</hibernate-configuration>

到这里整合已经完成

三、新建实体类和hibernate映射文件

例如新建一个User类,成员变量如下,创建构造方法和get、set方法

    private int uid;
    private String username;
    private String password;

接着,在User类所在的包下,新建映射文件User.hbm.xml

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

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">


<hibernate-mapping>
    <class name="entity.User" table="user">
        <id name="uid" type="int"><!-- 主键 -->
            <generator class="native" /><!-- 自动增长 -->
        </id>
        <property name="username" type="java.lang.String" />
        <property name="password" type="java.lang.String" />
    </class>
</hibernate-mapping>

再将映射文件User.hbm.xml的路径添加到hibernate.cfg.xml

        <mapping resource="entity/User.hbm.xml"/>

四、根据映射文件创建数据库表

在src/test/java下创建一个Test类

public class Test {

    @Test
    public void testSchemaExport(){
        //创建配置对象
        Configuration config = new Configuration().configure();
        //创建服务注册对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
        .applySettings(config.getProperties()).buildServiceRegistry();
        //创建SessionFactory
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        //创建session对象
        Session session = sessionFactory.getCurrentSession();
        //创建SchemaExport对象
        SchemaExport schemaExport = new SchemaExport(config);
        schemaExport.create(true, true);
    }

}

运行junit即可生成user表

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44745次
    • 积分:1185
    • 等级:
    • 排名:千里之外
    • 原创:69篇
    • 转载:4篇
    • 译文:2篇
    • 评论:12条
    我的微博
    博客专栏
    最新评论