Fork me on GitHub

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:util="http://www.springframework.org/schema/util"
 6     xsi:schemaLocation="
 7           http://www.springframework.org/schema/beans
 8           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9        http://www.springframework.org/schema/tx
10        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
11        http://www.springframework.org/schema/aop
12        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
13        http://www.springframework.org/schema/util
14        http://www.springframework.org/schema/util/spring-util-3.1.xsd
15        http://www.springframework.org/schema/context
16        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
17     
18     <aop:aspectj-autoproxy/>
19     <!-- 自动扫描含有@Service将其注入为bean -->
20     <context:component-scan base-package="com.ouc.service"/>    
21     <context:component-scan base-package="com.ouc.service.impl"/>
22     
23     <!--  
24     <import resource="spring-mvc.xml"/>
25     <import resource="spring-mybatis.xml"/>
26     -->
27     
28     <context:annotation-config />
29     <!--引入配置属性文件  -->
30     <context:property-placeholder location="classpath:jdbc.properties"/>
31 </beans>
View Code

     10. Spring-mybatis配置:Spring-mybatis.Xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans"    
  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4     xmlns:p="http://www.springframework.org/schema/p"  
  5     xmlns:aop="http://www.springframework.org/schema/aop"
  6     xmlns:cache="http://www.springframework.org/schema/cache"
  7     xmlns:task="http://www.springframework.org/schema/task"   
  8     xmlns:context="http://www.springframework.org/schema/context"  
  9     xmlns:jee="http://www.springframework.org/schema/jee"  
 10     xmlns:tx="http://www.springframework.org/schema/tx"  
 11     xsi:schemaLocation="    
 12         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 13         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
 14         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd  
 15         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
 16         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
 17         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
 18         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
 19     
 20     <!-- JNDI方式配置数据源 -->
 21     <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
 22     
 23     <!-- ========================================配置数据源========================================= -->
 24     <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 -->
 25     
 26     <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
 27         <property name="driverClassName" value="${jdbc.driverClassName}" />
 28         <property name="url" value="${jdbc.url}" />
 29         <property name="username" value="${jdbc.username}" />
 30         <property name="password" value="${jdbc.password}" />
 31 
 32         <!-- 初始化连接大小 -->
 33         <property name="initialSize" value="${jdbc.initialSize}" />
 34         <!-- 连接池最大使用连接数量 -->
 35         <property name="maxActive" value="${jdbc.maxActive}" />
 36         <!-- 连接池最小空闲 -->
 37         <property name="minIdle" value="${jdbc.minIdle}" />
 38         <!-- 获取连接最大等待时间 -->
 39         <property name="maxWait" value="${jdbc.maxWait}" />
 40 
 41         <property name="validationQuery" value="${jdbc.validationQuery}" />
 42         <property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
 43         <property name="testOnReturn" value="${jdbc.testOnReturn}" />
 44         <property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
 45 
 46         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
 47         <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
 48         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
 49         <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
 50 
 51         <!-- 打开removeAbandoned功能 -->
 52         <property name="removeAbandoned" value="${jdbc.removeAbandoned}" />
 53         <!-- 1800秒,也就是30分钟 -->
 54         <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
 55         <!-- 关闭abanded连接时输出错误日志 -->
 56         <property name="logAbandoned" value="${jdbc.logAbandoned}" />
 57 
 58         <!-- 监控数据库 -->
 59         <property name="filters" value="${jdbc.filters}" />
 60     </bean>
 61     
 62     <!-- ========================================分隔线============================ -->
 63     
 64     <!-- =================================针对myBatis的配置项======================= -->
 65     
 66     <!-- 配置 sqlSessionFactory-->
 67     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 68         <!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
 69         <property name="dataSource" ref="dataSource" />
 70         
 71         <!-- 自动扫描com/ouc/mapping/目录下的所有SQL映射的xml文件, 省掉Configuration.xml里的手工配置
 72         value="classpath:com/ouc/mapping/*.xml"指的是classpath(类路径)下com.ouc.mapping包中的所有xml文件
 73         UserMapper.xml位于com.ouc.mapping包下,这样UserMapper.xml就可以被自动扫描
 74         -->
 75         
 76         <property name="mapperLocations" value="classpath:com/ouc/mapping/*.xml" />
 77         <property name="typeAliasesPackage" value="com.ouc.model" />
 78         <property name="configLocation" value="classpath:mybatis-configure.xml" />
 79     </bean>
 80     
 81     <!-- 配置 扫描器-->
 82     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 83         <!-- 扫描com.ouc.dao这个包以及它的子包下的所有映射接口类 -->
 84         <property name="basePackage" value="com.ouc.dao" />
 85         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 86     </bean>
 87     
 88     <!-- ========================================分隔线============================ -->
 89     
 90     <!-- 配置Spring的事务管理 -->
 91     <bean id="transactionManager"
 92         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 93         <property name="dataSource" ref="dataSource" />
 94     </bean>
 95    
 96     
 97 <!-- 注解方式配置事物 -->
 98     <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
 99    
100     <!-- 拦截器方式配置事物 -->
101     <!--  
102     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
103         <tx:attributes>
104             <tx:method name="add*" propagation="REQUIRED" />
105             <tx:method name="append*" propagation="REQUIRED" />
106             <tx:method name="insert*" propagation="REQUIRED" />
107             <tx:method name="save*" propagation="REQUIRED" />
108             <tx:method name="update*" propagation="REQUIRED" />
109             <tx:method name="modify*" propagation="REQUIRED" />
110             <tx:method name="edit*" propagation="REQUIRED" />
111             <tx:method name="delete*" propagation="REQUIRED" />
112             <tx:method name="remove*" propagation="REQUIRED" />
113             <tx:method name="repair" propagation="REQUIRED" />
114             <tx:method name="delAndRepair" propagation="REQUIRED" />
115 
116             <tx:method name="get*" propagation="SUPPORTS" />
117             <tx:method name="find*" propagation="SUPPORTS" />
118             <tx:method name="load*" propagation="SUPPORTS" />
119             <tx:method name="search*" propagation="SUPPORTS" />
120             <tx:method name="datagrid*" propagation="SUPPORTS" />
121 
122             <tx:method name="*" propagation="SUPPORTS" />
123         <:attributes>
124     <:advice>
125     
126     <aop:config>
127         <aop:pointcut id="transactionPointcut" expression="execution(* com.ouc.service..*Impl.*(..))" />
128         <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
129     </aop:config>
130     -->
131     
132     <!-- 配置druid监控spring jdbc -->
133     <!--  
134     <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
135     </bean>
136     
137     <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
138         <property name="patterns">
139             <list>
140                 <value>com.ouc.service.*<alue>
141             <st>
142         </property>
143     </bean>
144     
145     <aop:config>
146         <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
147     </aop:config>
148     -->
149 </beans>
View Code

   11.Spring-druid配置:Spring-druid.Xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"    
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 4     xmlns:p="http://www.springframework.org/schema/p"  
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:cache="http://www.springframework.org/schema/cache"
 7     xmlns:task="http://www.springframework.org/schema/task"   
 8     xmlns:context="http://www.springframework.org/schema/context"  
 9     xmlns:jee="http://www.springframework.org/schema/jee"  
10     xmlns:tx="http://www.springframework.org/schema/tx"  
11     xsi:schemaLocation="    
12         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
13         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
14         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd  
15         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
16         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
17         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
18         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">   
19 
20     <!-- 配置druid监控spring jdbc -->
21     <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />
22 
23     <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
24         <property name="patterns">
25             <list>
26                 <value>com.ouc.service.*</value>
27             </list>
28         </property>
29     </bean>
30     <aop:config>
31         <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
32     </aop:config>
33 
34 </beans>
View Code

  12.Spring-dubbo配置:Spring-dubbo.Xml (备注:提供者的配置)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://code.alibabatech.com/schema/dubbo
 8         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 9 
10     <!-- 提供方应用信息,用于计算依赖关系 -->
11     <dubbo:application name="U-LabProvider" />
12 
13     <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
14 
15     <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
16 
17     <!-- 用dubbo协议在20880端口暴露服务 -->
18     <dubbo:protocol name="dubbo" port="20880" />
19     
20     <!-- 声明需要暴露的服务接口 -->
21     <dubbo:service interface="com.ouc.service.BranchCompanyService" ref="braComService"/>
22 </beans>
View Code

     13. jdbc.properties配置:jdbc.properties

 1 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
 2 jdbc.url=jdbc\:oracle\:thin\:@222.195.151.239\:1521\:orcl
 3 jdbc.username=ULab
 4 jdbc.password=ouccs
 5 jdbc.initialSize=2
 6 jdbc.maxActive=300
 7 jdbc.minIdle=0
 8 jdbc.maxWait=60000
 9 jdbc.validationQuery=SELECT 1 FROM DUAL
10 jdbc.testOnBorrow=false
11 jdbc.testOnReturn=false
12 jdbc.testWhileIdle=true
13 jdbc.timeBetweenEvictionRunsMillis=60000
14 jdbc.minEvictableIdleTimeMillis=25200000
15 jdbc.removeAbandoned=true
16 jdbc.removeAbandonedTimeout=1800
17 jdbc.logAbandoned=true
18 jdbc.poolPreparedStatements=true
19 jdbc.maxOpenPreparedStatements=200
20 jdbc.filters=stat

     jdbc.driverClassName:数据库驱动程序名称

     jdbc.url:连接字符串

  14. log4j.properties配置:log4j.properties,该文件无需修改,直接拷贝即可。

 1 log4j.rootLogger=INFO,stdout,logfile
 2 
 3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
 5 log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n  
 6 
 7 log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
 8 log4j.appender.logfile.File=/U-Lab/local/log/maven
 9 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
10 log4j.appender.logfile.DatePattern='_'yyyy-MM-dd'.log'
11 log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %-5p [%t] %c.%M(%L) - %m%n
12   
13 log4j.logger.com.ibatis=debug
14 log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
15 log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
16 log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
17 log4j.logger.java.sql.Connection=debug
18 log4j.logger.java.sql.Statement=debug
19 log4j.logger.java.sql.PreparedStatement=debug,stdout
20 
21 log4j.logger.jdbc.sqlonly=OFF
22 log4j.logger.jdbc.sqltiming=OFF
23 log4j.logger.jdbc.audit=OFF
24 log4j.logger.jdbc.resultset=OFF
25 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

 1 package com.ouc.model;
 2 
 3 import java.io.Serializable;
 4 
 5 /* ----模块商分公司------------
 6  * 
 7  */
 8 
 9 public class BranchCompany implements Serializable {
10     
11     private static final long serialVersionUID = 1L;
12     
13     private Integer id;                      //分公司ID
14     private String name;                     //分公司名称
15     private String address;                  //地址   
16     private String telNumber;                //联系方式   
17     private String moduleNo;                 //模块商编号
18     private Integer labId;                   //实验室ID
19     private String remark;                   //备注
20     
21     public Integer getId() {
22         return id;
23     }
24     public void setId(Integer id) {
25         this.id = id;
26     }
27     
28     public String getName() {
29         return name;
30     }
31     public void setName(String name) {
32         this.name = name;
33     }
34     
35     public String getAddress() {
36         return address;
37     }
38     public void setAddress(String address) {
39         this.address = address;
40     }
41     
42     public String getTelNumber() {
43         return telNumber;
44     }
45     public void setTelNumber(String telNumber) {
46         this.telNumber = telNumber;
47     }
48     
49     public String getModuleNo() {
50         return moduleNo;
51     }
52     public void setModuleNo(String moduleNo) {
53         this.moduleNo = moduleNo;
54     }
55     
56     public Integer getLabId() {
57         return labId;
58     }
59     public void setLabId(Integer labId) {
60         this.labId = labId;
61     }
62     
63     public String getRemark() {
64         return remark;
65     }
66     public void setRemark(String remark) {
67         this.remark = remark;
68     }
69 }
View Code

  (2)服务接口类:BranchCompanyService

 1 package com.ouc.service;
 2  
 3 importjava.util.List;
 4 importcom.ouc.model.BranchCompany;
 5  
 6 public interface BranchCompanyService {
 7    
 8    public List<BranchCompany> getBranchCompanys();
 9   
10    public int saveBranchCompany(BranchCompanybranchCompany);
11 }
View Code

   (3)服务接口实现类:BranchCompanySerImpl

 1 package com.ouc.service.impl;
 2  
 3 importcom.ouc.dao.BranchCompanyMapper;
 4 import com.ouc.model.BranchCompany;
 5 importcom.ouc.service.BranchCompanyService;
 6  
 7 importjava.util.List;
 8  
 9 importjavax.annotation.Resource;
10 importorg.springframework.stereotype.Service;
11  
12 @Service("braComService")
13 /* 此处注解在Spring-Dubbo.xml文件中用到:
14 <!--声明需要暴露的服务接口 -->
15    <dubbo:serviceinterface="com.ouc.service.BranchCompanyService"ref="braComService"/>
16 */
17  
18 public class BranchCompanySerImplimplements BranchCompanyService{
19   
20    @Resource  BranchCompanyMapperbranchCompanyMapper;
21  
22    @Override
23    public List<BranchCompany> getBranchCompanys()
24    {
25       return branchCompanyMapper.getBraComsFromDataBase();
26    }
27   
28    @Override
29    public int saveBranchCompany(BranchCompanybranchCompany)
30    {
31       return branchCompanyMapper.insertBranchCompany(branchCompany);
32    }
33 }
View Code

    (4)数据库操作接口dao类:BranchCompanyMapper

 1 package com.ouc.dao;
 2  
 3 importjava.util.List;
 4 importcom.ouc.model.BranchCompany;
 5  
 6 public interface BranchCompanyMapper {
 7   
 8       int insertBranchCompany(BranchCompanybranchCompany);
 9       
10       public List<BranchCompany> getBraComsFromDataBase();
11 }
View Code

    (5)数据库SQL语句XML:BranchCompanyMapper.Xml

 1 <?xmlversion="1.0"encoding="UTF-8"?>
 2 <!DOCTYPEmapperPUBLIC "-//mybatis.org//DTDMapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mappernamespace="com.ouc.dao.BranchCompanyMapper">
 4  
 5 <!--
 6 <resultMapid="BraComResultMap" type="com.ouc.model.BranchCompany">
 7       <id column="id"property="id" jdbcType="INTEGER" />
 8       <result column="name"property="name" jdbcType="VARCHAR" />
 9       <result column="address"property="address" jdbcType="VARCHAR" />
10       <result column="telNumber"property="telNumber" jdbcType="VARCHAR" />
11       <result column="moduleNo"property="moduleNo" jdbcType="VARCHAR" />
12       <result column="labId"property="labId" jdbcType="INTEGER" />
13       <result column="remark"property="remark" jdbcType="VARCHAR" />
14 </resultMap>
15 -->
16  
17 <cachetype="org.mybatis.caches.ehcache.LoggingEhcache"> 
18     <propertyname="timeToIdleSeconds"value="3600"/>
19     <propertyname="timeToLiveSeconds"value="3600"/>
20     <propertyname="maxEntriesLocalHeap"value="1000"/> 
21     <propertyname="maxEntriesLocalDisk"value="10000000"/> 
22     <propertyname="memoryStoreEvictionPolicy"value="LRU"/> 
23 </cache>
24  
25 <insertid="insertBranchCompany"parameterType="com.ouc.model.BranchCompany"useGeneratedKeys="true"keyProperty="id"flushCache="true">
26       insert into BranchCompany (
27       id,name,address,telNumber,moduleNo,labId,remark)
28       values (
29       #{id},
30       #{name},
31       #{address},
32       #{telNumber},
33       #{moduleNo},
34       #{labId},
35       #{remark}
36       )
37    </insert>
38   
39    <selectid="getBraComsFromDataBase"resultType="com.ouc.model.BranchCompany">
40       select * from BranchCompany
41    </select>
42 </mapper> 
View Code

   (6)运行控制类:BranchCompanyController

 1 package com.ouc.controller;
 2  
 3 importjavax.annotation.Resource;
 4 importjava.util.List;
 5 importorg.springframework.stereotype.Controller;
 6 importorg.springframework.web.bind.annotation.RequestMapping;
 7 importorg.springframework.web.bind.annotation.ResponseBody;
 8  
 9 importcom.ouc.service.BranchCompanyService;
10 importcom.ouc.model.BranchCompany;
11  
12 @Controller 
13 @RequestMapping("/BranchCompanyController")
14 public classBranchCompanyController {
15    @Resource
16    private BranchCompanyService braComService;
17    
18     @RequestMapping(value ="/BraCom_view")
19     public @ResponseBody ObjectGetBranchCompany(){
20       List<BranchCompany>braComs=braComService.getBranchCompanys();
21       returnbraComs;
22     }
23 }
View Code

   17.测试类:Provider

 1 package com.ouc.test;
 2  
 3 importorg.springframework.context.support.ClassPathXmlApplicationContext;
 4 importcom.ouc.model.*;
 5 import com.ouc.service.*;
 6  
 7 public classProvider {
 8    private static BranchCompanyServicebraComService;
 9   
10    public static void main(String[] args) throwsException {
11      
12       ClassPathXmlApplicationContext cxt = newClassPathXmlApplicationContext(
13            newString[]{"classpath:spring.xml","classpath:spring-mybatis.xml","classpath:spring-dubbo.xml"},true);
14      
15       cxt.start();
16       /*braComService =(BranchCompanyService)cxt.getBean("braComService");
17      
18       BranchCompany braCom = newBranchCompany();
19       braCom.setName("Hai");
20       braCom.setId(1);
21       braCom.setLabId(2);
22       braCom.setAddress("SSdsg");
23       braCom.setModuleNo("SSS2000");
24       braCom.setTelNumber("125604");
25       braCom.setRemark(" ");
26       braComService.saveBranchCompany(braCom);
27       */
28    }
29 }
View Code

    运行 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

posted @ 2015-09-06 18:46  火雨(Nick)  阅读(605)  评论(0编辑  收藏  举报