注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

中华神鸟

QQ:935658701

 
 
 

日志

 
 

【转载】Maven管理spring3.2+hibernate3.6配置文件(搞了很久才搞定)  

2014-04-28 22:41:13|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1.maven配置文件pom.xml

<?xml version="1.0"?>
<project
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.ibc</groupId>
  <artifactId>internet-baccaret</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>
 <artifactId>web</artifactId>
 <packaging>war</packaging>
 <name>web Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <properties>
  <!-- jetty 的版本 -->
  <jetty.version>9.0.4.v20130625</jetty.version>
  <javax.servlet.version>2.5</javax.servlet.version>
  <junit.version>4.10</junit.version>
  <log4j.version>1.2.16</log4j.version>
  <mysql.jdbc.version>5.1.21</mysql.jdbc.version>
  <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
  <javassist.version>3.12.1.GA</javassist.version>
  <hibernate.version>3.6.9.Final</hibernate.version>
 </properties>
 <dependencies>
  <!-- junit的依赖 -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>${junit.version}</version>
   <scope>test</scope>
  </dependency>
  <!-- jetty的依赖 -->
  <dependency>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-maven-plugin</artifactId>
   <version>${jetty.version}</version>
  </dependency>
  <!-- javax.servlet的依赖 -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>${javax.servlet.version}</version>
  </dependency>
  <!-- log4j的依赖 -->
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>${log4j.version}</version>
  </dependency>
  <!-- mysql的依赖 -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>${mysql.jdbc.version}</version>
  </dependency>
  <dependency>
   <groupId>javassist</groupId>
   <artifactId>javassist</artifactId>
   <version>${javassist.version}</version>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>0.2.9</version>
  </dependency>
  <!-- hibernate部分 -->
   <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate.version}</version>
   </dependency>

   <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate.version}</version>
    <classifier>sources</classifier>
   </dependency>

   <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>${hibernate.version}</version>
   </dependency>
   
 </dependencies>
 <build>
  <finalName>web</finalName>
  <plugins>
   <!-- 配置jetty插件 -->
   <plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
   </plugin>
   <!-- 配置源文件目录 注意:若这里不配置的话,我们新建的项目只会认默认的src/main/java目录,在配置这里的时候,我还配置了一个修复性的东西,详细情况ctrl
    + 1 提示查看 -->
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.1</version>
    <!-- 配置执行的命令集 -->
    <executions>
     <!-- 配置单独的一个命令 -->
     <execution>
      <id>add-source</id>
      <phase>generate-sources</phase>
      <goals>
       <goal>add-source</goal>
      </goals>
      <configuration>
       <!-- 源文件配置目录 -->
       <sources>
        <source>src/main/resources</source>
        <source>src/main/java</source>
       </sources>
      </configuration>
     </execution>
    </executions>
   </plugin>
  </plugins>
  <pluginManagement>
   <plugins>
    <!--This plugin's configuration is used to store Eclipse m2e settings
     only. It has no influence on the Maven build itself. -->
    <plugin>
     <groupId>org.eclipse.m2e</groupId>
     <artifactId>lifecycle-mapping</artifactId>
     <version>1.0.0</version>
     <configuration>
      <lifecycleMappingMetadata>
       <pluginExecutions>
        <pluginExecution>
         <pluginExecutionFilter>
          <groupId>
           org.apache.maven.plugins
          </groupId>
          <artifactId>
           maven-resources-plugin
          </artifactId>
          <versionRange>[2.3,)</versionRange>
          <goals>
           <goal>testResources</goal>
          </goals>
         </pluginExecutionFilter>
         <action>
          <ignore></ignore>
         </action>
        </pluginExecution>
       </pluginExecutions>
      </lifecycleMappingMetadata>
     </configuration>
    </plugin>
   </plugins>
  </pluginManagement>
 </build>
</project>

2.applicationcontext.xml配置文件

<?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:aop="http://www.springframework.org/schema/aop"    
    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-3.0.xsd     
          http://www.springframework.org/schema/tx     
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
          http://www.springframework.org/schema/context     
          http://www.springframework.org/schema/context/spring-context-3.0.xsd     
          http://www.springframework.org/schema/aop     
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
   
<!--  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> -->
<!--      <property name="location"> -->
<!--       <value>/WEB-INF/config.properties</value> -->
<!--    </property> -->
<!--  </bean>  -->
 <bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:config.properties" />
 </bean>

 <!-- 配置数据源 -->
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  init-method="init" destroy-method="close">
  <!-- 配置数据库连接基本信息 -->
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />

  <!-- 配置初始化大小、最小、最大 -->
  <property name="maxActive" value="20" />
  <property name="initialSize" value="1" />
  <property name="minIdle" value="1" />
  <!-- 配置获取连接等待超时的时间 -->
  <property name="maxWait" value="60000" />
  <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  <property name="timeBetweenEvictionRunsMillis" value="3000" />
  <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  <property name="minEvictableIdleTimeMillis" value="300000" />
  <property name="validationQuery" value="${validationQuery}" />
  <property name="testWhileIdle" value="true" />
  <property name="testOnBorrow" value="false" />
  <property name="testOnReturn" value="false" />
  <!-- 打开PSCache,并且指定每个连接上PSCache的大小,mysql 不使用 -->
  <property name="poolPreparedStatements" value="true" />
  <property name="maxPoolPreparedStatementPerConnectionSize"
   value="20" />
  <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat,log4j" />
  <!-- 慢查询sql打印 -->
  <property name="connectionProperties" value="druid.stat.slowSqlMillis=100" />
 </bean>
<!-- 组件自动扫描&&注入 -->
<!-- 使用 annotation --> 
    <context:annotation-config />
   
  <!-- 使用 annotation 自动注册bean -->
 <context:component-scan base-package="src.main.java.com.star.*" />
 <!-- 配置session工厂 -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
  <property name="packagesToScan">
   <list>
    <value>main.java.com.star.manager.entity</value>
   </list>
  </property>
 </bean>
<!--  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> -->
<!--   <property name="dataSource" ref="dataSource"/> -->
<!--   <property name="hibernateProperties"> -->
<!--    <value> -->
<!--    hibernate.dialect=org.hibernate.dialect.MySQLDialect -->
<!--    hibernate.show_sql=true -->
<!--    hibernate.format_sql=false -->
<!--    hibernate.query.substitutions=true 1, false 0 -->
<!--    hibernate.jdbc.batch_size=20 -->
<!--    hibernate.query.factory_class=org.hibernate.hql.classic.ClassicQueryTranslatorFactory -->
<!--    </value> -->
<!--   </property> -->
<!--  </bean> -->

 <!-- 数据源事务管理 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean> 

<!-- 注入sessionFactory -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
</beans>

3.在UserDaoImpl里面用Main方法测试(获取已经注入的bean)

@Repository("userDao")
public class UserDaoImpl implements UserDao {
 @Autowired
 private HibernateTemplate hibernateTemplate;
 
 public HibernateTemplate getHibernateTemplate() {
  return hibernateTemplate;
 }

 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  this.hibernateTemplate = hibernateTemplate;
 }
 public void addUser(User user) {
  System.out.print("锟斤拷锟斤拷没锟斤拷锟较?");
  this.getHibernateTemplate().save(user);
 }
 public static void main(String[] args) {
  User user=new User();
  user.setId(3);
  user.setUsername("star");
  user.setPassword("password3");
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  HibernateTemplate hib = (HibernateTemplate)context.getBean("hibernateTemplate");
  hib.save(user);
 }
 
}


原文地址:http://guofei-715.blog.163.com/blog/static/441694402013854297979/

  评论这张
 
阅读(52)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017