首页 > 编程语言 > 详细

Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)

时间:2015-11-30 17:36:19      阅读:367      评论:0      收藏:0      [点我收藏+]

1. 在IntelliJ中新建maven项目

给出一个建好的示例

技术分享

2. 在pom.xml中配置依赖

包括:
spring-context
spring-orm
hibernate-core
mysql
commons-dbcp
aspectjweaver

3. resources右键new一个Xml Configuration File--Spring Config配置文件:spring-config.xml(或者applicationContext.xml)

    配置dataSource、sessionFactory及transactionManager。

    可能会提示一个Application Context的什么配置,按提示操作即可;或者在IntelliJ工具的Project Structure下的Facets中进行spring的配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

       <!--自动扫描指定包及其子包下的所有Bean类-->
       <context:component-scan base-package="com.test.app.dao.impl"/>

       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
              <property name="url" value="jdbc:mysql://localhost:3306/javaee"/>
              <property name="username" value="root"/>
              <property name="password" value="root"/>
       </bean>
       
       <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <property name="annotatedClasses">
                     <list>
                            <value>com.test.app.domain.User</value>
                     </list>
              </property>
              <property name="hibernateProperties">
                     <value>
                            hibernate.dialect=org.hibernate.dialect.MySQLDialect
                            hibernate.hbm2ddl.auto=update
                            hibernate.show_sql=true
                            <!--hibernate.format_sql=true-->
                     </value>
              </property>
       </bean>

       <!--配置hibernate事务管理器-->
       <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"/>
       </bean>
       <!--根据Annotation来生成事务代理-->
       <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

4. 新建User实体类,并进行Annotation注解

 

5. 新建UserDao接口及其实现类UserDaoImpl,并进行Annotation注解

 

6. 新建测试类MainTest

Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)

原文:http://www.cnblogs.com/weilunhui/p/5007679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!