`
aijuans
  • 浏览: 1548800 次
社区版块
存档分类
最新评论

随笔-Spirngmvc的总结

 
阅读更多

搭建springmvc+hibernate+sping 框架

  1. 首先需要的jar报导入到项目中;

  2. 配置文件;spring配置文件在web.xml里面要引入,项目启动初始化spring的控制器,spingbean的注入, <!-- Controller方法调用规则定义 --><!-- 页面View层基本信息设定 --><!-- servlet映射列表,所有控制层Controller的servlet在这里定义 -->

    web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>

            <!--引用一些配置文件,便于管理把dao,service,hibernate,web单独一个配置文件,另一种方式参考注1-->
            <param-value>/WEB-INF/hib-config.xml,/WEB-INF/web-config.xml,/WEB-INF/service-config.xml,/WEB-INF/dao-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

 

注1:利用context-param 的servlet上下文引入配置文件

<context-param>
   <param-name>contextConfigLocation</param-name>

    <!--spring开头的配置文件-->
   <param-value>classpath:spring-*.xml</param-value>

</context-param>

 

<servlet>
        <description>spring mvc servlet</description>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <description>spring mvc 配置文件</description>
            <param-name>contextConfigLocation</param-name>

            <!--加载扫描spring的控制器-->
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>

        <!--项目启动实例化次servlet,就一定要有<init-param>指定一个sping的配置文件-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

 

web-config.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" 
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <!-- Controller方法调用规则定义 -->
    <bean id="paraMethodResolver"
      class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="action"/>
        <property name="defaultMethodName" value="list"/>
    </bean>
  
   <!-- 页面View层基本信息设定 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView"/>
        <!--<property name="prefix" value="/myjsp/"/>-->
        <property name="suffix" value=".jsp"/>
    </bean>

<!-- servlet映射列表,所有控制层Controller的servlet在这里定义 -->
    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="user.do">userController</prop>
            </props>
        </property>
    </bean>

<bean id="userController" class="com.sxt.action.UserController">
    <property name="userService" ref="userService"></property>
</bean>
</beans>


hib-config.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-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  http://www.springframework.org/schema/context   
   http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
    <context:component-scan  base-package="com.sxt"/>   
    <!-- 支持aop注解 -->
    <aop:aspectj-autoproxy />
    
        <!--数据源的信息可以放到properties文件夹里面,注2-->
    <bean id="dataSource"  
            class="org.apache.commons.dbcp.BasicDataSource">  
            <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property>  
            <property name="url" value="jdbc:mysql://localhost:3306/myhib"></property>  
            <property name="username" value="root"></property>  
            <property name="password" value="123456"></property>
    </bean>  

   <bean id="sessionFactory"  
       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
           <property name="dataSource"> <ref bean="dataSource" /></property>
           <property name="hibernateProperties">  
               <props>  
                   <!-- key的名字前面都要加hibernate. -->
                   <prop key="hibernate.dialect">  
                       org.hibernate.dialect.MySQLDialect  
                   </prop>  
                   <prop key="hibernate.show_sql">true</prop>
                   <prop key="hibernate.hbm2ddl.auto">update</prop>
               </props>
           </property>
        <property name="packagesToScan">
            <value>com.sxt.po</value>
        </property>
   </bean>  

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!--配置一个JdbcTemplate实例-->  
<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">   
     <property name="dataSource" ref="dataSource"/>   
</bean>  

<!-- 配置事务管理 注3 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<aop:config> 
    <aop:pointcut expression="execution(public * com.sxt.service.impl.*.*(..))" id="businessService"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 
<tx:advice id="txAdvice" transaction-manager="txManager" > 
    <tx:attributes> 
        <tx:method name="find*"  read-only="true" propagation="NOT_SUPPORTED"  /> 
        <!-- get开头的方法不需要在事务中运行 。 
        有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的--> 
        <tx:method name="*"/>    <!-- 其他方法在实务中运行 --> 
    </tx:attributes> 
</tx:advice> 

</beans>

 

Hibernate的配置主要有!!!!:

要扫描的包(hibernate注解);

数据源;sessionFactory;

hibernateTemplate;

配置事物(获取数据不启动事务)

 

注2:

<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:dbconfig.properties" />

这样获取值${jdbc.url.jeecg}

db.properties内容
hibernate.dialect=org.hibernate.dialect.MySQLDialect
validationQuery.sqlserver=SELECT 1
jdbc.url.jeecg=jdbc:mysql://localhost:3306/projectdb?useUnicode=true&characterEncoding=UTF-8
jdbc.username.jeecg=root
jdbc.password.jeecg=xlj123
jdbc.dbType=mysql

 

注3:

<!-- 配置事物管理器,在*ServiceImpl里写@Transactional就可以启用事物管理,在查找的方法加上 read-only只读,@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)//查询不需要开事务-->
    <bean name="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />

 

 

service-config.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" 
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="userService" class="com.sxt.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    
</beans>

dao-config.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" 
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <bean id="userDao" class="com.sxt.dao.UserDao">
      <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
</beans>

1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics