星空网 > 软件开发 > ASP.net

SpringMVC MyBatis PostgreSQL整合

继续记录学习过程欧~~

昨天不小心把辛辛苦苦做的SpringMVC MyBatis PostgreSQL代码给删除掉了,哎~想undo,结果不允许

不过塞公失马焉知非福

今天再来一遍就是了,就当是巩固了,不过确实把一些遗留的问题给解决了。

昨天首先遇到的一个问题就是,由于配置文件路径不对,导致Web应用程序初始化错误,导致无法加载,

我还傻乎乎的通过浏览器访问,结果心灰意冷,因为以前都是初始化没有问题,真正访问程序的时候出问题,

此种情况还会在eclipse里面显示出错误信息再进行定位。但是现在什么错误信息都没有,直接访问不了了,就让人很担心,

后来调查发现,在启动tomcat服务器的时候,如果Web应用程序加载有问题,会出现错误信息,比如哪个配置文件无法读取之类的错误。

着实让我看到了生的希望,把问题给排除了。

OK,进入正题。

首先肯定是要创建动态web工程,加入web.SpringMVC MyBatis PostgreSQL整合

<?<web-app version="2.4"     xsi:schemaLocation="http://java.sun.com/  http://java.sun.com/  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/config/applicationContext. </context-param>


<listener>
<listener-class>my.MyContextLoaderListener</listener-class>
</listener>

<!--
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
-->

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- 拦截所有以do结尾的请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC MyBatis PostgreSQL整合

下面这段代码是为了创建全局的ApplicationContext用的。

SpringMVC MyBatis PostgreSQL整合
  <context-param>  
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/config/applicationContext. </context-param>

<listener>
<listener-class>my.MyContextLoaderListener</listener-class>
</listener>
SpringMVC MyBatis PostgreSQL整合

MyContextLoaderListener从ContextLoaderListener继承。

SpringMVC MyBatis PostgreSQL整合
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class MyContextLoaderListener extends ContextLoaderListener{

@Override
public void contextInitialized(ServletContextEvent event){
super.contextInitialized(event);
ServletContext context = event.getServletContext();

//获取web环境下的ApplicationContext
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

//将ApplicationContext,set到ContextUtil的静态变量context
ContextUtil.setContext(ctx);
}
}
SpringMVC MyBatis PostgreSQL整合

创建ContextUtil

SpringMVC MyBatis PostgreSQL整合
package my;

import org.springframework.context.ApplicationContext;

public class ContextUtil {
private static ApplicationContext context;

public static ApplicationContext getContext() {
return context;
}

public static void setContext(ApplicationContext aContext) {
context = aContext;
}
}
SpringMVC MyBatis PostgreSQL整合

这样在需要的时候,就可以像下面这样访问代码,而不需要通过request等得到ServletContext再去获取了。

    //ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext()); 
ApplicationContext ctx=ContextUtil.getContext();
UserMapper maper=(UserMapper)ctx.getBean(UserMapper.class);
User user = maper.getUser("fff");

 

下面来配置SpringMVC的配置文件dispatcherServlet-servlet.SpringMVC MyBatis PostgreSQL整合

<?<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 定义映射 -->
<bean id="urlMapping"
>
<property name="mappings">
<props>
<prop key="helloWorld.do">helloWorldAction</prop>
</props>
</property>
</bean>
<!-- 定义视图 -->
<bean id="viewResolver"
>
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
</bean>

<!-- 定义控制器 -->
<bean id="helloWorldAction" >
<property name="helloWorld">
<value>Good Luck!</value>
</property>
<property name="viewPage">
<value>/index.jsp</value>
</property>
</bean>
</beans>
SpringMVC MyBatis PostgreSQL整合

我的理解就是把helloWorld.do映射到helloWorldAction里面去,再定义一下显示的方法。

SpringMVC MyBatis PostgreSQL整合
package com.jp.action;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.ContextUtil;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import bean.User;
import Mapper.UserMapper;

public class HelloWorldAction implements Controller{
private Logger logger=Logger.getLogger(this.getClass().getName());
private String helloWorld;
private String viewPage;

public String getHelloWorld() {
return helloWorld;
}

public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}

public String getViewPage() {
return viewPage;
}

public void setViewPage(String viewPage) {
this.viewPage = viewPage;
}

public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
// TODO Auto-generated method stub

//ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
ApplicationContext ctx=ContextUtil.getContext();
UserMapper maper=(UserMapper)ctx.getBean(UserMapper.class);
User user = maper.getUser("fff");

Map model=new HashMap();
model.put("helloWorld",user.getName());
return new ModelAndView(getViewPage(),model);
}
}
SpringMVC MyBatis PostgreSQL整合

这里在取显示数据的地方,是由映射器接口UserMapper,通过MyBatis连接PostgreSQL取得的。

但这个映射器接口UserMapper是通过Spring的注入生成。

来看看applicationContext.SpringMVC MyBatis PostgreSQL整合

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

<bean id="myDataSource" >
<property name="driverClassName" value="org.postgresql.Driver">
</property>
<property name="url"
value="jdbc:postgresql:testdb">
</property>
<property name="username" value="postgres"></property>
<property name="password" value="nirvana7"></property>
</bean>

<bean id="sqlSessionFactory" >
<property name="dataSource" ref="myDataSource"/>
<property name="configLocation" value="WEB-INF/classes/config/mybatis-config. </bean>

<bean >
<property name="basePackage" value="Mapper"/>
</bean>

<bean id="transactionManager" >
<property name="dataSource" ref="myDataSource"/>
</bean>
</beans>
SpringMVC MyBatis PostgreSQL整合

org.mybatis.spring.mapper.MapperScannerConfigurer会扫描Mapper的package,然后生成接口,可以参照前篇文章来了解。

到这里主要流程都已经搞定了,很多麻烦的地方在于配置文件的路径问题,很是让人烦恼啊。

》》源码地址获取

springmvc + mybatis整合详细,及遇到的问题请参看以下资料:

参考资料:

http://www.springmvc,net/detail/6074493.html

http://my.spring.net/wangbiglei/blog/489583

http://my.springmvc.net/wangbiglei/blog/489604




原标题:SpringMVC MyBatis PostgreSQL整合

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流