• Java开发全终端实战租房项目-服务的具体实现


    新增房源信息

    数据结构

    搭建虚拟机环境

    #拉取镜像 
    docker pull percona:5.7.23 
    #创建容器 
    docker run --name percona -e MYSQL_ROOT_PASSWORD=root -d -i -p 3306:3306 percona:5.7.23
    #启动容器 
    docker start percona
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    CREATE TABLE `tb_estate` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) DEFAULT NULL COMMENT '楼盘名称',
      `province` varchar(10) DEFAULT NULL COMMENT '所在省',
      `city` varchar(10) DEFAULT NULL COMMENT '所在市',
      `area` varchar(10) DEFAULT NULL COMMENT '所在区',
      `address` varchar(100) DEFAULT NULL COMMENT '具体地址',
      `year` varchar(10) DEFAULT NULL COMMENT '建筑年代',
      `type` varchar(10) DEFAULT NULL COMMENT '建筑类型',
      `property_cost` varchar(10) DEFAULT NULL COMMENT '物业费',
      `property_company` varchar(20) DEFAULT NULL COMMENT '物业公司',
      `developers` varchar(20) DEFAULT NULL COMMENT '开发商',
      `created` datetime DEFAULT NULL COMMENT '创建时间',
      `updated` datetime DEFAULT NULL COMMENT '更新时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1006 DEFAULT CHARSET=utf8 COMMENT='楼盘表';
    
    -- 初始数据
    INSERT INTO `tb_estate` (`id`, `name`, `province`, `city`, `area`, `address`, `year`, `type`, `property_cost`, `property_company`, `developers`, `created`, `updated`) VALUES ('1001', '中远两湾城', '上海市', '上海市', '普陀区', '远景路97弄', '2001', '塔楼/板楼', '1.5', '上海中远物业管理发展有限公司', '上海万业企业股份有限公司', '2018-11-06 23:00:20', '2018-11-06 23:00:23');
    INSERT INTO `tb_estate` (`id`, `name`, `province`, `city`, `area`, `address`, `year`, `type`, `property_cost`, `property_company`, `developers`, `created`, `updated`) VALUES ('1002', '上海康城', '上海市', '上海市', '闵行区', '莘松路958弄', '2001', '塔楼/板楼', '1.5', '盛孚物业', '闵行房地产', '2018-11-06 23:02:30', '2018-11-27 23:02:33');
    INSERT INTO `tb_estate` (`id`, `name`, `province`, `city`, `area`, `address`, `year`, `type`, `property_cost`, `property_company`, `developers`, `created`, `updated`) VALUES ('1003', '保利西子湾', '上海市', '上海市', '松江区', '广富林路1188弄', '2008', '塔楼/板楼', '1.75', '上海保利物业管理', '上海城乾房地产开发有限公司', '2018-11-06 23:04:22', '2018-11-06 23:04:25');
    INSERT INTO `tb_estate` (`id`, `name`, `province`, `city`, `area`, `address`, `year`, `type`, `property_cost`, `property_company`, `developers`, `created`, `updated`) VALUES ('1004', '万科城市花园', '上海市', '上海市', '松江区', '广富林路1188弄', '2002', '塔楼/板楼', '1.5', '上海保利物业管理', '上海城乾房地产开发有限公司', '2018-11-13 16:43:40', '2018-11-13 16:43:42');
    INSERT INTO `tb_estate` (`id`, `name`, `province`, `city`, `area`, `address`, `year`, `type`, `property_cost`, `property_company`, `developers`, `created`, `updated`) VALUES ('1005', '上海阳城', '上海市', '上海市', '闵行区', '罗锦路888弄', '2002', '塔楼/板楼', '1.5', '上海莲阳物业管理有限公司', '上海莲城房地产开发有限公司', '2018-11-06 23:23:52', '2018-11-06 23:23:55');
    
    CREATE TABLE `tb_house_resources` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `title` varchar(100) DEFAULT NULL COMMENT '房源标题',
      `estate_id` bigint(20) DEFAULT NULL COMMENT '楼盘id',
      `building_num` varchar(5) DEFAULT NULL COMMENT '楼号(栋)',
      `building_unit` varchar(5) DEFAULT NULL COMMENT '单元号',
      `building_floor_num` varchar(5) DEFAULT NULL COMMENT '门牌号',
      `rent` int(10) DEFAULT NULL COMMENT '租金',
      `rent_method` tinyint(1) DEFAULT NULL COMMENT '租赁方式,1-整租,2-合租',
      `payment_method` tinyint(1) DEFAULT NULL COMMENT '支付方式,1-付一押一,2-付三押一,3-付六押一,4-年付押一,5-其它',
      `house_type` varchar(255) DEFAULT NULL COMMENT '户型,如:2室1厅1卫',
      `covered_area` varchar(10) DEFAULT NULL COMMENT '建筑面积',
      `use_area` varchar(10) DEFAULT NULL COMMENT '使用面积',
      `floor` varchar(10) DEFAULT NULL COMMENT '楼层,如:8/26',
      `orientation` varchar(2) DEFAULT NULL COMMENT '朝向:东、南、西、北',
      `decoration` tinyint(1) DEFAULT NULL COMMENT '装修,1-精装,2-简装,3-毛坯',
      `facilities` varchar(50) DEFAULT NULL COMMENT '配套设施, 如:1,2,3',
      `pic` varchar(200) DEFAULT NULL COMMENT '图片,最多5张',
      `house_desc` varchar(200) DEFAULT NULL COMMENT '描述',
      `contact` varchar(10) DEFAULT NULL COMMENT '联系人',
      `mobile` varchar(11) DEFAULT NULL COMMENT '手机号',
      `time` tinyint(1) DEFAULT NULL COMMENT '看房时间,1-上午,2-中午,3-下午,4-晚上,5-全天',
      `property_cost` varchar(10) DEFAULT NULL COMMENT '物业费',
      `created` datetime DEFAULT NULL,
      `updated` datetime DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='房源表';
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    创建itcast-haoke-manage-dubbo-server-house-resources工程

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>itcast-haoke-manage-dubbo-serverartifactId>
            <groupId>cn.itcast.haoke.managegroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>itcast-haoke-manage-dubbo-server-house-resourcesartifactId>
        <packaging>pompackaging>
        <modules>
            <module>itcast-haoke-manage-dubbo-server-house-resources-dubbo-interfacemodule>
            <module>itcast-haoke-manage-dubbo-server-house-resources-dubbo-servicemodule>
        modules>
    
        <dependencies>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.0.5version>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.47version>
                <optional>trueoptional>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    创建itcast-haoke-manage-dubbo-server-house-resources-dubbo-interface子工程

    对外提供的sdk包
    只提供pojo实体以及接口,不提供实现类

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>itcast-haoke-manage-dubbo-server-house-resourcesartifactId>
            <groupId>cn.itcast.haoke.managegroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>itcast-haoke-manage-dubbo-server-house-resources-dubbo-interfaceartifactId>
    
    
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    创建itcast-haoke-manage-dubbo-server-house-resources-dubbo-service

    具体实现

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>itcast-haoke-manage-dubbo-server-house-resourcesartifactId>
            <groupId>cn.itcast.haoke.managegroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>itcast-haoke-manage-dubbo-server-house-resources-dubbo-serviceartifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
            <dependency>
                <groupId>cn.itcast.haoke.managegroupId>
                <artifactId>itcast-haoke-manage-dubbo-server-house-resources-dubbo-interfaceartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    编写BasePojo文件

    编写BasePojo,所有的pojo类都要继承该类,在该类中定义了created、updated字段,表明在每一个表中都需要有这2个字段。

    package cn.itcast.haoke.dubbo.server.pojo;
    
    import lombok.Data;
    
    import java.util.Date;
    
    @Data
    public abstract class BasePojo implements java.io.Serializable {
    
        private Date created;
        private Date updated;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    使用MybatisPlus的AutoGenerator插件生成代码文件

    创建itcast-haoke-manage-dubbo-server-generator工程

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>itcast-haoke-manage-dubbo-serverartifactId>
            <groupId>cn.itcast.haoke.managegroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>itcast-haoke-manage-dubbo-server-generatorartifactId>
    
        <dependencies>
            
            <dependency>
                <groupId>org.freemarkergroupId>
                <artifactId>freemarkerartifactId>
                <version>2.3.28version>
            dependency>
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.0.5version>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.47version>
                <optional>trueoptional>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    编写CodeGenerator

    package cn.itcast.haoke.generator;
    
    import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    import com.baomidou.mybatisplus.core.toolkit.StringPool;
    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.InjectionConfig;
    import com.baomidou.mybatisplus.generator.config.*;
    import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    import org.apache.commons.lang3.StringUtils;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class CodeGenerator {
    
        /**
         * 

    * 读取控制台内容 *

    */
    public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("itcast"); gc.setOpen(false); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://192.168.58.136:3306/haoke?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模块名")); pc.setParent("cn.itcast.haoke.dubbo.server"); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定义输入文件名称 return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setSuperEntityClass("cn.itcast.haoke.dubbo.server.pojo.BasePojo"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController"); strategy.setInclude(scanner("表名")); strategy.setSuperEntityColumns("id"); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    运行代码,控制台会询问
    请输入模块名:
    houseResources
    请输入表名:
    tb_house_resources
    显示结果
    在这里插入图片描述
    可以看到,自动生成了controller、entity、mapper、service等内容。
    其实,一般只需要entity就可以了,也就是pojo类。(其他生成的代码都没用)
    生成的类是这样的:
    在这里插入图片描述

    package cn.itcast.haoke.dubbo.server.houseResources.entity;
    
    import cn.itcast.haoke.dubbo.server.pojo.BasePojo;
    import java.time.LocalDateTime;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    /**
     * 

    * 房源表 *

    * * @author itcast * @since 2022-07-29 */
    @Data @EqualsAndHashCode(callSuper = true) @Accessors(chain = true) public class TbHouseResources extends BasePojo { private static final long serialVersionUID = 1L; /** * 房源标题 */ private String title; /** * 楼盘id */ private Long estateId; /** * 楼号(栋) */ private String buildingNum; /** * 单元号 */ private String buildingUnit; /** * 门牌号 */ private String buildingFloorNum; /** * 租金 */ private Integer rent; /** * 租赁方式,1-整租,2-合租 */ private Boolean rentMethod; /** * 支付方式,1-付一押一,2-付三押一,3-付六押一,4-年付押一,5-其它 */ private Boolean paymentMethod; /** * 户型,如:2室1厅1卫 */ private String houseType; /** * 建筑面积 */ private String coveredArea; /** * 使用面积 */ private String useArea; /** * 楼层,如:8/26 */ private String floor; /** * 朝向:东、南、西、北 */ private String orientation; /** * 装修,1-精装,2-简装,3-毛坯 */ private Boolean decoration; /** * 配套设施, 如:1,2,3 */ private String facilities; /** * 图片,最多5张 */ private String pic; /** * 描述 */ private String houseDesc; /** * 联系人 */ private String contact; /** * 手机号 */ private String mobile; /** * 看房时间,1-上午,2-中午,3-下午,4-晚上,5-全天 */ private Boolean time; /** * 物业费 */ private String propertyCost; private LocalDateTime created; private LocalDateTime updated; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    @EqualsAndHashCode(callSuper = true)
    这个是自动生成equals和hashCode方法,我们一般不需要,所以将该注解去掉
    @Accessors(chain = true)
    这个是表示,生成的set方法将采用链式编程方式,建议保留

    什么是链式编程?
    在这里插入图片描述
    将整理到pojo文件并且拷贝到工程
    将整理的pojo文件拷贝到itcast-haoke-manage-dubbo-server-house-resources-dubbo-interface工程中。
    在这里插入图片描述

    package cn.itcast.haoke.dubbo.server.pojo;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    import java.time.LocalDateTime;
    
    /**
     * 

    * 房源表 *

    * * @author itcast */
    @Data @Accessors(chain = true) @TableName("tb_house_resources") public class HouseResources extends BasePojo { private static final long serialVersionUID = 779152022777511825L; @TableId(value = "id", type = IdType.AUTO) private Long id; /** * 房源标题 */ private String title; /** * 楼盘id */ private Long estateId; /** * 楼号(栋) */ private String buildingNum; /** * 单元号 */ private String buildingUnit; /** * 门牌号 */ private String buildingFloorNum; /** * 租金 */ private Integer rent; /** * 租赁方式,1-整租,2-合租 */ private Integer rentMethod; /** * 支付方式,1-付一押一,2-付三押一,3-付六押一,4-年付押一,5-其它 */ private Integer paymentMethod; /** * 户型,如:2室1厅1卫 */ private String houseType; /** * 建筑面积 */ private String coveredArea; /** * 使用面积 */ private String useArea; /** * 楼层,如:8/26 */ private String floor; /** * 朝向:东、南、西、北 */ private String orientation; /** * 装修,1-精装,2-简装,3-毛坯 */ private Integer decoration; /** * 配套设施, 如:1,2,3 */ private String facilities; /** * 图片,最多5张 */ private String pic; /** * 描述 */ private String houseDesc; /** * 联系人 */ private String contact; /** * 手机号 */ private String mobile; /** * 看房时间,1-上午,2-中午,3-下午,4-晚上,5-全天 */ private Integer time; /** * 物业费 */ private String propertyCost; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136

    定义新增房源的dubbo服务

    在itcast-haoke-manage-dubbo-server-house-resources-dubbo-interface工程

    package cn.itcast.haoke.dubbo.server.api;
    
    import cn.itcast.haoke.dubbo.server.pojo.HouseResources;
    
    public interface ApiHouseResourcesService {
    
        /**
         * 新增房源
         *
         * @param houseResources
         *
         * @return -1:输入的参数不符合要求,0:数据插入数据库失败,1:成功
         */
        int saveHouseResources(HouseResources houseResources);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    实现新增房源服务

    在itcast-haoke-manage-dubbo-server-house-resources-dubbo-service工程,编写application.properties配置文件

    # Spring boot application
    spring.application.name = itcast-haoke-manage-dubbo-server-house-resources
    
    # 数据库
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://192.168.58.136:3306/haoke?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=root
    
    # 服务的扫描包
    dubbo.scan.basePackages = cn.itcast.haoke.dubbo.server.api
    
    # 应用名称
    dubbo.application.name = dubbo-provider-house-resources
    
    # 协议以及端口
    dubbo.protocol.name = dubbo
    dubbo.protocol.port = 20880
    
    # zk注册中心
    dubbo.registry.address = zookeeper://192.168.58.136:2181
    dubbo.registry.client = zkclient
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    编写HouseResourcesMapper接口

    package cn.itcast.haoke.dubbo.server.mapper;
    
    import cn.itcast.haoke.dubbo.server.pojo.HouseResources;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    
    public interface HouseResourcesMapper extends BaseMapper<HouseResources> {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    编写MybatisPlus配置类

    package cn.itcast.haoke.dubbo.server.config;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Configuration;
    
    @MapperScan("cn.itcast.haoke.dubbo.server.mapper")
    @Configuration
    public class MybatisConfig {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    编写service
    这里编写的Service是Spring的service,不是dubbo服务,因为需要控制事务以及一些逻辑。
    先定义接口:

    package cn.itcast.haoke.dubbo.server.service;
    
    import cn.itcast.haoke.dubbo.server.pojo.HouseResources;
    
    public interface HouseResourcesService {
    
        /**
         * @param houseResources
         *
         * @return -1:输入的参数不符合要求,0:数据插入数据库失败,1:成功
         */
        int saveHouseResources(HouseResources houseResources);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    编写BaseService实现

    package cn.itcast.haoke.dubbo.server.service.impl;
    
    import cn.itcast.haoke.dubbo.server.pojo.BasePojo;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import java.util.Date;
    import java.util.List;
    
    public abstract class BaseServiceImpl<T extends BasePojo> {
    
        @Autowired
        private BaseMapper<T> mapper;
    
        /**
         * 根据id查询数据
         *
         * @param id
         * @return
         */
        public T queryById(Long id) {
            return this.mapper.selectById(id);
        }
    
        /**
         * 查询所有数据
         *
         * @return
         */
        public List<T> queryAll() {
            return this.mapper.selectList(null);
        }
    
        /**
         * 根据条件查询一条数据
         *
         * @param record
         * @return
         */
        public T queryOne(T record) {
            return this.mapper.selectOne(new QueryWrapper<>(record));
        }
    
        /**
         * 根据条件查询数据列表
         *
         * @param record
         * @return
         */
        public List<T> queryListByWhere(T record) {
            return this.mapper.selectList(new QueryWrapper<>(record));
        }
    
        /**
         * 根据条件分页查询数据列表
         *
         * @param record
         * @param page
         * @param rows
         * @return
         */
        public IPage<T> queryPageListByWhere(T record, Integer page, Integer rows) {
            // 获取分页数据
            return this.mapper.selectPage(new Page<T>(page, rows), new QueryWrapper<>
                    (record));
        }
    
        /**
         * 保存数据
         *
         * @param record
         * @return
         */
        public Integer save(T record) {
            record.setCreated(new Date());
            record.setUpdated(record.getCreated());
            return this.mapper.insert(record);
        }
    
        /**
         * 更新数据
         *
         * @param record
         * @return
         */
        public Integer update(T record) {
            record.setUpdated(new Date());
            return this.mapper.updateById(record);
        }
    
        /**
         * 根据id删除数据
         *
         * @param id
         * @return
         */
        public Integer deleteById(Long id) {
            return this.mapper.deleteById(id);
        }
    
        /**
         * 根据ids批量删除数据
         *
         * @param ids
         * @return
         */
        public Integer deleteByIds(List<Long> ids) {
            return this.mapper.deleteBatchIds(ids);
        }
    
        /**
         * 根据条件删除数据
         *
         * @param record
         * @return
         */
        public Integer deleteByWhere(T record) {
            return this.mapper.delete(new QueryWrapper<>(record));
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125

    具体实现类:

    package cn.itcast.haoke.dubbo.server.service.impl;
    
    import cn.itcast.haoke.dubbo.server.pojo.HouseResources;
    import cn.itcast.haoke.dubbo.server.service.HouseResourcesService;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    @Transactional
    @Service
    public class HouseResourcesServiceImpl extends BaseServiceImpl implements HouseResourcesService {
    
        /**
         * @param houseResources
         *
         * @return -1:输入的参数不符合要求,0:数据插入数据库失败,1:成功
         */
        @Override
        public int saveHouseResources(HouseResources houseResources) {
    
            // 添加校验或者是其他的一些逻辑
    
            if(StringUtils.isBlank(houseResources.getTitle())){
                // 不符合要求
                return -1;
            }
    
            return super.save(houseResources);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    编写启动类

    package cn.itcast.haoke.dubbo.server;
    
    import org.springframework.boot.WebApplicationType;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    
    @SpringBootApplication
    public class DubboProvider {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(DubboProvider.class)
                    .web(WebApplicationType.NONE) // 非 Web 应用
                    .run(args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    实现restful接口

    itcast-haoke-manage-api-server工程是,为前端系统提供接口服务,是dubbo服务的消费方
    添加依赖
    因为是dubbo的服务方,需要添加dubbo提供方提供的接口、pojo的依赖

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>itcast-haoke-manageartifactId>
            <groupId>cn.itcast.haoke.managegroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>itcast-haoke-manage-api-serverartifactId>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>cn.itcast.haoke.managegroupId>
                <artifactId>itcast-haoke-manage-dubbo-server-house-resources-dubbo-interfaceartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
    
        dependencies>
    
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    编写service
    HouseResourcesService用于调用dubbo服务。

    package cn.itcast.haoke.dubbo.api.service;
    
    import cn.itcast.haoke.dubbo.server.api.ApiHouseResourcesService;
    import cn.itcast.haoke.dubbo.server.pojo.HouseResources;
    import com.alibaba.dubbo.config.annotation.Reference;
    import org.springframework.stereotype.Service;
    
    @Service
    public class HouseResourcesService {
    
        @Reference(version = "1.0.0")
        private ApiHouseResourcesService apiHouseResourcesService;
    
        public boolean save(HouseResources houseResources) {
            int result =
                    this.apiHouseResourcesService.saveHouseResources(houseResources);
            return result == 1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    编写controller

    package cn.itcast.haoke.dubbo.api.controller;
    
    import cn.itcast.haoke.dubbo.api.service.HouseResourcesService;
    import cn.itcast.haoke.dubbo.server.pojo.HouseResources;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    @Controller
    @RequestMapping("house/resources")
    public class HouseResourcesController {
    
        @Autowired
        private HouseResourcesService houseResourcesService;
    
        /**
         * 新增房源
         *
         * @param houseResources json数据
         * @return
         */
        @PostMapping
        @ResponseBody
        public ResponseEntity<Void> save(@RequestBody HouseResources houseResources) {
            try {
                boolean bool = this.houseResourcesService.save(houseResources);
                if (bool) {
                    return ResponseEntity.status(HttpStatus.CREATED).build();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    
        /**
         * test
         *
         * @return
         */
        @GetMapping
        @ResponseBody
        public ResponseEntity<String> get() {
            return ResponseEntity.ok("ok");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    编写启动类

    package cn.itcast.haoke.dubbo.api;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DubboApiApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboApiApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    编写application.properties配置文件

    # Spring boot application
    spring.application.name = itcast-haoke-manage-api-server
    server.port = 18080
    #logging.level.root=DEBUG
    
    # 应用名称
    dubbo.application.name = dubbo-consumer-haoke-manage
    
    #  zk注册中心
    dubbo.registry.address = zookeeper://172.16.55.185:2181
    dubbo.registry.client = zkclient
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述
    运行启动服务
    运行即可启动tomcat服务,进行测试:
    url:http://127.0.0.1:18080/house/resources
    数据:

    {"title":"东方曼哈顿 3室2厅 16000元","buildingNum":"2","buildingUnit":"1","buildingFloorNum":"1","rent":"1111","paymentMethod":"1","rentMethod":"1","coveredArea":"2","useArea":"2","orientation":"南","decoration":"1","facilities":"1,2,3,8,9","houseDesc":"这个经纪人很懒,没写核心卖点","contact":"张三","mobile":"11111111111","time":"1","propertyCost":"11","floor":"1/2","houseType":"1室1厅1卫1厨1阳台","estateId":"1005"}
    
    • 1

    在这里插入图片描述
    在这里插入图片描述

    整合前端系统

    增加房源标题
    之前的前端页面src/pages/haoke/House/AddResource.js实现中,缺少了标题一项,现补上

    <FormItem {...formItemLayout} label="房源标题">
                              {getFieldDecorator('title',{rules:[{ required: true, message:"此项为必填项" }]})(<Input style={{ width: '100%' }}  />)}
                            </FormItem>
    
    • 1
    • 2
    • 3

    增加model
    src/pages/haoke/House/models/form.js

    import { routerRedux } from 'dva/router';
    import { message } from 'antd';
    import { addHouseResource } from '@/services/haoke';
    
    export default {
      namespace: 'house',
    
      state: {
    
      },
    
      effects: {
        *submitHouseForm({ payload }, { call }) {
          yield call(addHouseResource, payload);
          message.success('提交成功');
        }
      },
    
      reducers: {
        saveStepFormData(state, { payload }) {
          return {
            ...state
          };
        },
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    增加services
    src/services/haoke.js,用于请求服务并且处理业务逻辑。

    import request from '@/utils/request';
    
    export async function addHouseResource(params) {
      return request('/haoke/house/resources', {
        method: 'POST',
        body: params
      });
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    修改表单提交地址
    AddResource.js

    dispatch({
                        type: 'house/submitHouseForm',
                        payload: values,
                    });
    
    • 1
    • 2
    • 3
    • 4

    通过反向代理解决跨域问题
    由于我们前端系统和后台服务系统的端口不同,会导致跨域问题,我们通过umi提供的反向代理功能解决这个问题。在config.js中进行配置proxy:

     proxy: {
        '/haoke/': {
          target: 'http://127.0.0.1:18080',
          changeOrigin: true,
          pathRewrite: { '^/haoke/': '' }
        }
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    配置代理后,以/haoke开头的请求,就会被代理。
    代理效果是这样的:
    请求:http://localhost:8000/haoke/house/resources
    实际:http://127.0.0.1:18080/house/resources

    在这里插入图片描述
    测试
    在这里插入图片描述
    在这里插入图片描述

    源代码

    后端项目
    链接:https://pan.baidu.com/s/1guR2Wc-mDZ-AVDE4Az-T-A?pwd=5gn1
    提取码:5gn1
    –来自百度网盘超级会员V3的分享
    链接:https://pan.baidu.com/s/17OA8GSbMJz1GcBueufGjFQ?pwd=g0tf
    提取码:g0tf
    –来自百度网盘超级会员V3的分享

  • 相关阅读:
    2.1 C++面向对象编程_访问控制和继承
    Actor model 的理解与 protoactor-go 的分析
    什么是回流和重绘?
    微擎源码 志汇同城微圈小程序10.8.0开源版
    CSS垂直居中的方法
    【Vue面试题十一】、Vue组件之间的通信方式都有哪些?
    面向5G-Advanced演进的移动性增强技术
    OpenHarmony命令行工具hdc_std使用总结
    史上最全ubuntu18.04安装教程|搜狗输入法配置教程|网络配置|相关命令配置
    刷题记录(NC20313 [SDOI2008]仪仗队)
  • 原文地址:https://blog.csdn.net/peacezhi/article/details/126054266