对 “mybatis - XxxMapper.java接口中方法的参数 和 返回值类型,怎样在 XxxMapper.xml 中配置的问题” 这篇文章做一下优化
这个pom.xml文件,就是上面说的这篇文章的父工程的pom.xml,即:下面这个pom.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"><modelVersion>4.0.0</modelVersion><groupId>com.atguigu</groupId><artifactId>ssm-mybatis-parts</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>mybatis-base-quick</module><module>mybatis-base-param-input</module><module>mybatis-base-result-output</module><module>mybatis-base-crud</module></modules><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- mybatis依赖 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.11</version></dependency><!-- mysql驱动,mybatis底层依赖jdbc驱动实现,本次不需要导入连接池,mybatis自带 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.25</version></dependency><!-- junit5测试 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.3.1</version></dependency>* 主要说说这个 lombok配合lombok插件一起使用,可以使用注解@Data给实体类自动添加setter、getter、toString方法<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency></dependencies></project>
上面说的lombok
maven依赖,要与这个插件一起使用才可以哦!!!
再说一下 测试文件src/test/java/com.atguigu.MybatisTest.java
主要是 @BeforeEach、@AfterEach 这两个注解
package com.atguigu.test;import com.atguigu.mapper.UserMapper;
import com.atguigu.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;import javax.imageio.ImageReader;
import java.io.IOException;
import java.io.InputStream;public class MybatisTest {private SqlSession sqlSession;@BeforeEach // 每次走测试方法之前,先走此初始化方法public void before() throws IOException {InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);sqlSession = sqlSessionFactory.openSession(true); // 开启自动提交}@AfterEach // 每次走测试方法之后,都会调用此方法public void after(){sqlSession.close();}@Testpublic void testInsert() {UserMapper mapper = sqlSession.getMapper(UserMapper.class);User user = new User();user.setUsername("root");user.setPassword("123456");int insert = mapper.insert(user);System.out.println("insert= " + insert);}@Testpublic void testUpdate() {UserMapper mapper = sqlSession.getMapper(UserMapper.class);User user = new User();user.setId(1);user.setUsername("root2");user.setPassword("1234562");int update = mapper.update(user);System.out.println("update= " + update);}@Testpublic void testDelete() {UserMapper mapper = sqlSession.getMapper(UserMapper.class);int delete = mapper.delete(1);System.out.println("delete= " + delete);}@Testpublic void testSelectById() {}@Testpublic void testSelectAll() {}
}
还有 @Data 注解
src/main/java/com.atguigu.pojo/User.java
package com.atguigu.pojo;import lombok.Data;@Data
这个注解可以给实体类自动添加getter、setter、toString方法
public class User {private Integer id;private String username;private String password;
}