程序猿全敏的gravatar头像
程序猿全敏 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。

spring MVC+spring+hibernate项目整合入门实例

此时页面乱码,这不是传到数据库时的乱码,是从页面传到控制层的乱码,此时,在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>

spring MVC+spring+hibernate项目整合入门实例


打赏

文件名:springmvc01.rar,文件大小:9629.644K 下载
最代码最近下载分享源代码列表最近下载
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日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友