1 Mybatis框架的核心接口测试

张创琦 2022.03.08

1 数据库部分

建表

  1. -- ----------------------------
  2. -- Table structure for team
  3. -- ----------------------------
  4. DROP TABLE IF EXISTS `team`;
  5. CREATE TABLE `team` (
  6. `teamId` int NOT NULL AUTO_INCREMENT COMMENT '球队ID',
  7. `teamName` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '球队名称',
  8. `location` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '球队位置',
  9. `createTime` date NULL DEFAULT NULL COMMENT '球队建立时间',
  10. PRIMARY KEY (`teamId`) USING BTREE
  11. ) ENGINE = InnoDB AUTO_INCREMENT = 1113 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;
  12. -- ----------------------------
  13. -- Records of team
  14. -- ----------------------------
  15. INSERT INTO `team` VALUES (1001, '76人', '宾夕法尼亚州费城', '1967-01-01');
  16. INSERT INTO `team` VALUES (1002, '篮网', '纽约州布鲁克林', '1967-01-01');
  17. INSERT INTO `team` VALUES (1003, '雄鹿', '威斯康星州密尔沃基', '1968-01-01');
  18. INSERT INTO `team` VALUES (1004, '黄蜂', '夏洛特', '2004-01-01');
  19. INSERT INTO `team` VALUES (1005, '老鹰', '乔治亚州亚特兰大市', '1949-01-01');
  20. INSERT INTO `team` VALUES (1006, '热火', '佛罗里达州迈阿密', '1988-01-01');
  21. INSERT INTO `team` VALUES (1007, '凯尔特人', '马赛诸萨州波士顿', '1946-01-01');
  22. INSERT INTO `team` VALUES (1008, '尼克斯', '纽约州纽约市', '1976-01-01');
  23. INSERT INTO `team` VALUES (1009, '步行者', '印第安纳波利斯', '1967-01-01');
  24. INSERT INTO `team` VALUES (1010, '公牛', '伊利诺斯州芝加哥', '1966-01-01');
  25. INSERT INTO `team` VALUES (1011, '猛龙', '加拿大安大略省多伦多', '1995-01-01');
  26. INSERT INTO `team` VALUES (1012, '骑士', '俄亥俄州克里夫兰', '1970-01-01');
  27. INSERT INTO `team` VALUES (1013, '奇才', '华盛顿D.C', '1961-01-01');
  28. INSERT INTO `team` VALUES (1014, '魔术', '佛罗里达州奥兰多', '1989-01-01');
  29. INSERT INTO `team` VALUES (1015, '活塞', '密歇根州底特律', '1948-01-01');
  30. INSERT INTO `team` VALUES (1016, '爵士', '犹他州盐湖城', '1974-01-01');
  31. INSERT INTO `team` VALUES (1017, '太阳', '亚利桑那州菲尼克斯', '1968-01-01');
  32. INSERT INTO `team` VALUES (1018, '快船', '洛杉矶', '1970-01-01');
  33. INSERT INTO `team` VALUES (1019, '掘金', '科罗拉多州丹佛', '1976-01-01');
  34. INSERT INTO `team` VALUES (1020, '湖人', '加利福尼亚州洛杉矶', '1948-01-01');
  35. INSERT INTO `team` VALUES (1021, '开拓者', '俄勒冈州波特兰', '1970-01-01');
  36. INSERT INTO `team` VALUES (1022, '独行侠', '达拉斯', '1980-01-01');
  37. INSERT INTO `team` VALUES (1023, '灰熊', '田纳西州孟菲斯', '1995-01-01');
  38. INSERT INTO `team` VALUES (1024, '马刺', '德克萨斯州圣安东尼奥', '1976-01-01');
  39. INSERT INTO `team` VALUES (1025, '勇士', '加州奥克兰', '1946-01-01');
  40. INSERT INTO `team` VALUES (1026, '鹈鹕', '新奥尔良', '1988-01-01');
  41. INSERT INTO `team` VALUES (1027, '国王', '加州萨科拉门托', '1948-01-01');
  42. INSERT INTO `team` VALUES (1028, '雷霆', '俄克拉荷马州俄克拉荷马城', '1967-01-01');
  43. INSERT INTO `team` VALUES (1029, '火箭', '德克萨斯州休斯顿', '1967-01-01');
  44. INSERT INTO `team` VALUES (1030, '森林狼', '明尼苏达州明尼阿波利斯', '1989-01-01');
  45. INSERT INTO `team` VALUES (1047, 'lina的球队', '洛杉矶', '2021-04-14');
  46. INSERT INTO `team` VALUES (1053, 'lina的球队', '洛杉矶', '2021-04-16');
  47. INSERT INTO `team` VALUES (1054, '开课吧球队1', '洛杉矶', '2021-04-16');

2 pom.xml配置相关依赖

  1. <dependencies>
  2. <!-- 保证会出现 Spring Config 选项,以在 resources 文件下面建立 xml 文件 -->
  3. <dependency>
  4. <groupId>org.springframework</groupId>
  5. <artifactId>spring-context</artifactId>
  6. <version>5.2.13.RELEASE</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.mybatis</groupId>
  10. <artifactId>mybatis</artifactId>
  11. <version>3.5.6</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>mysql</groupId>
  15. <artifactId>mysql-connector-java</artifactId>
  16. <version>8.0.16</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>4.12</version>
  22. <scope>test</scope>
  23. </dependency>
  24. </dependencies>
  25. <build>
  26. <!-- 步骤 7 -->
  27. <!-- 配置映射文件的扫描路径 -->
  28. <resources>
  29. <resource>
  30. <directory>src/main/java</directory>
  31. <includes>
  32. <include>**/*.properties</include>
  33. <include>**/*.xml</include>
  34. </includes>
  35. <filtering>false</filtering>
  36. </resource>
  37. </resources>
  38. <plugins>
  39. <plugin>
  40. <artifactId>maven-compiler-plugin</artifactId>
  41. <version>3.1</version>
  42. <configuration>
  43. <source>1.8</source>
  44. <target>1.8</target>
  45. </configuration>
  46. </plugin>
  47. </plugins>
  48. </build>

3 编写 mybatis 的配置文件

resources/mybatis.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <!-- mybatis全局配置文件:节点的顺序参考:按住ctrl+configuration节点,进入文件观看 -->
  6. <configuration>
  7. <environments default="development">
  8. <environment id="development">
  9. <transactionManager type="JDBC"> </transactionManager>
  10. <dataSource type="POOLED">
  11. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  12. <property name="url" value="jdbc:mysql://127.0.0.1:3306/ssm1? useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT"/>
  13. <property name="username" value="root"/>
  14. <property name="password" value="123456"/>
  15. </dataSource>
  16. </environment>
  17. </environments>
  18. // 步骤 6
  19. <!-- 注册映射文件 -->
  20. <mappers>
  21. <mapper resource="com/kkb/pojo/Team.xml"/>
  22. </mappers>
  23. </configuration>

4 编写 pojo.Team 实体类

  1. package com.kkb.pojo;
  2. import java.util.Date;
  3. public class Team {
  4. private Integer teamId;
  5. private String teamName;
  6. private String location;
  7. private Date createTime;
  8. @Override
  9. public String toString() {
  10. return "Team{" +
  11. "teamId=" + teamId +
  12. ", teamName='" + teamName + '\'' +
  13. ", location='" + location + '\'' +
  14. ", createTime=" + createTime +
  15. '}';
  16. }
  17. public Integer getTeamId() {
  18. return teamId;
  19. }
  20. public void setTeamId(Integer teamId) {
  21. this.teamId = teamId;
  22. }
  23. public String getTeamName() {
  24. return teamName;
  25. }
  26. public void setTeamName(String teamName) {
  27. this.teamName = teamName;
  28. }
  29. public String getLocation() {
  30. return location;
  31. }
  32. public void setLocation(String location) {
  33. this.location = location;
  34. }
  35. public Date getCreateTime() {
  36. return createTime;
  37. }
  38. public void setCreateTime(Date createTime) {
  39. this.createTime = createTime;
  40. }
  41. }

5 编写ORM映射文件

resources/com/kkb/pojo/Team.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.kkb.pojo.Team">
  6. <select id="queryAll" resultType="com.kkb.pojo.Team">
  7. select * from team;
  8. </select>
  9. </mapper>

6 在mybatis.xml文件中注册映射文件

见上。

7 在pom.xml中配置映射文件的扫描路径

见上。

8 编写测试代码

/test/java/com.kkb.test/TestTeam.java

  1. package com.kkb.test;
  2. import com.kkb.pojo.Team;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import org.junit.Test;
  8. import java.io.IOException;
  9. import java.io.Reader;
  10. import java.util.List;
  11. public class TestTeam {
  12. //mybatis的配置文件--相当于创建工厂的图纸
  13. private String resource = "mybatis.xml";
  14. @Test
  15. public void testFindAll(){
  16. try{
  17. //1、读取mybatis的配置文件, 注意导包不要导错了
  18. Reader reader = Resources.getResourceAsReader(resource);
  19. //2、创建SqlSessionFactory对象,目的是获取sqlSession--根据图纸创建工厂
  20. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  21. //3、创建可以执行SQL语句的SqlSession--工厂创建产品
  22. SqlSession sqlSession = sqlSessionFactory.openSession();
  23. //4、执行SQL语句
  24. List<Team> teamList = sqlSession.selectList("com.kkb.pojo.Team.queryAll");
  25. //5、循环输出查询的结果
  26. for(Team team: teamList) {
  27. System.out.println(team);
  28. }
  29. //6、关闭SqlSession,释放资源
  30. sqlSession.close();
  31. } catch(IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }

9 测试类运行结果

  1. Team{teamId=1001, teamName='76人', location='宾夕法尼亚州费城', createTime=Sun Jan 01 08:00:00 CST 1967}
  2. Team{teamId=1002, teamName='篮网', location='纽约州布鲁克林', createTime=Sun Jan 01 08:00:00 CST 1967}
  3. Team{teamId=1003, teamName='雄鹿', location='威斯康星州密尔沃基', createTime=Mon Jan 01 08:00:00 CST 1968}
  4. Team{teamId=1004, teamName='黄蜂', location='夏洛特', createTime=Thu Jan 01 08:00:00 CST 2004}
  5. Team{teamId=1005, teamName='老鹰', location='乔治亚州亚特兰大市', createTime=Sat Jan 01 08:00:00 CST 1949}
  6. Team{teamId=1006, teamName='热火', location='佛罗里达州迈阿密', createTime=Fri Jan 01 08:00:00 CST 1988}
  7. Team{teamId=1007, teamName='凯尔特人', location='马赛诸萨州波士顿', createTime=Tue Jan 01 08:00:00 CST 1946}
  8. Team{teamId=1008, teamName='尼克斯', location='纽约州纽约市', createTime=Thu Jan 01 08:00:00 CST 1976}
  9. Team{teamId=1009, teamName='步行者', location='印第安纳波利斯', createTime=Sun Jan 01 08:00:00 CST 1967}
  10. Team{teamId=1010, teamName='公牛', location='伊利诺斯州芝加哥', createTime=Sat Jan 01 08:00:00 CST 1966}
  11. Team{teamId=1011, teamName='猛龙', location='加拿大安大略省多伦多', createTime=Sun Jan 01 08:00:00 CST 1995}
  12. Team{teamId=1012, teamName='骑士', location='俄亥俄州克里夫兰', createTime=Thu Jan 01 08:00:00 CST 1970}
  13. Team{teamId=1013, teamName='奇才', location='华盛顿D.C', createTime=Sun Jan 01 08:00:00 CST 1961}
  14. Team{teamId=1014, teamName='魔术', location='佛罗里达州奥兰多', createTime=Sun Jan 01 08:00:00 CST 1989}
  15. Team{teamId=1015, teamName='活塞', location='密歇根州底特律', createTime=Thu Jan 01 08:00:00 CST 1948}
  16. Team{teamId=1016, teamName='爵士', location='犹他州盐湖城', createTime=Tue Jan 01 08:00:00 CST 1974}
  17. Team{teamId=1017, teamName='太阳', location='亚利桑那州菲尼克斯', createTime=Mon Jan 01 08:00:00 CST 1968}
  18. Team{teamId=1018, teamName='快船', location='洛杉矶', createTime=Thu Jan 01 08:00:00 CST 1970}
  19. Team{teamId=1019, teamName='掘金', location='科罗拉多州丹佛', createTime=Thu Jan 01 08:00:00 CST 1976}
  20. Team{teamId=1020, teamName='湖人', location='加利福尼亚州洛杉矶', createTime=Thu Jan 01 08:00:00 CST 1948}
  21. Team{teamId=1021, teamName='开拓者', location='俄勒冈州波特兰', createTime=Thu Jan 01 08:00:00 CST 1970}
  22. Team{teamId=1022, teamName='独行侠', location='达拉斯', createTime=Tue Jan 01 08:00:00 CST 1980}
  23. Team{teamId=1023, teamName='灰熊', location='田纳西州孟菲斯', createTime=Sun Jan 01 08:00:00 CST 1995}
  24. Team{teamId=1024, teamName='马刺', location='德克萨斯州圣安东尼奥', createTime=Thu Jan 01 08:00:00 CST 1976}
  25. Team{teamId=1025, teamName='勇士', location='加州奥克兰', createTime=Tue Jan 01 08:00:00 CST 1946}
  26. Team{teamId=1026, teamName='鹈鹕', location='新奥尔良', createTime=Fri Jan 01 08:00:00 CST 1988}
  27. Team{teamId=1027, teamName='国王', location='加州萨科拉门托', createTime=Thu Jan 01 08:00:00 CST 1948}
  28. Team{teamId=1028, teamName='雷霆', location='俄克拉荷马州俄克拉荷马城', createTime=Sun Jan 01 08:00:00 CST 1967}
  29. Team{teamId=1029, teamName='火箭', location='德克萨斯州休斯顿', createTime=Sun Jan 01 08:00:00 CST 1967}
  30. Team{teamId=1030, teamName='森林狼', location='明尼苏达州明尼阿波利斯', createTime=Sun Jan 01 08:00:00 CST 1989}
  31. Team{teamId=1047, teamName='lina的球队', location='洛杉矶', createTime=Wed Apr 14 08:00:00 CST 2021}
  32. Team{teamId=1053, teamName='lina的球队', location='洛杉矶', createTime=Fri Apr 16 08:00:00 CST 2021}
  33. Team{teamId=1054, teamName='开课吧球队1', location='洛杉矶', createTime=Fri Apr 16 08:00:00 CST 2021}