SpringBoot 结合 MyBatis-Plus 增删改查操作

今天用eclipse打了个SpringBoot结合MyBatis-Plus的增删改查,折腾了一天多时间,中间出现的问题记录一下,也可以帮大家作为参考:

我用的数据库是MySql,表是Users表:

创建SpringBoot项目,创建实体类、mapper、service以及实现类、controller、jsp、application配置文件:

我数据库里的主键是自增的,这里的主键一定要加上 @TableId ,配上其类型。我今天就是由于没加这个注解导致只能查询和删除,不能添加和修改的问题,整了好久,最后问了一个大神才知道是这里除了问题,在此向大神表示感谢!

这里要注意:别忘了添加 @Mapper 注解和继承 MyBatis-Plus 封装的 BaseMapper<T> ,加上泛型

启动类很简单,没有就一个 @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>

猜你喜欢

转载自blog.csdn.net/qq_42423072/article/details/82858735