Complete a complete summary of a springboot project-------II

Source: Internet
Author: User
Tags manual writing using git

We go on to continue with the Springboot project.

I. Swagger2
Interface description, testing whether each interface is valid

1. Add Dependent Pom.xml
Close the Spring-boot project before editing pom.xml
Eclipse Edit Pom.xml. Eclipse will help us automatically download dependencies when the edits are complete and saved pom.xml. There may be some problems with this automated operation of Eclipse.

MVN Install

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>

2. Add a configuration file

Webconfig.java

Package com.briup.apps.poll.config;

Import org.springframework.context.annotation.Configuration;
Import Org.springframework.web.servlet.config.annotation.CorsRegistry;
Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class Webconfig implements Webmvcconfigurer {
@Override
public void Addcorsmappings (Corsregistry registry) {
Registry.addmapping ("/**"). Allowedorigins ("*"). Allowedmethods ("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"). Allowcredentials (True). MaxAge (3600);
}
}

Swagger2.java

/**
* Project Name:poll
* File Name:Swagger2.java
* Package Name:com.briup.apps.poll.config
* date:2018 June 10 6:22:51
* Copyright (c) 2018, [email protected] All rights Reserved.
*
*/

Package com.briup.apps.poll.config;

Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

Import Springfox.documentation.builders.ApiInfoBuilder;
Import springfox.documentation.builders.PathSelectors;
Import springfox.documentation.builders.RequestHandlerSelectors;
Import Springfox.documentation.service.ApiInfo;
Import Springfox.documentation.spi.DocumentationType;
Import Springfox.documentation.spring.web.plugins.Docket;
Import Springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
* Classname:swagger2 <br/>
* Function:todo ADD Function. <br/>
* Reason:todo ADD Reason. <br/>
* date:2018 June 10 pm 6:22:51 <br/>
* @author Lichunyu
* @version
* @since JDK 1.6
* @see
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
Public Docket Createrestapi () {
return new Docket (documentationtype.swagger_2)
. Apiinfo (Apiinfo ())
. Select ()
. APIs (Requesthandlerselectors.basepackage ("Com.briup.apps.poll.web"))
. Paths (Pathselectors.any ())
. build ();
}

Private Apiinfo Apiinfo () {
return new Apiinfobuilder ()
. Title ("YQG, tuning system API")
. Description ("Google search engine-please FQ enter, http://www.google.com")
. Termsofserviceurl ("http://www.google.com")
. Version ("1.0")
. build ();
}
}

Then enter http://localhost:8080/swagger-ui.html in the browser and you will be able to access your interface and test

Add:

Package Com.briup.apps.poll.config;import Org.mybatis.spring.annotation.mapperscan;import Org.springframework.context.annotation.Configuration, @Configuration @mapperscan ("  Com.briup.apps.poll.dao")publicclass  mybatisconfig {    }

The purpose of this profile Mybatiesconfig.java is to be able to automatically find the mapper in the DAO layer

Two. If you are a good programmer or want to be a good programmer, GitHub is the tool you must use

Using Git on Eclipse to upload and download local and remote repository code, first you need a GitHub account, create a warehouse with your project name, and then open windows->preperences-> on eclipse Git->configuation, choose Reposity Settings, select the project you want to upload, will automatically give you the location of a GitHub configuration file, click Open, configure

[Core] symlinks=falserepositoryformatversion=0FileMode=falselogallrefupdates=true[Remote"Origin"] URL = https://github.com/qingguoyan/poll3.git Fetch = +refs/heads/*: Refs/remotes/origin/*[branch "master"] Remote = origin merge = Refs/heads/master

Note: The URL is the GitHub warehouse address for your project, save after configuration is complete, right-click on the Project->team->share project, click Team->commit again, select your unstaged file down to staged, Enter a commit message and submit your code to the local repository before committing to the remote repository.

Submit to Remote repository: Team->removeto->push, fill in the remote warehouse address you want to upload, next, select Master,add,next, enter two upload password, complete and then refresh your warehouse on GitHub, Found your warehouse has your project, and done!

Three. Use MyBatis generator to build the database access layer, but only for operations on single table

Three ways to write data access layer code
1. Manual writing (style is not uniform, time is longer, redundancy is too high)
2. Package Yourself
Basedao
3. Mybatis Generator (best!)
Automatically generate Pojo based on table, map interface, map file
MyBatis swagger, plug-in (with one time)
1) Add plugins in Pom.xml
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
</plugin>
2) Add configuration file
1. According to which tables are generated
2. Where did the Pojo come from?
3. Where to put the generated mapper interface
4. Where to put the generated mapper files
5. How to Generate
...

<?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"><!--Mybatis-generator's core configuration file--><generatorconfiguration> <classpathentry location="C:\briup\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/> <context id="Db2tables"Targetruntime="MyBatis3"> <jdbcconnection driverclass="Com.mysql.jdbc.Driver"Connectionurl="jdbc:mysql://127.0.0.1:3306/poll2.0"userId="Root"Password="Root"> </jdbcConnection> <!--Specifies that the generated type is Java type and avoids type fields such as number in database <javatyperesolver > &LT;PR Operty name="forcebigdecimals"Value="false"/> </javaTypeResolver> <!--automatically generated entity storage package path--<javamodelgenerator targetpackage="Com.briup.apps.poll.bean"targetproject="./src/main/java"> <property name="enablesubpackages"Value="true"/> <property name="trimstrings"Value="true"/> </javaModelGenerator> <!--automatically generated *mapper.xml file storage path--<sqlmapgenerator targetpackage="Mapper"targetproject="./src/main/resources"> <property name="enablesubpackages"Value="true"/> </sqlMapGenerator> <!--automatically generated *mapper.java storage path--<javaclientgenerator type="Xmlmapper"Targetpackage="Com.briup.apps.poll.dao"targetproject="./src/main/java"> <property name="enablesubpackages"Value="true"/> </javaClientGenerator> <!--map configuration--<table tablename="Poll_school"Domainobjectname="School"></table> <table tablename="Poll_grade"Domainobjectname="Grade"></table> <table tablename="Poll_clazz"Domainobjectname="Clazz"></table> <table tablename="Poll_course"Domainobjectname="Course"></table> <table tablename="Poll_user"Domainobjectname="User"></table> <table tablename="poll_options"Domainobjectname="Options"></table> <table tablename="poll_question"Domainobjectname="Question"></table> <table tablename="Poll_questionnaire"Domainobjectname="Questionnaire"></table> <table tablename="POLL_QQ"Domainobjectname="questionnairequestion"></table> <table tablename="Poll_survey"Domainobjectname="Survey"></table> <table tablename="poll_answers"Domainobjectname="Answers"></table> </context></generatorConfiguration>


3) Start working (Close app)
$ mvn-dmybatis.generator.overwrite=true Mybatis-generator:generate

When you're done, you'll find that your project automatically generates Bean,dao,mapper.xml, saving time and making it easy to operate on a single table.

Four. Unified development of the corresponding interface

Background development---"front-end development of the response mode unified, the return status of two
1.{
status:200
Message: ' Success '
Data:[]
}

0.9
status:500
Message: ' Database exception '
Data:null
}

Package Com.briup.apps.poll.util; Public classMsgresponse {PrivateInteger stauts;//Status Code 200 success 500 code exception    PrivateString message;//Error, Success information    PrivateObject data;//data is null         Public Staticmsgresponse Success (String message, Object data) {msgresponse response=NewMsgresponse (); Response.setstauts ( $);        Response.setmessage (message);        Response.setdata (data); returnresponse; }         Public Staticmsgresponse error (String message) {msgresponse response=NewMsgresponse (); Response.setstauts ( -);        Response.setmessage (message); Response.setdata (NULL); returnresponse; }             PublicInteger getstauts () {returnstauts; }     Public voidsetstauts (Integer stauts) { This. stauts =stauts; }     PublicString getMessage () {returnmessage; }     Public voidsetmessage (String message) { This. Message =message; }     PublicObject GetData () {returndata; }     Public voidsetData (Object data) { This. data =data; }}

Complete a complete summary of a springboot project-------II

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.