搭建springmvc+hibernate+sping 框架
-
首先需要的jar报导入到项目中;
-
配置文件;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>
相关推荐
随笔--学习系列--什么是电流角、控制角、相位超前角、内功率因数角、功率角、转矩角、功率因数角、转子初始角-CSDN博客 (2024_3_6 19_42_06).html
教师随笔-教师政治学习笔记随笔5篇.docx
教师随笔-教师政治教学反思随笔5篇.docx
教师随笔-初中道德与法治教师随笔5篇.docx
教师随笔-高中政治课教师教学随笔5篇.docx
教师随笔-初中政治教师教学西方教育随笔5篇.docx
由于提供的文件信息【标题】、【描述】、【标签】和【部分内容】内容都是一些特殊符号和乱码,而非实际有效文字信息。这样的信息无法用来生成具体的IT知识点。IT知识需要有明确的、有意义的上下文内容作为基础,例如...
教师寒假生活随笔-教师生活.doc
想给生疏人随笔写信_有关写网络好友的随笔-生疏好友.docx
有关写乡村记忆的随笔-小水凼-小孤山村乡村记忆馆.docx
幼儿教育随笔-让友爱之花在游戏中绽放.pdf
幼儿教育随笔-利用游戏规则巧对幼儿任性行为.pdf
如何引导幼儿进行同伴评价幼儿教育随笔-引导孩子自己处理同伴间冲突.rar
在一篇名为《【学校老师训练感悟随笔-持一颗感恩之心】 心怀一颗感恩之心》的随笔中,我们可以窥见教师在教育实践中关于感恩教育、教育反思、家校合作与教师角色定位等方面的深刻感悟。 首先,感恩教育的价值在随笔...
根据给定文件的信息,我们可以提炼出以下几个主要的知识点: ### 1. 幼儿教育的重要性 - **基础知识**:从婴幼儿出生到成长的过程是不断进步发展的。幼儿园阶段是这一过程中的重要环节,它不仅关系到孩子们的身体...
作为一名幼儿教师,我在与孩子们朝夕相处的过程中,深刻体验到了何谓“用心的感动”。这不仅仅是一份工作,更是情感的交流与智慧的传递。在孩子们的眼中,我看到了纯真与好奇,他们的每一个问题、每一次探索都让我为...
教育随笔《创新思维在画图归纳》以四年级信息技术课程为例,深入探讨了如何在教学实践中激发学生的创造力。通过具体的案例分析,本文旨在探讨如何使学生在使用Windows“画图”软件进行图像创作的过程中,突破传统...
### 总结 通过对以上知识点的梳理,可以看出郑杰与钱穆两位学者在教育和历史领域都有着独特的见解。郑杰提倡一种积极面对生活挑战的态度,并强调个人发展与自由的重要性;而钱穆则通过《中国历代政治得失》展现了对...