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 配置节 :
<localRepository>E:/workspace/maven/repo</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
<?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/> <!-- 自动扫描含有@Service将其注入为bean --> <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>
<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"> <!-- JNDI方式配置数据源 --> <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> <!-- ========================================配置数据源========================================= --> <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${jdbc.initialSize}" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="${jdbc.maxActive}" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="${jdbc.minIdle}" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${jdbc.maxWait}" /> <property name="validationQuery" value="${jdbc.validationQuery}" /> <property name="testOnBorrow" value="${jdbc.testOnBorrow}" /> <property name="testOnReturn" value="${jdbc.testOnReturn}" /> <property name="testWhileIdle" value="${jdbc.testWhileIdle}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="${jdbc.removeAbandoned}" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="${jdbc.logAbandoned}" /> <!-- 监控数据库 --> <property name="filters" value="${jdbc.filters}" /> </bean> <!-- ========================================分隔线============================ --> <!-- =================================针对myBatis的配置项======================= --> <!-- 配置 sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 --> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描com/ouc/mapping/目录下的所有SQL映射的xml文件, 省掉Configuration.xml里的手工配置 value="classpath:com/ouc/mapping/*.xml"指的是classpath(类路径)下com.ouc.mapping包中的所有xml文件 UserMapper.xml位于com.ouc.mapping包下,这样UserMapper.xml就可以被自动扫描 --> <property name="mapperLocations" value="classpath:com/ouc/mapping/*.xml" /> <property name="typeAliasesPackage" value="com.ouc.model" /> <property name="configLocation" value="classpath:mybatis-configure.xml" /> </bean> <!-- 配置 扫描器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描com.ouc.dao这个包以及它的子包下的所有映射接口类 --> <property name="basePackage" value="com.ouc.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- ========================================分隔线============================ --> <!-- 配置Spring的事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 注解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 拦截器方式配置事物 --> <!-- <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="repair" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> <:attributes> <:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.ouc.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> --> <!-- 配置druid监控spring jdbc --> <!-- <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"> </bean> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> <property name="patterns"> <list> <value>com.ouc.service.*<alue> <st> </property> </bean> <aop:config> <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /> </aop:config> --> </beans>
<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"> <!-- 配置druid监控spring jdbc --> <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>
<?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" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.ouc.service.BranchCompanyService" ref="braComService"/> </beans>
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<BranchCompany> 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文件中用到: <!--声明需要暴露的服务接口 --> <dubbo:serviceinterface="com.ouc.service.BranchCompanyService"ref="braComService"/> */ public class BranchCompanySerImplimplements BranchCompanyService{ @Resource BranchCompanyMapperbranchCompanyMapper; @Override public List<BranchCompany> 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<BranchCompany> getBraComsFromDataBase(); }
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEmapperPUBLIC "-//mybatis.org//DTDMapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mappernamespace="com.ouc.dao.BranchCompanyMapper"> <!-- <resultMapid="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> --> <cachetype="org.mybatis.caches.ehcache.LoggingEhcache"> <propertyname="timeToIdleSeconds"value="3600"/> <propertyname="timeToLiveSeconds"value="3600"/> <propertyname="maxEntriesLocalHeap"value="1000"/> <propertyname="maxEntriesLocalDisk"value="10000000"/> <propertyname="memoryStoreEvictionPolicy"value="LRU"/> </cache> <insertid="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> <selectid="getBraComsFromDataBase"resultType="com.ouc.model.BranchCompany"> select * from BranchCompany </select> </mapper>
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; } }
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