SpringBoot結合MyBatis-Plus增刪改查操作
阿新 • • 發佈:2018-12-12
今天用eclipse打了個SpringBoot結合MyBatis-Plus的增刪改查,折騰了一天多時間,中間出現的問題記錄一下,也可以幫大家作為參考:
我用的資料庫是MySql,表是Users表:
建立SpringBoot專案,建立實體類、mapper、service以及實現類、controller、jsp、application配置檔案:
我資料庫裡的主鍵是自增的,這裡的主鍵一定要加上 @TableId ,配上其型別。我今天就是由於沒加這個註解導致只能查詢和刪除,不能新增和修改的問題,整了好久,最後問了一個大神才知道是這裡除了問題,在此向大神表示感謝!
這裡要注意:別忘了新增 @Mapper
啟動類很簡單,沒有就一個 @SpringBootApplication 註解,沒有其他的東西
配置埠號、資料庫驅動、檢視字首和字尾名、MyBatis-Plus(我最後兩個配的:一個是mapper檔案位置,一個是實體類包名。如有不對,還請指教)
最後,把我 pom.xml 中的程式碼粘出來以供參考(從 parent 節點到 build 節點):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--SpringBoot核心jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--mybatis plus核心庫--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.3</version> </dependency> <!-- mybatis plus與springboot整合 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency> <!--MyBatis--> <!-- <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--MySql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!--LomBok:一個能減少很多重複程式碼的外掛--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.14.4</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--配置支援jsp--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>8.5.12</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- springboot熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- springboot熱部署 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>