当前位置:网站首页>22-07-04 Xi'an Shanghao housing project experience summary (01)

22-07-04 Xi'an Shanghao housing project experience summary (01)

2022-07-05 09:20:00 £ small feather

Shanghao house is a second-hand house management service platform , Open up high-quality resources and online capabilities , Aggregate online and offline second-hand real estate resources , Create an all-round second-hand housing service ecological market , Provide consumers with high-quality real estate service resources .

Understand the project business according to the demonstration

Shanghao room backstage management system : Authority management system

Front end of Shanghao house : Find Fangwang


Surprised : Extract common modules

BaseController, It's really simple , But considering everyone in the future Controller You need to use the two lines of encapsulated code ,

In addition , After modification and addition, you will get . It's for consumers Controller inherited .

public class BaseController {
    private final static String PAGE_SUCCESS = "common/successPage";
    public String successPage(Model model, String successMessage){
        model.addAttribute("messagePage",successMessage);
        return PAGE_SUCCESS;
    }
}

 BaseService It is used for middlemen “service-api” To inherit ,

  Its contents are as follows , Inheriting it is equivalent to Service There are five general methods of adding, deleting, modifying and checking

public interface BaseService<T> {
    void insert(T t);

    T getById(Long id);

    /**
     *  Logical deletion 
     * @param id
     */
    void delete(Long id);

    /**
     *  Modify the information 
     * @param t
     */
    void update(T t);

    /**
     *  Paging query information 
     * @param filters
     * @return
     */
    PageInfo<T> findPage(Map<String, Object> filters);
}

BaseServiceImpl and BaseMapper Let the service provider use inheritance . Write specific business logic , A write operation on the database .

here BaseServiceImpl Is an abstract class , There is an abstract method in it getEntityMapper(), This method is to allow subclasses that inherit it to override it , Really mapper Interface to him , He can help you call methods

public abstract class BaseServiceImpl<T> {

    /**
     *  This abstract method is used to obtain the real mapper Interface 
     * @return
     */
    protected abstract BaseMapper<T> getEntityMapper();
    
    public void insert(T t) {
        getEntityMapper().insert(t);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public T getById(Long id) {
        return getEntityMapper().getById(id);
    }


    public void delete(Long id) {
        getEntityMapper().delete(id);
    }


    public void update(T t) {
        getEntityMapper().update(t);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public PageInfo<T> findPage(Map<String, Object> filters) {
        // take pageSize and pageNum Strong conversion int type 
        // The second parameter indicates the default value if the forced conversion fails 
        int pageNum = CastUtil.castInt(filters.get("pageNum"),1);
        int pageSize = CastUtil.castInt(filters.get("pageSize"),10);
        // Open paging 
        PageHelper.startPage(pageNum,pageSize);
        // Call the methods of the persistence layer to query the data set 
        // Encapsulation return result , The teacher didn't pass the second parameter here 
        return new PageInfo<>(getEntityMapper().findPage(filters),10);
    }
}

Finally, this BaseMapper 了 , Every one we create mapper Interfaces should inherit this interface , So our mapper Interface can omit the five most commonly used methods

public interface BaseMapper<T> {

    /**
     *  Save an entity 
     * @param t
     */
    void insert(T t);

    /**
     *  Through a sign ID  Get a unique entity 
     * @param id
     * @return
     */
    T getById(Long id);

    /**
     * modify 
     * @param t
     */
    void update(T t);

    /**
     *  Delete 
     * @param id
     */
    void delete(Long id);

    /**
     *  Paging query 
     * @param filters
     * @return
     */
    Page<T> findPage(Map<String, Object> filters);
}

surprised 2、 Dictionaries

The dictionary itself is cleverly designed

The visual effect of this page is also very nice: This tree like directory looks very advanced , In fact, what the back end returns to the front end is only one json Array .

java Back end code :

The back-end logic is very simple , According to the id To query all child nodes , Use the found data Stream Flow to finish .

2021/11/12 Beijing stream flow , Inner class ,lambda expression _£ Little feather's blog -CSDN Blog

    public List<Map<String, Object>> findZnodes(Long id) {
        //1.  Call persistence layer methods , According to the parent node id Inquire about List<Dict>
        List<Dict> dictList = dictMapper.findListByParentId(id);
        // Use Stream flow 
        List<Map<String, Object>> znodes = dictList.stream()
                .map(dict -> {
                    Map<String, Object> znode = new HashMap<>();
                    // Go to znode In the store id
                    znode.put("id", dict.getId());
                    // Go to znode In the store name
                    znode.put("name", dict.getName());
                    // Go to znode In the store isParent
                    znode.put("isParent", dictMapper.countIsParent(dict.getId()) > 0);
                    return znode;
                })
                .collect(Collectors.toList());
        return znodes;
    }

  stay SQlyog The following is executed in sql:

--  According to the id To query all child nodes 
SELECT * FROM hse_dict WHERE parent_id =1


Click all categories

Observe network, Sent such a request , That's the kind of situation we analyzed above  

http://139.198.152.148:8001/dict/findZnodes?id=1

Back to the front end json data : In fact, the return is still json Array .

{
    "code": 200,
    "data": [
        {
            "isParent": true,
            "name": " House type ",
            "id": 10000
        },
        {
            "isParent": true,
            "name": " floor ",
            "id": 20000
        },
        {
            "isParent": true,
            "name": " Building structure ",
            "id": 30000
        },
        {
            "isParent": true,
            "name": " Decoration ",
            "id": 40000
        },
        {
            "isParent": true,
            "name": " toward ",
            "id": 50000
        },
        {
            "isParent": true,
            "name": " The purpose of the house is ",
            "id": 60000
        },
        {
            "isParent": true,
            "name": " province ",
            "id": 100000
        }
    ],
    "message": " success ",
    "ok": true
}

Let's click on the house type , Then send a request to query

http://139.198.152.148:8001/dict/findZnodes?id=10000

  Back to the front end json The array is as follows :

{
    "code": 200,
    "data": [
        {
            "isParent": false,
            "name": " A room ",
            "id": 10001
        },
        {
            "isParent": false,
            "name": " Two rooms ",
            "id": 10002
        },
        {
            "isParent": false,
            "name": " Three rooms ",
            "id": 10003
        },
        {
            "isParent": false,
            "name": " Four rooms ",
            "id": 10004
        },
        {
            "isParent": false,
            "name": " More than four rooms ",
            "id": 10005
        }
    ],
    "message": " success ",
    "ok": true
}

Two uses of enumeration :

usage 1: Listing release status HouseStatus

public enum HouseStatus {
    // Unpublished means that users cannot see , But the background management system can see 
    PUBLISHED(1," The published "), UNPUBLISHED(0," Unpublished ");
    public int code;
    public String message;

    HouseStatus(int code, String message) {
        this.code = code;
        this.message = message;
    }
}

Use when adding listing information :

    @PostMapping("/save")
    public String save(House house,Model model){
        // Unpublished 
        house.setStatus(HouseStatus.UNPUBLISHED.code);
        houseService.insert(house);
        return successPage(model," Add housing information successfully ");
    }

Enumeration usage 2: Define as private variable .

public enum DictCode {
    HOUSETYPE("houseType"),FLOOR("floor"),BUILDSTRUCTURE("buildStructure"),
    DECORATION("decoration"),DIRECTION("direction"),HOUSEUSE("houseUse");
    private String message;

    DictCode(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

Use :

//3.  Query various initialization lists : List of house types 、 Floor list 、 Decoration list ....
List<Dict> houseTypeList =
dictService.findDictListByParentDictCode(DictCode.HOUSETYPE.getMessage());

getListingDateString() House class

stay show.html in

<dt> Listing time :</dt><dd th:text="${house.listingDateString}"></dd>
<dt> Last deal :</dt><dd th:text="${house.lastTradeDateString}"></dd>

But actually House These two properties are not in this class , Only the following two Date attribute , So how to display the data on the page

  • Listing date private Date listingDate;
  • Last transaction date private Date lastTradeDate;

  An important knowledge point , stay thymeleaf In the syntax , In the request domain get Method to get value . So it's actually House This class has two more methods . Wonderful , Maggie wonderful house

	/**
	 *  Get the listing date in string type 
	 * @return
	 */
	public String getListingDateString() {
		Date date = this.getListingDate();
		if(null == date) {
			return "";
		}
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = df.format(date);
		return dateString;
	}


	/**
	 *  Get the last transaction date in string 
	 * @return
	 */
	public String getLastTradeDateString() {
		Date date = this.getLastTradeDate();
		if(null == date) {
			return "";
		}
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = df.format(date);
		return dateString;
	}

原网站

版权声明
本文为[£ small feather]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050909519861.html