SPRINGMVC MyBatis PostgreSQL Integration

Source: Internet
Author: User
Tags postgresql tomcat server

Continue to record the learning process Europe ~ ~

Yesterday accidentally made Springmvc MyBatis PostgreSQL code to delete, hey ~ want to undo, results do not allow

But Sey Park is not blessed to lose his horse.

Once again today, it's time to consolidate, but it's true that some of the remaining problems have been solved.

One of the first problems encountered yesterday was that because of incorrect configuration file path, the Web application initialization error caused the failure to load.

I also foolishly through the browser access, the results are discouraged, because it was not the initial initialization of the problem, the real access to the program when the problem,

This situation will also show the error message in Eclipse and then locate it. But now any error message is not, direct access can not, let people are very worried,

Later, the investigation found that when the Tomcat server was started, if the Web application was loaded with a problem, an error message would appear, such as which configuration file could not be read.

Really let me see the hope of life, the problem to exclude.

OK, get to the chase.

The first thing to do is to create a dynamic Web project, and add XML. config as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<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.xml</param-value>
</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>
<!--intercept all requests ending with do-
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

The following code is used to create a global applicationcontext.

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

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

Mycontextloaderlistener inherit from Contextloaderlistener.

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 ();

Get applicationcontext under the Web environment
ApplicationContext CTX = webapplicationcontextutils.getrequiredwebapplicationcontext (context);

Static variable context for Applicationcontext,set to ContextUtil
Contextutil.setcontext (CTX);
}
}

Create ContextUtil

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;
}
}

This way, when needed, you can access the code as follows, without having to get servletcontext by request.

        
ApplicationContext Ctx=contextutil.getcontext ();
Usermapper maper= (Usermapper) Ctx.getbean (Usermapper.class);

Below to configure the SPRINGMVC configuration file Dispatcherservlet-servlet.xml, my understanding is this, hehe

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE beans Public "-//spring//dtd bean//en"
"Http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<!--definition Map--
<bean id= "UrlMapping"
class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name= "Mappings" >
<props>
<prop key= "Helloworld.do" >helloWorldAction</prop>
</props>
</property>
</bean>
<!--definition View--
<bean id= "Viewresolver"
class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "Viewclass" >
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
</bean>

<!--define the controller--
<bean id= "helloworldaction" class= "Com.jp.action.HelloWorldAction" >
<property name= "HelloWorld" >
<value>good luck!</value>
</property>
<property name= "ViewPage" >
<value>/index.jsp</value>
</property>
</bean>
</beans>

My understanding is to map the helloworld.do into the helloworldaction, and then define the display method.

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);
}
}

Where the data is displayed, it is made by the Mapper interface Usermapper, via the MyBatis connection to PostgreSQL.

But this mapper interface Usermapper is generated through the injection of spring.

Take a look at how the Applicationcontext.xml file is injected into the usermapper.

<?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"
xmlns:context= "Http://www.springframework.org/schema/context"
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" class= "Org.apache.commons.dbcp.BasicDataSource" >
<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" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "DataSource" ref= "myDataSource"/>
<property name= "configlocation" value= "Web-inf/classes/config/mybatis-config.xml"/>
</bean>

<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "Basepackage" value= "Mapper"/>
</bean>

<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "myDataSource"/>
</bean>
</beans>

Org.mybatis.spring.mapper.MapperScannerConfigurer scans the Mapper package and generates the interface, which can be understood in the previous article.

Here the main process has been done, a lot of trouble lies in the path of the configuration file, it is very annoying ah.

"Source Address acquisition

SPRINGMVC + mybatis Integration details, and the problems encountered, please refer to the following information:

Resources:

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

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

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

SPRINGMVC MyBatis PostgreSQL Integration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.