• Dubbo+zookeeper实现单表的增删改查


    1、数据库准备

    建表语句

    CREATE TABLE `tb_brand` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) DEFAULT NULL COMMENT '品牌名称',
      `first_char` varchar(1) DEFAULT NULL COMMENT '品牌首字母',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8;
    
    /*Data for the table `tb_brand` */
    
    insert  into `tb_brand`(`id`,`name`,`first_char`) values (1,'联想','L'),(2,'华为','H'),(3,'三星','S'),(4,'小米','X'),(5,'OPPO','O'),(6,'360','S'),(7,'中兴','Z'),(8,'魅族','M'),(9,'苹果','P'),(10,'VIVO','V'),(11,'诺基亚','N'),(12,'锤子','C'),(13,'长虹','C'),(14,'海尔','H'),(15,'飞利浦','F'),(16,'TCL','T'),(17,'海信','H'),(18,'夏普','X'),(19,'创维','C'),(20,'东芝','D'),(21,'康佳','K'),(22,'LG','L'),(25,'','a'),(26,'背背佳','B'),(29,'好的2','H'),(30,'测试','T'),(31,'网易','W');

    2、创建Java项目

    第一步、创建一个空的Java项目,并创建父工程dz_parent

    第二步、创建子模块

    dz_dao——》jar工程

    dz_manager_web——》war工程

    dz_pojo——》jar工程

    dz_sellergoods_interface——》jar工程,用来维护dubbo+zookeeper中的服务消费者和提供者的接口

    dz_sellergoods_service——》war工程

    第三步、在父工程pom.xml中添加所需的jar包依赖和插件

    <!-- 集中定义依赖版本号 -->
        <properties>
            <junit.version>4.12</junit.version>
            <spring.version>4.2.4.RELEASE</spring.version>
            <pagehelper.version>4.0.0</pagehelper.version>
            <servlet-api.version>2.5</servlet-api.version>
            <dubbo.version>2.8.4</dubbo.version>
            <zookeeper.version>3.4.6</zookeeper.version>
            <zkclient.version>0.1</zkclient.version>
            <mybatis.version>3.2.8</mybatis.version>
            <mybatis.spring.version>1.2.2</mybatis.spring.version>
            <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
            <mysql.version>5.1.32</mysql.version>
            <druid.version>1.0.9</druid.version>
        </properties>
    
    
        <dependencies>
    
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jms</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- dubbo相关 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient.version}</version>
            </dependency>
            <dependency>
                <groupId>javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.11.0.GA</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.28</version>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.10</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>
            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis.spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.miemiedev</groupId>
                <artifactId>mybatis-paginator</artifactId>
                <version>${mybatis.paginator.version}</version>
            </dependency>
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- 连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
    
        </dependencies>

     maven工程插件

    <build>
            <plugins>
                <!-- java编译插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
            </plugins>
    
            <!-- 使用Maven部署的时候,xml和properties配置文件也一起部署到Tomcat -->
            <!--tomcat启动的时候Maven工程默认把src/main/resources的*.properties;*.xml文件加载到Tomcat中-->
            <!--会有中情况:Mapper接口类和Mapper映射文件同包同名src/main/java 下 -->
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                        <include>**/*.conf</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <!-- 默认是以下配置 -->
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                        <include>**/*.conf</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
    
        </build>

    第四步、在各个模块的pom.xml配置文件中添加模块之间的依赖

    依赖关系如下

    dao依赖pojo

    interface依赖pojo

    service依赖interface

    web依赖service

     第五步、创建pojo实体类

    此处使用mybatis的逆向工程生成所需的类及其接口和映射文件

    5.1 创建一个maven模块,命名为mybatis_generator,导入如下pom依赖和插件

    <dependencies>
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-core</artifactId>
                <version>1.3.7</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.5</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.7</version>
                    <configuration>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </build>

     5.2 在mybatis_generator模块的resources目录下编写逆向工程配置文件generatorConfig.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration
            PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
            "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    <generatorConfiguration>
        <!-- classPathEntry:数据库的 JDBC驱动的jar 包地址 -->
        <classPathEntry
                location="F:\mysql-connector-java-5.1.32.jar" />
        <context id="default" targetRuntime="MyBatis3">
    
            <!-- 配置生成pojo的序列化的插件,mybatis支持很多插件,这些插件都在 org.mybatis.generator.plugins包下  -->
            <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
    
            <commentGenerator>
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="false" />
            </commentGenerator>
            <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://127.0.0.1:3306/pinyougoudb" userId="root"
                            password="root">
            </jdbcConnection>
    
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- targetProject:生成 POJO 类的位置 -->
            <javaModelGenerator targetPackage="com.alibaba.pojo" targetProject="F:\coding\dubboandzookeeper\dz_parent\dz_pojo/src/main/java">
                <!-- 是否对model添加 构造函数 -->
                <property name="constructorBased" value="true"/>
                <!-- enableSubPackages:是否让schema 作为包的后缀,是否允许子包,即targetPackage.schemaName.tableName -->
                <property name="enableSubPackages" value="false"/>
                <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
                <property name="immutable" value="false"/>
                <!-- 是否对类CHAR类型的列的数据进行trim操作,从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
    
            <!-- 生成 XML文件 -->
            <sqlMapGenerator targetPackage="com.alibaba.dao.mappper"      targetProject="F:\coding\dubboandzookeeper\dz_parent\dz_dao/src/main/resources">
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
            <!--生成接口-->
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.alibaba.dao.mapper"  targetProject="F:\coding\dubboandzookeeper\dz_parent\dz_dao/src/main/java">
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
    
    
            <!-- 指定数据库表 -->
            <table  tableName="tb_brand" domainObjectName="TbBrand" mapperName="TbBrandMapper"
                    enableCountByExample="false" enableDeleteByExample="false"
                    enableSelectByExample="true" enableUpdateByExample="false"/>
        </context>
    </generatorConfiguration>

     5.3 点击maven的mybatis的逆向工程生成插件的generate命令生成文件到配置的指定位置

     第六步、编写接口

  • 相关阅读:
    插入与归并(python)(原创)
    同一环境下新建Standby RAC库
    配置Server Side TAF
    RAC禁用DRM特性
    Oracle获取数据库中的对象创建语句
    SQL Tuning 基础概述10
    Oracle数据块损坏的恢复实例
    Oracle的FIXED
    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part2:clusterware安装和升级
    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级
  • 原文地址:https://www.cnblogs.com/kitor/p/11112251.html
Copyright © 2020-2023  润新知