preface
in recent days, I have taken time to add several new functions to the code generator (it is expected to be released tonight). At present, it supports postgresql database, HikariCP connection pool and swagger2 (there is a link to the generator at the end of the article. Friends who like it can download it for free forever. It is recommended that you install the idea plug-in, which is simple and fast), By the way, explain the difference between postgresql and mysql, the difference between HikariCP and Druid, and the use of swagger2. Otherwise, there's nothing to write. Yes, I'm here! Let's have a look.
PostgreSql VS MySql
MySQL is known as the most popular database server in the world. It is small, fast, open-source, free, portable and easy to use. These features make it the database brother of domestic Internet enterprises, but the real reason for its popularity is due to its history, In 2008, MySQL was acquired by SUN company and has been available to everyone for free. At that time, MySQL accumulated thousands of fans, so the enterprise trained a large number of programmers skilled in MySQL. Although PG had many advanced features at that time, most of the Internet could not be used, and PG did not have the features needed by the Internet - fast. The continuation of history has created today's situation. It can be said that the Internet has made mysql.
in the past 10 years, SUN was acquired by Oracle, and MySQL was also taken over by Oracle. Up to now, MySQL is still one of the most popular databases for developers.
compared with MySql, PG may be unfamiliar to you. PG is known as the most advanced database server in the world. One is the most popular and the other is the most advanced. You can enjoy it.
PG is currently maintained by more than 1000 top Internet developers. The update speed is relatively fast, and it is completely open source and free of charge. PG is more like an integrated database. It integrates the advantages of relational database and non relational database, so that you can store data structure flexibly.
in addition, data consistency and integrity are high priorities of PG, which is why many projects in Japan use PG instead of MySQL, which saves a lot of trouble in transaction control, such as common concurrent updates. PG can ensure its correctness, because the hidden column saves the version field, It is equivalent to helping you realize optimistic locking without coding. MySQL must manually lock it. In addition, PG has its irreplaceable advantages in the storage of geographic information, and the external table is also an exciting function. In short, to be fair, PG is much more powerful than MySQL! It is slowly rising. I believe it will maintain a relative balance with MySQL in the future.
in terms of operation mode, PG is process mode and MySQL is thread mode. Generally, process mode can have higher resource utilization in multi CPU environment. Process mode shared data needs shared memory, while thread mode data itself is shared in process space. Different threads need to control the synchronization between threads for data access. Thread mode consumes relatively less resources, so in theory, MySQL supports more connections than PG, but pgpool, an excellent connection pool software, can solve this problem!
finally, one thing to note: neither PostgreSQL nor MySQL can claim to be better than the other. For users, there are only the right ones, not the best.
HikariCP VS Druid
Druid connection pool is an open source database connection pool component of Alibaba. In recent years, KO has lost the old C3P0 and DBCP and become a new favorite of database connection pool. It is adopted by many enterprises because of its simple use, security, fast speed, powerful and rich monitoring features. Druid connection pool is balanced in all aspects, which is enough for most applications. It also gives a monitoring interface, which is also excellent.
however, it should be noted that when our application has defects, the use of Druid connection pool may cause the connection to not be released all the time, which often leads to the exception of unable to obtain database connection. We need to locate the location of the problem through the following configuration:
<property name="removeAbandoned" value="true" /><property name="removeAbandonedTimeout" value="180" /><property name="logAbandoned" value="true" />
after locating the problem and solving it, just comment it out, because it will cost more performance, so use it with caution in the production environment!
as a rising star, HikariCP is known as the fastest database connection pool. It is so fast that even SpringBoot2 adopts it as the default connection pool. The amount of code is only 130kb. As the saying goes, the essence is concentrated.
HikariCP not only blocks, but also has authoritative certification for stability and reliability. It has an excellent reputation. The author optimizes and simplifies the bytecode and uses FastList instead of ArrayList to bring strong performance support.
if you want the ultimate performance, you can consider using HikariCP!
Swagger2
Swagger2 can help us generate the description of interface documents, facilitate the communication between front and rear staff, and improve work efficiency.
the usage of Swagger2 is extremely simple. Here, take the Springboot project as an example. First, we need to introduce pom dependency:
<dependency> <groupId>io.springfoxgroupId> <artifactId>springfox-swagger2artifactId> <version>2.8.0version>dependency><dependency> <groupId>io.springfoxgroupId> <artifactId>springfox-swagger-uiartifactId> <version>2.8.0version>dependency>
then create the corresponding configuration class:
@Configuration//Annotation enable swagger2 function @ enableswagger2public class swaggerconfig {@ bean public docket API() {return new docket (documentationtype. Swagger_2). Apiinfo (apiinfo()) / / whether it is enabled (true enable false hide. It is recommended to hide the production environment). Enable (true). Select()) //Scan the package path. Setting basePackage will take all methods of all @ API marked classes under the package as APIs APIs (requesthandlerselectors. basePackage ("testup. Controller") / / specify the path to process pathselectors Any () represents all paths paths(PathSelectors.any()) . build(); } Private apiinfo apiinfo() {return new apiinfobuilder() / / set the document title (API name). title("Swagger2 interface document") / / document description. description("interface description"). License ("Apache 2.0"). Licenseurl(“ http://www.apache.org/licenses/LICENSE-2.0 ") / / terms of service URL. Termsofserviceurl (" https://swagger.io/ ") / / version number. Version (" 1.0 "). Build();}}
if interceptors are used:
@Configurationpublic class MvcConfig implements WebMvcConfigurer { /** * Interceptor */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //Your business logic..... return true; } }).addPathPatterns("/**").excludePathPatterns("/login", "/register", "/login/doLogin", "/user/register", "/mystatic/**", "/druid/**", "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); }}
if Spring Security is used:
@Overridepublic void configure(WebSecurity web) throws Exception { //allow Swagger URL to be accessed without authentication web. ignoring(). Antmatchers ("/ V2 / API docs", / / swagger API JSON "/ swagger resources / configuration / UI", / / used to obtain supported actions "/ swagger resources", / / used to obtain API docs URI "/ swagger resources / configuration / security", / / security options "/ swagger UI. HTML");}
then add the following annotations to the controller to be accessed, for example (@ Api and @ ApiOperation annotations are used here):
@RestController@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")@Api(tags = "Login interface")@RequestMapping("/login")public class LoginController { @ApiOperation(value = "Sign in") @RequestMapping(value = "/doLogin", method = RequestMethod.POST) public CommonResult doLogin(@RequestBody User user, HttpSession session) { if (("admin".equals(user.getUserName()) && "root".equals(user.getPassword()))) { session.setAttribute("user", user); return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG); } return new CommonResult(ResultConstant.LOGIN_FAIL_CODE, ResultConstant.FAIL_MSG); } @ApiOperation(value = "Log out") @RequestMapping(value = "/doLogOut",method = RequestMethod.POST) public CommonResult doLogOut(HttpSession session) { session.removeAttribute("user"); return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG); }}
finally, add annotations (@ ApiModel and @ ApiModelProperty) to the entity class:
@ApiModelpublic class User implements Serializable { /** * serialVersionUID */ private static final long serialVersionUID = 1L; @ApiModelProperty(value = "user name", name = "userName") private String userName; @ApiModelProperty(value = "password", name = "password") private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
visit http://localhost:ip:port/ Project path / swagger UI html: through this interface, you can also test the interface, which greatly facilitates our development and docking work, and bid farewell to the maintenance of complex documents!
User defined parameter configuration list
the new version of the code generator adds a common parameter configuration interface, where you can select the type of connection pool (Druid and hikaricp are currently supported) and whether to enable Swagger, as follows:More parameter configurations will be added one after another!
postgresql is also easy to use. You only need to switch the database type to postgresql:The code generated by the multi table query module in postgresql mode may have problems because it has not been tested yet...
epilogue
after talking about technology, let's pay attention to our life. Due to the impact of the epidemic, the domestic economy has been greatly impacted. Negative information such as layoffs and unable to find a job has burst out everywhere. We don't have to be nervous about it. After the storm, we will see a rainbow. What we have to do is to constantly improve our technical level, read more books and learn more, Become the core personnel of the industry, in this way, we can be sure of success and remain calm.
I hope that foreign countries can control the development of the epidemic as soon as possible, defeat this cunning virus, wake up every morning and look at the increasing figures, which is really worrying.
finally, I hope everyone will put health first at all times. After all, the body is the capital of the revolution. Without health, everything becomes meaningless.
thank you for watching. See you next time! Write a message
Attachment:
Generator download link:
http://www.zrxlh.top:8088/coreCode/
Source address:
https://gitee.com/zrxjava/codeMan
Previous recommendations:
Although the volume is small, there are many functions! SSM/SpringBoot code generator adds idea plug-in support, which can be installed into idea and used directly!
Comprehensive upgrade of SSM/SpringBoot code generator - add a new front and back-end separation responsive theme and fix several bugs
Is it difficult to build a station? The most complete guide in history - teach you to use WordPress to build your own site!
Related resources: JDBC connection pool hikaricp zip
If you want to follow more dry goods and good articles in real time, scan the following figure: