问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

如何配置springmvc+hibernate

发布网友 发布时间:2022-04-07 22:59

我来回答

1个回答

热心网友 时间:2022-04-08 00:28

首先配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 上下文配置文件 -->
<context-param>
<description>spring config</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<description>spring listerner</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name> flushMode </param-name>
<param-value>AUTO </param-value>
</init-param>
</filter>

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- 配置过滤器,同时把所有的请求都转为utf-8编码 -->
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置找不到页面时返回的页面 -->
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>

<!-- 配置项目主页 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

接下来配置
springmvc-servlet.xml和applicationContext.xml
这两个文件都放在web-inf下面
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 连接数据库 -->
<bean id="mydataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ucs_tdc"></property>
<property name="username" value="root"></property>
<property name="password" value="516725"></property>
<property name="initialSize" value="2"></property>
<property name="maxActive" value="15"></property>

</bean>
<!-- Hibernate 设置-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="mydataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ucs/tdc/pojo/AdminInfo.hbm.xml</value>

</list>
</property>

</bean>
<!-- 开启组件自动扫描 -->
<context:component-scan base-package="com.*"></context:component-scan>
<!-- 开启AOP注解 -->
<aop:aspectj-autoproxy/>
<!-- 申明事务管理,采用AOP形式切入 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 属性配置 -->
<!-- 对一下操作进行事务管理 -->
<tx:attributes>
<tx:method name="*" read-only="true"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="change*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="account*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED"/>

</tx:attributes>
</tx:advice>
<!-- AOP切入点设置 -->
<aop:config proxy-target-class="true">
<aop:pointcut expression="within(com.ucs.tdc.*)" id="serviceOperation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>
</beans>
springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启组件自动扫描 -->
<context:component-scan base-package="com.ucs.tdc.*"></context:component-scan>
<!-- 开启AOP注解 -->
<aop:aspectj-autoproxy/>
<!--Spring mvc -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/jsp/"
p:suffix=".jsp" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list >
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- * -->
<!--
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/jsp/getinfoList.do"/>
<mvc:mapping path="/jsp/getinfoList.do"/>
<mvc:mapping path="/jsp/getserverinfo.do"/>
<mvc:mapping path="/jsp/setServerInfo.do"/>
<mvc:mapping path="/jsp/modification.do"/>
<mvc:mapping path="/jsp/toAdd.do"/>
<mvc:mapping path="/jsp/AddorEditOneInfo.do"/>
<mvc:mapping path="/jsp/delete.do"/>
<bean class="com.ucs.tdc.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
-->
</beans>
contoller
@Controller
public class AdminController {
static Logger log = Logger.getLogger(AdminController.class);
@Resource
private AdminInterFace interFace;
public void setInterFace(AdminInterFace interFace) {
this.interFace = interFace;
}
static Gson gson;

/***普通用户登录**/
@RequestMapping(value = "jsp/user_Log/login")
public boolean user_login(@RequestParam String user_info) {
Boolean flag=false;
UserInfo info=gson.fromJson(user_info, UserInfo.class);
flag =interFace.verifyuser(info);
return flag;

}

/**管理员登录***/
@RequestMapping(value = "jsp/admin_Log/login")
public boolean admin_login(@RequestParam String admininfo) {
Boolean flag=false;
AdminInfo info=gson.fromJson(admininfo, AdminInfo.class);
interFace.verifyAdmin(info);
return flag;

}

}
接口:
public interface AdminInterFace {

public Boolean verifyuser(UserInfo info);
public void verifyAdmin(AdminInfo info);
}
实现类: session自动管理,采用事务处理
@Repository("AdminImpl")
public class AdminImpl extends HibernateDaoSupport implements AdminInterFace{
@Resource
public void setMySessionFactory(SessionFactory sf){
super.setSessionFactory(sf);
}

public boolean verifyAdmin(String adminName,String pw){
String hql="from AdminInfo ";
Object[] params={adminName};
List<AdminInfo> list =this.getHibernateTemplate().find(hql);
System.out.println(list.size());
AdminInfo info=list.get(0);
System.out.println(info.getUserName()+" "+info.getUserPw());
return false;
}
public Boolean verifyuser(UserInfo info) {
// TODO Auto-generated method stub
return null;
}
public void verifyAdmin(AdminInfo info) {
// TODO Auto-generated method stub

}
}
项目业务处理和数据处理放置在同一个台服务器上,前
配置Spring+Spring MVC+Hibernate需要哪些jar包

2.下载SpringMvc的依赖包,jstl.jar,standard.jar,commons-logging.jar。前面两个是JSTL表达式需要的(如果不使用JSTL,可以不下载),后面一个是日志需要的。也就是说,实际上SpringMVC只依赖一个包。都可以在MVN的仓库里找到。3.把第一步下载的dist包里lib下的jar和第二步下载来的三个依赖jar包都...

java用spring mvc + hibernate,实现数据库的增删改查

1)在applicationContext里面配置Hibernate的dataSource和SessionFactory 2)编写Dao的接口是实现类。如果用到Spring的Hibernate的操作模板(HibernateTemplate)可以继承HibernateDaoSupport,实现类注解成@Repository 3)编写Action,然后注解成@Controller,在Action里面需要用@Autowired注入Dao的实例 4)配置applicationC...

springmvc验证的hibernate validator 框架 的groups怎么用

1.安装好之后在开始菜单里面找.找到之后可以创建一个快捷方式到桌面.截图把位置.2.如果没什么问题的话,你点击连接就进去到数据库里面了.右键新建.3.弹出这个对话框,按照图片中的设置.记得路径保存到其他盘,自己新建一个文件夹.放到C盘,如果数据库文件大了,会拖慢整个电脑的运行.4.大保存到这个文件夹...

如何在Maven中配置Spring依赖

从Spring 3.2开始,Spring MVC Test项目已经被包含到核心的Spring Test框架中(原来是一个独立项目,项目托管在GitHub)。所以,从Spring 3.2开始,仅需要在依赖配置中配置spring-test依赖即可。注意:对于使用Spring 3.1及以下版本的应用来说,独立的spring-mvc-test依赖还是可以使用的,可以参考这里进行...

java 国际化时间如何处理? spring mvc hibernate

&lt;!-- 国际化消息处理 --&gt; &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver"&gt; &lt;property name="defaultLocale" value="zh_CN" /&gt; &lt;/bean&gt;

简单叙述struts spring和hibernate开源框架功能和结合方式

比如一个User实例)注入,后者即声明式事务处理。其实,spring也可以取代struts接受展现层的请求接收和转发,他也有自己的mvc框架。最后,问一下。你是新学这几个框架吗?如果新学,先不要考虑集成的问题。按照struts hibernate spring顺序来学,hibernate较简单,spring稍有难度。有问题,可以追问。

...spring,hibernate 都各自起着什么作用,还有MVC的作用是什么?_百 ...

MVC模式就是model、view、Controller的简称,解析的自己搜百度的百科。分层一般分为DAO层、Service层、Action层(当然细分的还有他们的*Impl实现),但不是将struts2 , spring,hibernate 硬塞到一个固定层里去。在DAO层,一般会继承HibernateDAOSupport这个类,如果查看这个类的源代码,就知道其实际就是对...

...和 ssh作用(详细说说status、spring、hibernate的作用)

一、纠正下ssh的第一个s是struts,ssh是这三个框架的组合 二、SSH是典型的JAVAEE三层框架:表现层(Stuts)、业务逻辑层(Spring)、持久层(Hibernate)。软件分层是为了实现"高内聚、低耦合"。把问题划分开来各个解决,易于控制,易于延展,易于分配资源等等 三、SSH中充当表现层的是Struts,它是一个MVC...

springmvc中 sessionfactory到底是什么 知乎

1,Spring中设置的SessionFactory的bean如何产生hibernate需要的SessionFactory呢,在配置文件中,SessionFactory的class属性为org.springframework.orm.hibernate3.LocalSessionFactoryBean 如果用到标注产生model的情况,则SessionFactory的class属性为:org.springframework.orm.hibernate3.annotation.AnnotationSession...

有什么好书讲解spring框架的原理和用法的麽

Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中。Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity...

spring配置mybatis springwebmvc配置文件 spring和hibernate spring mvc配置 springmvcxml配置 java spring mvc springmvc配置详解 springmvc配置文件 springmvc配置文件详解
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
当前我国经济工作的主线是什么 空调几匹最合适房间 高考时带手表进考场会被监考老师没收吗? 大方县办营业执照在哪里呀 石林县城办营业执照在哪里 家里潮湿有味道怎么办 家里潮湿有味道怎么改善 蛋糕的做法家庭做法蒸蛋糕 黄煌经方沙龙(第一期)        目录       _百... 如何为朋友妈妈庆祝生日呢? 大学班长生日祝福语 给小阿姨的生日祝福语 那个女孩的脚that girl’s foot为什么不是复数feet? 相关系数计算公式是什么? oppo自带的音乐软件是虾米音乐,能不能和淘宝账号绑定的?我没找到?是只能虾米app才能绑吗? oppo 自带音乐播放器怎么都是虾米的歌 怎样去掉oppo上的虾米音乐 为什Oppo A57自带的音乐听不了音乐?而且下面还写着应版权方要求,该歌曲暂时无法播放。 OPPOR9自带虾米音乐怎么使用兑换码? 积分商城兑换的oppo手机虾米svip怎样登录 为什么oppo自带音乐登陆账户会是虾米账户呢?之前都是oppo账户的 OPPO a59s 手机自带音乐出现 ,仅限虾米试听是怎么回事? 这个是oppo自带的音乐播放器,那个虾米账号怎么在虾米APP上登陆 OPPO手机自带音乐盒虾米出现“仅限虾米试听”听不了歌曲,就算充了虾米会员也不行? 开通oppo账号每月收费吗? 注册oppo会员要钱吗 注册OPPO手机VIP会员要收费吗? 一个捂脸表情包加一个炸弹是什么意思? 求一张怂炸表情图 跪求分娩视频电视剧,【免费高清】在线观看百度网盘资源 跪求电视剧里的分娩视频,【在线观看】免费百度云资源 跪求影视分娩视频,【免费高清】在线观看百度网盘资源 这个男孩和这个女孩步行去学校用英语怎么写? 请教《千足女郎》怎么翻译好!千足两字是用拼音还是用英语? eight-foot-deep和eight feet deep 相关系数怎么算 The girl fell off the bus and hurt her foot这样写对吗? 统计学的! 计算相关系数的简要公式,并说明相关系数的取值范围及其判断标准? bboy、bgirl什么意思? 相关系数公式—求解 英汉互译:1.a tall girl( ) 2.big feet( ) the girl是什么意思 怎样求相关系数R值,公式是什么,用excel表怎样算 fightinggirl是什么意思? thegirlhadnofeetatall的意思 86针式打印机打印字符时出现一条白线是怎么回事?应如何处理 请问大家针式打印机打印表格时竖线断线怎么办 针式打印机打印表格出现波浪怎么解决? 为什么爱普生的针式打印机最近打印文件会断开就是打印几行字会放几行了接着打印下去 为什么LQ-730K针式打印机打印单据时部分字会断开成两行,但有的又不会。中间断开的部分还挺宽的。 中式烹饪炒菜? 奔腾b70机油尺怎么看