您的位置:首页 > 编程语言 > Java开发

Myeclipse中基于springMVC+spring+hibernate的非注解入门实例

2014-12-29 15:09 453 查看
一直做Android前端开发,想学学后台J2EE服务器开发 的知识,零基础第一步学习一个简单例子:

一, demo结构:







数据库:



二, SpingMVC框架:

拷贝相应的jar到lib纹路下:



三, 在myeclipse中添加Spring支持:

右键点击该工程,在对话框中选择“MyEclipse->Add Spring Capabilities...”,添加Spring,并进行相关配置,如图4所示,采用默认配置即可。本例选用的spring3.1。



配置Hibernate

1)右键点击该工程,在对话框中选择“MyEclipse->Add Hibernate Capabilities...”,添加Hibernate,并进行相关配置,如图5、图6、图7、图8、图9所示。

由于需要使用Spring配置Hibernate,因此选中所有的Library。



图 5

选择Spring的配置文件applicationContext.xml进行配置。



图 6

选择已有的Spring配置文件,并使用Spring配置Hibernate中的SessionFactory,SessionFactory的bean id为sessionFactory。



图 7

配置数据源DataSource的bean id为dataSource,且其配置信息采用数据库连接MySQL。



图 8

不另写SessionFactory类,而采用Spring为Hibernate已设计的SessionFactory类。



图 9

4.数据库逆向工程

1)切换Perspective至MyEclipse Hibernate,右键点击数据表myaccount,在对话框中选择“Hibernate Reverse Engineering...”,对user表的关系对象映射进行配置,如图10所示,其中第一个红框用于选择Java源文件目录,第二个红框用于选择是否创建关系对象映射文件,以hbm.xml结尾,第三个红框用于选择是否创建数据对象类,第四个红框用于选择是否创建数据访问对象类,均选择是,其他采用默认配置即可。



图 10

Spring以及Hibernate配置中图片参考别人demo的配置,跟本例在女文件目录,文件名等方面不一致(仅作参考),具体数据库反向等操作根据自己建立的数据库连接文件来。

四, 配置文件

配置文件:web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

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_3_0.xsd">
<display-name>SpringMVC Learning</display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- 加载所有的配置文件 -->

<!-- <context-param> -->

<!-- <param-name>contextConfigLocation</param-name> -->

<!-- <param-value>classpath:config/applicationContext.xml</param-value> -->

<!-- </context-param> -->

<servlet>

<servlet-name>Dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

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

<param-value>classpath:config/spring-mvc.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>Dispatcher</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<!-- 字符集过滤器 -->

<filter>

<description>字符集过滤器</description>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<description>字符集编码</description>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

主要指定相应的servlet。

配置文件:spring-mvc :

<?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"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/context/spring-mvc-3.2.xsd">
<!-- Action definition -->

<bean id="loginController" class="com.oliver.web.controller.LoginController">

<property name="successView" value="showAccount"></property>

<property name="failView" value="login"></property>

</bean>

<!-- 开启注解 -->

<!-- <mvc:annotation-driven /> -->

<!-- 静态资源(js/image)的访问 -->

<!-- <mvc:resources location="/js/" mapping="/js/**"/> -->

<!-- 上传文件时需要用到的分解器,默认将编码转为utf-8 -->

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="defaultEncoding">

<value>UTF-8</value>

</property>

</bean>

<!-- urlMapping -->

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<props>

<prop key="/pages/login.do">loginController</prop>

</props>

</property>

</bean>

<!--definition of ViewResolver -->

<!-- 配置SpringMVC的视图渲染器, 让其前缀为:/ 后缀为.jsp 将视图渲染到/pages/<method返回值>.jsp中 -->

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/pages/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

没有用注解,主要指定控制模块,urlmapping以及识图渲染。

配置文件: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:p="http://www.springframework.org/schema/p"

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

</property>

<property name="username" value="root"></property>

<property name="password" value="123456"></property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>com/oliver/dao/Account.hbm.xml</value></list>

</property></bean>

<bean id="AccountDAO" class="com.oliver.dao.AccountDAO">

<property name="sessionFactory">

<ref bean="sessionFactory"/>

</property>

</bean>

</beans>

主要配置数据源,sessionfactory等以及具体操作的bean。

五,相应的逻辑实现:

Account.java ,AccountDAO以及Account.hbm.xml为数据表反向自动生成;

控制模块实现简单逻辑:

LoginController.java:

package com.oliver.web.controller;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.FileSystemResource;

import org.springframework.core.io.Resource;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.AbstractController;

import com.oliver.dao.Account;

import com.oliver.dao.AccountDAO;

public class LoginController extends AbstractController {

private String successView;

private String failView;

public String getSuccessView() {

return successView;

}

public void setSuccessView(String successView) {

this.successView = successView;

}

public String getFailView() {

return failView;

}

public void setFailView(String failView) {

this.failView = failView;

}

@Override

protected ModelAndView handleRequestInternal(HttpServletRequest request,

HttpServletResponse response) throws Exception {

// TODO Auto-generated method stub

String username=request.getParameter("username");

String password=request.getParameter("password");

Account account =getListAccount(username);

Map<String ,Object> model=new HashMap<String,Object>();

if(account !=null){

model.put("account", account);

return new ModelAndView(getSuccessView(),model);

}else{

model.put("error", "卡号和密码不正确");

return new ModelAndView(getFailView(),model);

}

}

@SuppressWarnings("unchecked")

public Account getListAccount(String username){

Account thisAccount=null;

//从src/applicationContext.xml装载BeanFactory

Resource resource=new ClassPathResource("config/applicationContext.xml");

BeanFactory factory = new XmlBeanFactory(resource);

//从BeanFactory获取UserDAO

AccountDAO accountDAO = (AccountDAO) factory.getBean("AccountDAO");

//添加新User

List accountlist=accountDAO.findByUsername(username);

Iterator<Account> itaccount=accountlist.iterator();

while(itaccount.hasNext()){

Account myaccount=itaccount.next();

thisAccount=myaccount;

}

return thisAccount;

}

}

本例仅仅是联系demo,读取数据表中的内容(只有一项内容)所以才那么写。

六, 测试:

浏览器输入 :
http://localhost:8090/MvcSpringTest/pages 即可进入相应的demo功能测试。

具体实例源码下载链接:
http://download.csdn.net/detail/ccm_oliver/8311087
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: