Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建
MyEclipse或Eclipse,数据库使用Oracle。需要用到的软件有Zookeeper(注册中心),Tomcat(Web容器)和Maven(包管理)。
解压zookeeper-3.4.6到自己的安装目录(安装目录中最好不要出现中文)。
解压apache-maven-3.3.3到自己的安装目录。
解压apache-tomcat-7.0.63到自己的安装目录。
下载地址:http://zookeeper.apache.org/releases.html
(该实例使用版本是zookeeper-3.4.6)
例如windows下;
dataDir=E:\\wordDubbo\\zookeeper-3.4.6\\data
dataLogDir=E:\\wordDubbo\\zookeeper-3.4.6\\log
预先创建文件夹data和log
例如windows下:zookeeper-3.4.6\bin\zkServer.cmd
选择 Maven 3.3.3 (Binary tar.gz) 或 Maven 3.3.3 (Binary zip)
解压:将下载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 版本号,系统环境配置成功。
修改D:\apache-maven-3.3.3 下的 conf 文件夹下的 settings.xml 文件
更改localRepository 配置节 :
在 cmd 中敲并回车执行:mvn help:system
打开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“ ,保存确认。
准备:
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
File->New->Maven Project
添加本地Maven环境依赖:
Windows->Preferences->MyEclipse->Maven4MyEclipse->Installations->add->选择本地apache-maven-3.3.3安装路径->apply->UseSetting修改为本地。
备注:搭建中如果报错,引入dubbo.xsd离线文件,
不能识别:dubbo:application标签
下载dubbo.xsd
windows->preferences->MyEclicpse->XML->XMLCatalog
Add->UserSpecified Entries->http://code.alibabatech.com/schema/dubbo/dubbo.xsd
com.ouc.service.*
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:连接字符串
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
在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有关的类,其他类不需新建,我新建是项目所需。
package com.ouc.model;
import java.io.Serializable;
/* ----模块商分公司------------
*
*/
public class BranchCompany implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id; //分公司ID
private String name; //分公司名称
private String address; //地址
private String telNumber; //联系方式
private String moduleNo; //模块商编号
private Integer labId; //实验室ID
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;
}
}
package com.ouc.service;
importjava.util.List;
importcom.ouc.model.BranchCompany;
public interface BranchCompanyService {
public List getBranchCompanys();
public int saveBranchCompany(BranchCompanybranchCompany);
}
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")
/* 此处注解在Spring-Dubbo.xml文件中用到:
*/
public class BranchCompanySerImplimplements BranchCompanyService{
@Resource BranchCompanyMapperbranchCompanyMapper;
@Override
public List getBranchCompanys()
{
return branchCompanyMapper.getBraComsFromDataBase();
}
@Override
public int saveBranchCompany(BranchCompanybranchCompany)
{
return branchCompanyMapper.insertBranchCompany(branchCompany);
}
}
package com.ouc.dao;
importjava.util.List;
importcom.ouc.model.BranchCompany;
public interface BranchCompanyMapper {
int insertBranchCompany(BranchCompanybranchCompany);
public List getBraComsFromDataBase();
}
insert into BranchCompany (
id,name,address,telNumber,moduleNo,labId,remark)
values (
#{id},
#{name},
#{address},
#{telNumber},
#{moduleNo},
#{labId},
#{remark}
)
select * from BranchCompany
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(){
ListbraComs=braComService.getBranchCompanys();
returnbraComs;
}
}
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();
/*braComService =(BranchCompanyService)cxt.getBean("braComService");
BranchCompany braCom = newBranchCompany();
braCom.setName("Hai");
braCom.setId(1);
braCom.setLabId(2);
braCom.setAddress("SSdsg");
braCom.setModuleNo("SSS2000");
braCom.setTelNumber("125604");
braCom.setRemark(" ");
braComService.saveBranchCompany(braCom);
*/
}
}
运行 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