mybatis-plus整合mybatis
二、使用mybatis-plus整合mybatis,完成如下要求
1、对数据库表的增加
2、对数据库表按给定id删除
3、对数据库表的全部显示
在pom.xml里引入苞米豆的mybatis-plus依赖,引入spring-core,lombok,log4j,junit等需要用到的依赖,在末尾resource声明要编译的配置文件类型和路径.properties和.xml都要编译
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>mybatis_plus</artifactId> <groupId>com.xzit</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>day4_mybatis_1</artifactId> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.15</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>2.0.0-alpha7</version> </dependency> <!--<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency>--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
先来一个db.properties配置文件存数据库配置信息
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8 jdbc.username=root jdbc.password=zengyu1234
再来个SqlMapConfig.xml,引用db.properties里的信息,在末尾声明mapper的包的位置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.xzit.mapper"/> </mappers> </configuration>
从网上拷了一个log4j.properties配置文件用来在控制台打印日志,不然mybatis-plus不打印日志
参考文章:https://blog.csdn.net/qq_15092079/article/details/105269634
### ??### log4j.rootLogger = debug,stdout ### ???????? ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
新建一个Teacher实体类
package com.xzit.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Teacher { private Integer id; private String name; private String addr; private Integer age; private String job; private Integer sal; }
重点来了,TeacherMapper接口类要extends继承BaseMapper类,并且在泛型那里填上你要做增删改查的实体类<Teacher>,这样的话你接口里不用写方法,基本的增删改查方法在BaseMapper里已经用反射机制写好了
package com.xzit.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.xzit.entity.Teacher; import java.util.List; public interface TeacherMapper extends BaseMapper<Teacher> { // List<Teacher> selectList(); }
此时TeacherMapper.xml可以是空的啥也不写
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xzit.mapper.TeacherMapper"> </mapper>
最后用junit的机制在test包里新建一个测试类进行测试,测试方法上使用@Test注解
运行结果:
增加功能:
删除功能
查询全部功能