Maven+druid+MyBatis+spring+Oracle+Dubbo开发环境搭建
1.开发工具使用:
MyEclipse或Eclipse,数据库使用Oracle。需要用到的软件有Zookeeper(注册中心),Tomcat(Web容器)和Maven(包管理)。
2.初始环境配置:
解压zookeeper-3.4.6到自己的安装目录(安装目录中最好不要出现中文)。
解压apache-maven-3.3.3到自己的安装目录。
解压apache-tomcat-7.0.63到自己的安装目录。
3. zookeeper-3.4.6安装与配置
1)下载zookeeper,
下载地址:http://zookeeper.apache.org/releases.html
(该实例使用版本是zookeeper-3.4.6)
2)修改zookeeper-3.4.6\conf\zoo.cfg
例如windows下;
dataDir=E:\\wordDubbo\\zookeeper-3.4.6\\data
dataLogDir=E:\\wordDubbo\\zookeeper-3.4.6\\log
预先创建文件夹data和log
3)启动zookeeper
例如windows下:zookeeper-3.4.6\bin\zkServer.cmd
4 . apache-maven-3.3.3安装与配置
1) MAVEN 下载地址:
选择 Maven 3.3.3 (Binary tar.gz) 或 Maven 3.3.3 (Binary zip)
2) 配置系统环境:
解压:将下载MAVEN (apache-maven-3.3.3)压缩包进行解压放置在 D盘根目录下。
设置环境变量:计算机环境变量(系统变量部分) -> 新建变量名" MAVEN_HOME ",变量值:" D:\apache-maven-3.3.3 " 确定(保存),更改path: 在末尾追加 ” %MAVEN_HOME%\bin ;“。
打开cmd 命令输入 ” MVN -V “ 命令行出现, MAVEN 3.3.3 版本号,系统环境配置成功。
3)更改maven 的仓库默认存放地址
修改D:\apache-maven-3.3.3 下的 conf 文件夹下的 settings.xml 文件
更改localRepository 配置节 :
<localRepository>E:/workspace/maven/repo</localRepository>
4)下载相关包
在 cmd 中敲并回车执行:mvn help:system
5) 在Myeclipse 中使用Maven
打开myeclipse2014 选择 window -> prefences -> MyEclipse 下的-> Maven4MyEclipse
(1)修改 " Maven4MyEclipse" 下的Installations 点击 ” Add “ 选择目录 ” D:\apache-maven-3.3.3“
Global settings from installationdirectory( open file) 选择D:\apache-maven-3.3.3\conf\settings.xml
(2)修改 " Maven4MyEclipse" 下的 User Settings 下的 User Settings 属性” D:\apache-maven-3.3.3\conf\settings.xml“ ,保存确认。
5 . apache-tomcat-7.0.63安装与配置
准备:
1)安装好Java的JDK
2)下载好tomcat:版本为apache-tomcat-7.0.63,并解压到硬盘中。
配置步骤:
1)JDK:
(1)新建变量名:JAVA_HOME,
变量值:D:\Program Files\Java\jdk1.7.0_25
(2)打开PATH,添加变量值:
%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin
(3)新建变量名:CLASSPATH,变量值:
%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
备注:
(1).表示当前路径,%JAVA_HOME%就是引用前面指定的JAVA_HOME;
(2)JAVA_HOME指明JDK安装路径,此路径下包括lib,bin,jre等文件夹,tomcat,eclipse等的运行都需要依靠此变量。
(3)PATH使得系统可以在任何路径下识别java命令。
(4)CLASSPATH为java加载类(class or lib)路径,只有类在classpath中,java命令才能识别。
2)配置Tomcat环境变量
(1)新建变量名:CATALINA_BASE,变量值:apache-tomcat-7.0.63
(2)新建变量名:CATALINA_HOME,变量值:apache-tomcat-7.0.63
(3)打开PATH,添加变量值:
%CATALINA_HOME%\lib;%CATALINA_HOME%\bin
6. 新建Maven工程:(注:直接导工程请跳过此步骤)
File->New->Maven Project
添加本地Maven环境依赖:
Windows->Preferences->MyEclipse->Maven4MyEclipse->Installations->add->选择本地apache-maven-3.3.3安装路径->apply->UseSetting修改为本地。
7.工程结构:
8. Maven pom.xml文件配置:
备注:搭建中如果报错,引入dubbo.xsd离线文件,
不能识别:dubbo:application标签
下载dubbo.xsd
windows->preferences->MyEclicpse->XML->XMLCatalog
Add->UserSpecified Entries->http://code.alibabatech.com/schema/dubbo/dubbo.xsd
9.Spring配置:Spring.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:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
-
- <aop:aspectj-autoproxy/>
-
- <context:component-scan base-package="com.ouc.service"/>
- <context:component-scan base-package="com.ouc.service.impl"/>
-
- <!--
- <import resource="spring-mvc.xml"/>
- <import resource="spring-mybatis.xml"/>
- -->
-
- <context:annotation-config />
-
- <context:property-placeholder location="classpath:jdbc.properties"/>
- </beans>
10. Spring-mybatis配置:Spring-mybatis.Xml
11.Spring-druid配置:Spring-druid.Xml
- <pre name="code" class="html"><?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:aop="http://www.springframework.org/schema/aop"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xmlns:task="http://www.springframework.org/schema/task"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
- http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
-
-
- <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />
-
- <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
- <property name="patterns">
- <list>
- <value>com.ouc.service.*</value>
- </list>
- </property>
- </bean>
- <aop:config>
- <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
- </aop:config>
-
- </beans>
12.Spring-dubbo配置:Spring-dubbo.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:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
-
-
- <dubbo:application name="U-LabProvider" />
-
-
-
- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
-
-
- <dubbo:protocol name="dubbo" port="20880" />
-
-
- <dubbo:service interface="com.ouc.service.BranchCompanyService" ref="braComService"/>
- </beans>
13. jdbc.properties配置:jdbc.properties
- jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
- jdbc.url=jdbc\:oracle\:thin\:@222.195.151.239\:1521\:orcl
- jdbc.username=ULab
- jdbc.password=ouccs
- jdbc.initialSize=2
- jdbc.maxActive=300
- jdbc.minIdle=0
- jdbc.maxWait=60000
- jdbc.validationQuery=SELECT 1 FROM DUAL
- jdbc.testOnBorrow=false
- jdbc.testOnReturn=false
- jdbc.testWhileIdle=true
- jdbc.timeBetweenEvictionRunsMillis=60000
- jdbc.minEvictableIdleTimeMillis=25200000
- jdbc.removeAbandoned=true
- jdbc.removeAbandonedTimeout=1800
- jdbc.logAbandoned=true
- jdbc.poolPreparedStatements=true
- jdbc.maxOpenPreparedStatements=200
- jdbc.filters=stat
jdbc.driverClassName:数据库驱动程序名称
jdbc.url:连接字符串
14. log4j.properties配置:log4j.properties,该文件无需修改,直接拷贝即可。
- log4j.rootLogger=INFO,stdout,logfile
-
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
-
- log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
- log4j.appender.logfile.File=/U-Lab/local/log/maven
- log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
- log4j.appender.logfile.DatePattern='_'yyyy-MM-dd'.log'
- log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %-5p [%t] %c.%M(%L) - %m%n
-
- log4j.logger.com.ibatis=debug
- log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
- log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
- log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
- log4j.logger.java.sql.Connection=debug
- log4j.logger.java.sql.Statement=debug
- log4j.logger.java.sql.PreparedStatement=debug,stdout
-
- log4j.logger.jdbc.sqlonly=OFF
- log4j.logger.jdbc.sqltiming=OFF
- log4j.logger.jdbc.audit=OFF
- log4j.logger.jdbc.resultset=OFF
- log4j.logger.jdbc.connection=OFF
15.新建src/main/java资源文件,新建src/main/resources资源文件,新建src/test/java资源文件,新建src/test/resources资源文件.
在src/main/java资源文件下新建如下包:
com.ouc.model
com.ouc.service
com.ouc.service.impl
com.ouc.dao
com.ouc.mapping
com.ouc.controller
com.ouc.test
com.ouc.main
类的示例:例子中只用到与BranchCompany有关的类,其他类不需新建,我新建是项目所需。
16. 例子中只用到与BranchCompany有关的类
(1)model数据实体类:BranchCompany
- package com.ouc.model;
-
- import java.io.Serializable;
-
-
-
-
-
- public class BranchCompany implements Serializable {
-
- private static final long serialVersionUID = 1L;
-
- private Integer id;
- private String name;
- private String address;
- private String telNumber;
- private String moduleNo;
- private Integer labId;
- private String remark;
-
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
-
- public String getTelNumber() {
- return telNumber;
- }
- public void setTelNumber(String telNumber) {
- this.telNumber = telNumber;
- }
-
- public String getModuleNo() {
- return moduleNo;
- }
- public void setModuleNo(String moduleNo) {
- this.moduleNo = moduleNo;
- }
-
- public Integer getLabId() {
- return labId;
- }
- public void setLabId(Integer labId) {
- this.labId = labId;
- }
-
- public String getRemark() {
- return remark;
- }
- public void setRemark(String remark) {
- this.remark = remark;
- }
- }
(2)服务接口类:BranchCompanyService
- package com.ouc.service;
-
- importjava.util.List;
- importcom.ouc.model.BranchCompany;
-
- public interface BranchCompanyService {
-
- public List<BranchCompany> getBranchCompanys();
-
- public int saveBranchCompany(BranchCompanybranchCompany);
- }
(3)服务接口实现类:BranchCompanySerImpl
- package com.ouc.service.impl;
-
- importcom.ouc.dao.BranchCompanyMapper;
- import com.ouc.model.BranchCompany;
- importcom.ouc.service.BranchCompanyService;
-
- importjava.util.List;
-
- importjavax.annotation.Resource;
- importorg.springframework.stereotype.Service;
-
- @Service("braComService")
-
-
-
-
-
- public class BranchCompanySerImplimplements BranchCompanyService{
-
- @Resource BranchCompanyMapperbranchCompanyMapper;
-
- @Override
- public List<BranchCompany> getBranchCompanys()
- {
- return branchCompanyMapper.getBraComsFromDataBase();
- }
-
- @Override
- public int saveBranchCompany(BranchCompanybranchCompany)
- {
- return branchCompanyMapper.insertBranchCompany(branchCompany);
- }
- }
(4)数据库操作接口dao类:BranchCompanyMapper
- package com.ouc.dao;
-
- importjava.util.List;
- importcom.ouc.model.BranchCompany;
-
- public interface BranchCompanyMapper {
-
- int insertBranchCompany(BranchCompanybranchCompany);
-
- public List<BranchCompany> getBraComsFromDataBase();
- }
(5)数据库SQL语句XML:BranchCompanyMapper.Xml
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEmapperPUBLIC "-//mybatis.org//DTDMapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mappernamespacemappernamespace="com.ouc.dao.BranchCompanyMapper">
-
- <!--
- <resultMapidresultMapid="BraComResultMap" type="com.ouc.model.BranchCompany">
- <id column="id"property="id" jdbcType="INTEGER" />
- <result column="name"property="name" jdbcType="VARCHAR" />
- <result column="address"property="address" jdbcType="VARCHAR" />
- <result column="telNumber"property="telNumber" jdbcType="VARCHAR" />
- <result column="moduleNo"property="moduleNo" jdbcType="VARCHAR" />
- <result column="labId"property="labId" jdbcType="INTEGER" />
- <result column="remark"property="remark" jdbcType="VARCHAR" />
- </resultMap>
- -->
-
- <cachetypecachetype="org.mybatis.caches.ehcache.LoggingEhcache">
- <propertynamepropertyname="timeToIdleSeconds"value="3600"/>
- <propertynamepropertyname="timeToLiveSeconds"value="3600"/>
- <propertynamepropertyname="maxEntriesLocalHeap"value="1000"/>
- <propertynamepropertyname="maxEntriesLocalDisk"value="10000000"/>
- <propertynamepropertyname="memoryStoreEvictionPolicy"value="LRU"/>
- </cache>
-
- <insertidinsertid="insertBranchCompany"parameterType="com.ouc.model.BranchCompany"useGeneratedKeys="true"keyProperty="id"flushCache="true">
- insert into BranchCompany (
- id,name,address,telNumber,moduleNo,labId,remark)
- values (
- #{id},
- #{name},
- #{address},
- #{telNumber},
- #{moduleNo},
- #{labId},
- #{remark}
- )
- </insert>
-
- <selectidselectid="getBraComsFromDataBase"resultType="com.ouc.model.BranchCompany">
- select * from BranchCompany
- </select>
- </mapper>
(6)运行控制类:BranchCompanyController
- package com.ouc.controller;
-
- importjavax.annotation.Resource;
- importjava.util.List;
- importorg.springframework.stereotype.Controller;
- importorg.springframework.web.bind.annotation.RequestMapping;
- importorg.springframework.web.bind.annotation.ResponseBody;
-
- importcom.ouc.service.BranchCompanyService;
- importcom.ouc.model.BranchCompany;
-
- @Controller
- @RequestMapping("/BranchCompanyController")
- public classBranchCompanyController {
- @Resource
- private BranchCompanyService braComService;
-
- @RequestMapping(value ="/BraCom_view")
- public @ResponseBody ObjectGetBranchCompany(){
- List<BranchCompany>braComs=braComService.getBranchCompanys();
- returnbraComs;
- }
- }
17.测试类:Provider
- package com.ouc.test;
-
- importorg.springframework.context.support.ClassPathXmlApplicationContext;
- importcom.ouc.model.*;
- import com.ouc.service.*;
-
- public classProvider {
- private static BranchCompanyServicebraComService;
-
- public static void main(String[] args) throwsException {
-
- ClassPathXmlApplicationContext cxt = newClassPathXmlApplicationContext(
- newString[]{"classpath:spring.xml","classpath:spring-mybatis.xml","classpath:spring-dubbo.xml"},true);
-
- cxt.start();
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
-
运行 D:\Java\zookeeper-3.4.6\bin下的zkServer
zookeeper-3.4.6 ,apache-maven-3.3.3 ,apache-tomcat-7.0.63三个软件需要配置,请自行网上百度。
安装配置dubbo-admin-2.5.4
示例路径如下:
D:\Java\apache-tomcat-7.0.63\webapps\dubbo-admin-2.5.4
运行Provider,浏览器输入:http://localhost:8090/ dubbo-admin-2.5.4查看服务发布结果。
我修改了tomcat的端口,未修改的上面端口为8080.
Maven编译时主要是包的导入问题,如果无法在线下载包,请手动添加到对应路径。
相关学习网址:
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool
MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
http://www.cnblogs.com/xdp-gacl/p/4271627.html