In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The content of this article mainly explains "springboot2 combined with mybatis to achieve the function of adding, deleting, changing and searching", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "springboot2 combined with mybatis to achieve the function of adding, deleting, changing and searching"!
1. Scene description
This section combines springboot2, springmvc, mybatis, swagger2, etc., to build a complete add, delete, modify and query project. I hope that through this basic project, we can help friends get started with springboot2 project quickly.
two。 Solution
2.1 create a new springboot project
Create a new springboot project using idea (quick build of springboot project)
(1) new project
(2) gav setting
2.2 Project overall Plan and description 2.2.1 overall Plan
2.2.2 description
The project contains four major contents.
(1) pom.xml
Maven project prerequisites for defining projects, obtaining jar packages, packaging, and so on.
(2) Project configuration file
There are two, one is an in-project configuration file, and the other is used for mybatis-generate to generate related database operation files.
(3) spcrudapplication
Project startup class is a must for springboot project.
(4) springmvc corresponding class.
Contains controller, service, db and other related classes.
2.3 detailed description
2.3.1 pom file
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE com.laowang spcrud 0.0.1-SNAPSHOT spcrud Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java 8.0.15 org.springframework.boot spring-boot-starter-jdbc 2.1.5.RELEASE io.springfox springfox-swagger2 2.7.0 io. Springfox springfox-swagger-ui 2.7.0 org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.0 src/main/resources/ src/main/java * * / * .xml org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.3.5 src/main/resources/generatorConfig.xml true true
Description:
Contains 5 pieces of content
(1) web startup package
(2) Database
(3) swagger
(4) mybatis
(5) packing
2.3.2 Resource files
(1) application.properties
Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ruanjianlaowang?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8spring.datasource.username=rootspring.datasource.password=root
Description: database profile, connection, user name, password
(2) mybatis resource file
Description:
Contains several pieces of content:
(a) the classPathEntry tag defines the jar packet address of the mysql-connector
(B) jdbcConnection database connection information
(C) javaModelGenerator, sqlMapGenerator, and javaClientGenerator define the address where the generated file is stored
(d) table specifically executes the tabel that generates the code, adding a few tags and not generating the example method.
2.3.3 Startup class
Package com.laowang.spcrud;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication@EnableSwagger2@MapperScan ("com.laowang.spcrud.db.mapper") public class SpcrudApplication {public static void main (String [] args) {SpringApplication.run (SpcrudApplication.class, args);}}
Description:
@ SpringBootApplication all springboot project launch prerequisites
@ EnableSwagger2 starts swagger
@ MapperScan loads the mpper file.
2.3.4 springmvc class
(1) TestController
Package com.laowang.spcrud;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication@EnableSwagger2@MapperScan ("com.laowang.spcrud.db.mapper") public class SpcrudApplication {public static void main (String [] args) {SpringApplication.run (SpcrudApplication.class, args);}}
The ctroller class contains four methods for adding, deleting, modifying and querying, using the method of rest request.
(2) TestService
Package com.laowang.spcrud.service;import com.laowang.spcrud.db.entity.TLaowang;import com.laowang.spcrud.db.mapper.TLaowangMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class TestService {@ Autowired private TLaowangMapper tLaowangMapper; / * add * @ auther: software Lao Wang * / public void insertRecord (TLaowang tLaowang) {tLaowangMapper.insert (tLaowang) } / * delete * @ auther: software King * / public void deleteByPrimaryKey (int id) {tLaowangMapper.deleteByPrimaryKey (id);} / * Update * @ auther: software King * / public void updateByPrimaryKeySelective (TLaowang tLaowang) {tLaowangMapper.updateByPrimaryKeySelective (tLaowang);} / * * query * @ auther: software King * / public TLaowang selectByPrimaryKey (int id) {return tLaowangMapper.selectByPrimaryKey (id);}}
TestService class, the service class of addition, deletion, modification and query.
(3) entity class TLaowang
Package com.laowang.spcrud.db.entity;public class TLaowang {private Integer id; private String name; private String password; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name = = null? Null: name.trim ();} public String getPassword () {return password;} public void setPassword (String password) {this.password = password = = null? Null: password.trim ();}}
Operation entity class, which contains three fields: id, name, password
(4) mpper interface class TLaowangMapper
Package com.laowang.spcrud.db.mapper;import com.laowang.spcrud.db.entity.TLaowang;public interface TLaowangMapper {int deleteByPrimaryKey (Integer id); int insert (TLaowang record); int insertSelective (TLaowang record); TLaowang selectByPrimaryKey (Integer id); int updateByPrimaryKeySelective (TLaowang record); int updateByPrimaryKey (TLaowang record);}
(5) mapper interface xml
Id, name, password select from t_laowang where id = # {id,jdbcType=INTEGER} delete from t_laowang where id = # {id,jdbcType=INTEGER} SELECT LAST_INSERT_ID () insert into t_laowang (name, password) values (# {name,jdbcType=VARCHAR}, # {password,jdbcType=VARCHAR}) SELECT LAST_INSERT_ID () insert into t_laowang name, password, # {name,jdbcType=VARCHAR}, # {password,jdbcType=VARCHAR}, update t_laowang name = # {name,jdbcType=VARCHAR} Password = # {password,jdbcType=VARCHAR}, where id = # {id,jdbcType=INTEGER} update t_laowang set name = # {name,jdbcType=VARCHAR}, password = # {password,jdbcType=VARCHAR} where id = # {id,jdbcType=INTEGER}
4 and 5 together, here the use of mybatis automatically generated add, delete, change and search method, no expansion, the real project in addition to these, there will certainly be some extensions, such as query according to name and so on.
2.4 Database creation table statement
CREATE TABLE `t _ laowang` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (50) DEFAULT NULL, `password` varchar (50) DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
At this point, I believe that everyone on the "springboot2 combined with mybatis to achieve add, delete, change and search function" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.