Spring/springmvc/mybatis (persistence layer, business layer, control layer thinking summary)

Source: Internet
Author: User

preparatory work:# # 7 Importing provincial and municipal data into the database

1. Download the SQL script file from FTP

2. Move the script file to a location that is easy to describe the absolute path

3. Go to MySQL Console

4. Using the ' Tedu_store ' database

5. Run ' source E:\t_dict.sql ' to execute the script file (the command under Linux is source/home/soft01/Desktop/t_dict.sql;)-------------------------------- -----------------------------------------------------------

# # get provincial and municipal data

# # Target

Get a list of all provinces

Get the province's information according to the province's code name

Get a list of all municipalities in a province

According to the city code, get the city information

Get a list of all areas of a city

According to the code of the district, get the information of the District------------------------------------------------------------------------------------------

### Create entity classes (analyze front-end pages and write corresponding entity classes according to the fields of the tables in the database)

Create 3 entity classes in the ' Cn.tedu.store.entity ' package:
   Public classProvince {PrivateInteger ID; PrivateString Code; PrivateString name; //construction method, set/get,tostring, implement serializable    }     Public classCity {PrivateInteger ID; PrivateString Provincecode; PrivateString Code; PrivateString name; //construction method, set/get,tostring, implement serializable    }     Public classArea {PrivateInteger ID; PrivateString Citycode; PrivateString Code; PrivateString name; //construction method, set/get,tostring, implement serializable}

--------------------------------------------------------------------------------------------------------------- -------

### Persistence Layer

Create the ' Cn.tedu.store.mapper.ProvinceMapper ' interface and add an abstract method:
/**      * Get a list of all     provinces */Lists <Province> getprovincelist (    );      /**      * According to the province code, obtain the province     's information *    /province Getprovincebycode (String provincecode);

Then, create (copy and paste and modify) the ' provincemapper.xml ' mapping file under ' resources\mappers\ ':
<Mappernamespace= "Cn.tedu.store.mapper.ProvinceMapper">                <!--get a list of all provinces -        <!--list<province> getprovincelist () -        <SelectID= "Getprovincelist"Resulttype= "Cn.tedu.store.entity.Province">SELECT ID, province_code as code, province_name as name From T_dict_provinces</Select>        <!--get the province's information according to the province's code name -        <!--province Getprovincebycode (String provincecode) -        <SelectID= "Getprovincebycode"Resulttype= "Cn.tedu.store.entity.Province">SELECT ID, province_code as code, province_name as name From T_dict_provinces WHERE Province_code=#{provincecode}</Select>    </Mapper>    

### Business LayerTake the information for the province as an example, first create the corresponding business interface ' Cn.tedu.store.service.IProvinceService ' and add the same abstract method as the persistence layer interface:
/**      * Get a list of all     provinces */Lists <Province> getprovincelist (    );      /**      * According to the province code, obtain the province     's information *    /province Getprovincebycode (String provincecode);

Create ' Cn.tedu.store.service.ProvinceServiceImpl ' to implement the above interface and use ' @Service (' provinceservice ') ' annotations, then declare ' @Autowired in the class Provincemapper provincemapper; ' property, and then implement the abstract method in the interface:    
 Public List<province> getprovincelist () {        return  provincemapper.getprovincelist ();    }      Public province Getprovincebycode (String provincecode) {        return  Provincemapper.getprovincebycode (Provincecode);    }

### Controller Layer

Get a list of provinces

Request path: '/province/list.do ' request parameter: ' None ' request type: ' GET ' response: ' responseresult<list<province>> '

Get the province's information according to the province's code name

Request path: '/province/info.do ' request parameter: ' code=110000 ' request type: ' GET ' response: ' responseresult<province> '

Get a list of municipalities in a province

Request path: '/city/list.do ' request parameter: ' province_code=xx ' request type: ' GET ' response: ' responseresult<list<city>> '

...... ......

(In fact, the general idea is the idea of MVC, but there are a lot of details to pay attention to, to practice more.) )

-------------------------------------------------------------------------------------------

###the difference between a POST request and a GET request( Face TestGet is often used for address bar requests
Post request form submission, JSON application ( look at the previous code), generally Add method body

The amount of data submitted for a GET request is small
Post request data is generally unlimited

Get exposes a lot of information, such as ID, so it's less secure than a POST request.

Get requests are easy to bookmark because it is specific to a page, and post is not ( find a code to see)。

Spring/springmvc/mybatis (persistence layer, business layer, control layer thinking summary)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.