程序猿全敏
2016-07-18 15:51:33
原
spring MVC+spring+hibernate项目整合入门实例
1.新建一个web项目,添加所需要的jar包,修改web.xml配置文件
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <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>
2.新建实体User
package com.qm.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; private String pwd; public User() { } public User(String name, String pwd) { this.name = name; this.pwd = pwd; } public User(int id, String name, String pwd) { super(); this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
3.新建hib-config.xml配置文件,关于数据库的配置
<context:component-scan base-package="com.qm"/> <aop:aspectj-autoproxy /> <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/ssh?characterEncoding=UTF-8"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <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.qm.entity</value> </property> </bean>
4.新建UserDao UserService UserContorll
public void add(User u){ this.getHibernateTemplate().save(u); }
private UserDao userDao; public void add(String name,String pwd){ User u=new User(); u.setName(name); u.setPwd(pwd); userDao.add(u); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; }
private UserService userService; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { userService.add(request.getParameter("name"), request.getParameter("pwd")); User u=new User(); request.setAttribute("a", "welcome "+request.getParameter("name")); return new ModelAndView("index"); } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; }
5.新建dao-config.xml service-config.xml web-config.xml 注入
<bean id="userDao" class="com.qm.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
<bean id="userService" class="com.qm.service.UserService"> <property name="userDao" ref="userDao"></property> </bean>
<bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="action"/> <property name="defaultMethodName" value="list"/> </bean> <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> <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.qm.action.UserController"> <property name="userService" ref="userService"></property> </bean>
现在打开浏览器输入http://localhost:8080/springmvc01/reg.jsp
输入用户名小全,跳转到http://localhost/springmvc01/user.do。
此时页面乱码,这不是传到数据库时的乱码,是从页面传到控制层的乱码,此时,在web.xml加入编码过滤器
<filter> <filter-name>characterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
猜你喜欢
- SSH整合开发入门小例子,适合java web开发初学者
- Spring4+Hibernate4+MySQL+Maven项目整合Java Web实例(Annotations+XML)
- SSH整合项目完成老师分配学生,分页,模糊查询等功能,适合新手练习
- Spring+Spring Mvc+Hibernate+Bootstrap整合实现企业级的员工信息管理系统,带数据库
- ssh整合extjs的项目实例
- spring4.0.3+hibernate 4.3.7+bootstrap3.3+表单验证+kindeditor实现的实例
- SSH框架开发购物车
- springmvc+spring3.x+hibernate3.x的比较清晰一点的能运行的例子,谢谢
- ssh+bootstrap+oracle集成开发java项目日志管理系统
- ssh和bootstrap开发无线点餐系统,实现简单的增删改查操作
- Easyui SSH MySQL后台权限管理系统,很适合新生学习
- ssh简单的整合实例
请下载代码后再发表评论
文件名:springmvc01.rar,文件大小:9629.644K
下载
- /
- /springmvc01
- /springmvc01/.classpath
- /springmvc01/.mymetadata
- /springmvc01/.project
- /springmvc01/.settings
- /springmvc01/.settings/.jsdtscope
- /springmvc01/.settings/org.eclipse.jdt.core.prefs
- /springmvc01/.settings/org.eclipse.wst.jsdt.ui.superType.container
- /springmvc01/.settings/org.eclipse.wst.jsdt.ui.superType.name
- /springmvc01/src
- /springmvc01/src/com
- /springmvc01/src/com/qm
- /springmvc01/src/com/qm/action
- /springmvc01/src/com/qm/dao
- /springmvc01/src/com/qm/entity
- /springmvc01/src/com/qm
- /springmvc01/src/com
- /springmvc01

- 证精 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- 原 基于maven与springMVC的拦截器Interceptor,控制器Controller的使用
- spring mvc 学习使用模板 基础教程
- spring mvc实现文件上传功能
- 原证 Spring Mvc初学者专用,里面有4套案例!
- 原证 Spring MVC多视图配置简单demo实例,支持freemarker、velocity、jsp视图
- spring mvc注解代码实例教程
- 原证精 spring mvc如何将form表单中的对象类型绑定
- 精 SpringMVC的三种统一异常处理实例代码分享
- spring3.0 mvc和rest风格的小例子配置demo代码教程
- 原 Spring MVC+apache Shiro框架搭建,基于maven构建
- 待 Spring学习笔记之Spring MVC 入门教程

1973356987 LV13
2021年6月16日
fuchongbin LV1
2018年12月20日
zhaowendong LV3
2018年10月2日
seagoat LV1
2018年7月8日
你将相思赋予谁 LV1
2018年7月6日
18700882235 LV1
2018年2月24日
981090208 LV1
2017年12月12日
无忧123456 LV6
2017年11月22日
lightingstream LV1
2017年9月19日
chinafjfzlj LV31
2017年9月8日

爱吃鱼的水獭
2023年12月21日
暂无贡献等级
aaa最代码 LV14
2022年12月9日
zhangymo01
2022年12月1日
暂无贡献等级
dafqrf LV1
2021年12月8日
MyPary LV6
2021年11月29日
yema2986 LV2
2021年11月18日
linom199144 LV2
2021年7月5日
1973356987 LV13
2021年6月16日
秦sir3067683450 LV10
2021年6月2日
18770976402
2021年4月29日
暂无贡献等级