myeclipse2014搭建springMVC+Mybatis+多数据源环境

1.新建web工程

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第1张图片

2.添加spring支持

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第2张图片

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第3张图片

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第4张图片

勾上spring persistence

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第5张图片

3.配置文件新建源文件夹

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第6张图片

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第7张图片

applicationContext.xml移动到config下

添加依赖的mybaits和oracle数据包

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第8张图片

4.新建jdbc.properties

rkdata.driverClassName=oracle.jdbc.driver.OracleDriver
rkdata.url=jdbc\:oracle\:thin\:@192.168.118.52\:1521\:orcl
rkdata.username=yhrkdata
rkdata.password=rkdata2015
rkgis.driverClassName=oracle.jdbc.driver.OracleDriver
rkgis.url=jdbc\:oracle\:thin\:@192.168.118.52\:1521\:orcl
rkgis.username=yhrkgis
rkgis.password=rkgis2015

jdbc.maxActive=255
jdbc.maxIdle=20
jdbc.maxWait=100

5.新建log4j.properties

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p %c{1} - %m%n
log4j.logger.org.springframework.security=ERROR

6.新建包结构

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第9张图片

7.新建dispatcher-servlet.xml


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







class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/resources/jsp/" p:suffix=".jsp" />


8.WEB-INF下web.xml添加拦截器配置和字符编码配置


xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
frank

org.springframework.web.context.ContextLoaderListener


contextConfigLocation
classpath:applicationContext.xml


index.jsp



dispatcher
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:dispatcher-servlet.xml


1



dispatcher
/



characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8


forceEncoding
true



characterEncodingFilter
/*

9.配置applicationContext.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">



class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">


classpath:jdbc.properties













































10.编辑文件内容

Example_Mapper.xml


"http://mybatis.org/dtd/mybatis-3-mapper.dtd">






Example.java

package com.founder.example.model;


/**
 * 
 * 
 * @author steve
 * 
 */
public class Example {
}

11.IExampleDao.java

package com.founder.example.dao;
import java.util.List;
import java.util.Map;


/**
 * 
 * 
 * @author steve
 * 
 */
public interface IExampleDao {


List findDataExample();


List findGisExample();
}

ExampleDaoImpl.java

package com.founder.example.dao.impl;


import java.util.List;
import java.util.Map;


import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import com.founder.example.dao.IExampleDao;
import com.founder.example.model.Example;


@Transactional
@Repository
public class ExampleDaoImpl implements IExampleDao {
private SqlSessionTemplate rkdataSession;
private SqlSessionTemplate rkgisSession;


public SqlSessionTemplate getRkdataSession() {
return rkdataSession;
}


@Autowired
public void setRkdataSession(SqlSessionTemplate rkdataSession) {
this.rkdataSession = rkdataSession;
}


public SqlSessionTemplate getRkgisSession() {
return rkgisSession;
}


@Autowired
public void setRkgisSession(SqlSessionTemplate rkgisSession) {
this.rkgisSession = rkgisSession;
}


@Override
public List findDataExample() {
List list = rkdataSession.selectList(Example.class.getName()
+ "_Mapper.findDataExample");
return list;
}


@Override
public List findGisExample() {
List list = rkgisSession.selectList(Example.class.getName()
+ "_Mapper.findGisExample");
return list;
}
}

 12.IExampleService.java

package com.founder.example.service;


import java.util.List;
import java.util.Map;


public interface IExampleService {


List findDataExample();


List findGisExample();
}

ExampleServiceImpl.java

package com.founder.example.service.impl;


import java.util.List;
import java.util.Map;


import javax.annotation.Resource;


import org.springframework.stereotype.Service;


import com.founder.example.dao.IExampleDao;
import com.founder.example.service.IExampleService;


/**
 * @author steve
 *
 */
@Service
public class ExampleServiceImpl implements IExampleService {
@Resource
private IExampleDao exampleDao;


@Override
public List findDataExample() {
return exampleDao.findDataExample();
}


@Override
public List findGisExample() {
return exampleDao.findGisExample();
}


}

13.ExampleController.java

package com.founder.example.controller;


import java.util.List;
import java.util.Map;


import javax.annotation.Resource;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.founder.example.service.IExampleService;


@Controller
public class ExampleController {
@Resource
private IExampleService exampleService;


@RequestMapping("/findDataExample")
public ModelAndView findDataExample() {
List list = exampleService.findDataExample();// 传单独的值调用service方法
return new ModelAndView("dataExample", "list", list);
}


@RequestMapping("/findGisExample")
public ModelAndView findGisExample() {
List list = exampleService.findGisExample();// 传单独的值调用service方法
System.out.println(list.size());
return new ModelAndView("gisExample", "list", list);
}
}

14.新建jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib
prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>






My JSP 'dataExample.jsp' starting page






:
       




15.启动服务

访问http://localhost:8080/frank/findDataExample.action

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第10张图片

访问http://localhost:8080/frank/findGisExample.action

myeclipse2014搭建springMVC+Mybatis+多数据源环境_第11张图片

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(框架)