1 Mybatis框架的核心接口测试
张创琦 2022.03.08
1 数据库部分
建表
-- ----------------------------
-- Table structure for team
-- ----------------------------
DROP TABLE IF EXISTS `team`;
CREATE TABLE `team` (
`teamId` int NOT NULL AUTO_INCREMENT COMMENT '球队ID',
`teamName` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '球队名称',
`location` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '球队位置',
`createTime` date NULL DEFAULT NULL COMMENT '球队建立时间',
PRIMARY KEY (`teamId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1113 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of team
-- ----------------------------
INSERT INTO `team` VALUES (1001, '76人', '宾夕法尼亚州费城', '1967-01-01');
INSERT INTO `team` VALUES (1002, '篮网', '纽约州布鲁克林', '1967-01-01');
INSERT INTO `team` VALUES (1003, '雄鹿', '威斯康星州密尔沃基', '1968-01-01');
INSERT INTO `team` VALUES (1004, '黄蜂', '夏洛特', '2004-01-01');
INSERT INTO `team` VALUES (1005, '老鹰', '乔治亚州亚特兰大市', '1949-01-01');
INSERT INTO `team` VALUES (1006, '热火', '佛罗里达州迈阿密', '1988-01-01');
INSERT INTO `team` VALUES (1007, '凯尔特人', '马赛诸萨州波士顿', '1946-01-01');
INSERT INTO `team` VALUES (1008, '尼克斯', '纽约州纽约市', '1976-01-01');
INSERT INTO `team` VALUES (1009, '步行者', '印第安纳波利斯', '1967-01-01');
INSERT INTO `team` VALUES (1010, '公牛', '伊利诺斯州芝加哥', '1966-01-01');
INSERT INTO `team` VALUES (1011, '猛龙', '加拿大安大略省多伦多', '1995-01-01');
INSERT INTO `team` VALUES (1012, '骑士', '俄亥俄州克里夫兰', '1970-01-01');
INSERT INTO `team` VALUES (1013, '奇才', '华盛顿D.C', '1961-01-01');
INSERT INTO `team` VALUES (1014, '魔术', '佛罗里达州奥兰多', '1989-01-01');
INSERT INTO `team` VALUES (1015, '活塞', '密歇根州底特律', '1948-01-01');
INSERT INTO `team` VALUES (1016, '爵士', '犹他州盐湖城', '1974-01-01');
INSERT INTO `team` VALUES (1017, '太阳', '亚利桑那州菲尼克斯', '1968-01-01');
INSERT INTO `team` VALUES (1018, '快船', '洛杉矶', '1970-01-01');
INSERT INTO `team` VALUES (1019, '掘金', '科罗拉多州丹佛', '1976-01-01');
INSERT INTO `team` VALUES (1020, '湖人', '加利福尼亚州洛杉矶', '1948-01-01');
INSERT INTO `team` VALUES (1021, '开拓者', '俄勒冈州波特兰', '1970-01-01');
INSERT INTO `team` VALUES (1022, '独行侠', '达拉斯', '1980-01-01');
INSERT INTO `team` VALUES (1023, '灰熊', '田纳西州孟菲斯', '1995-01-01');
INSERT INTO `team` VALUES (1024, '马刺', '德克萨斯州圣安东尼奥', '1976-01-01');
INSERT INTO `team` VALUES (1025, '勇士', '加州奥克兰', '1946-01-01');
INSERT INTO `team` VALUES (1026, '鹈鹕', '新奥尔良', '1988-01-01');
INSERT INTO `team` VALUES (1027, '国王', '加州萨科拉门托', '1948-01-01');
INSERT INTO `team` VALUES (1028, '雷霆', '俄克拉荷马州俄克拉荷马城', '1967-01-01');
INSERT INTO `team` VALUES (1029, '火箭', '德克萨斯州休斯顿', '1967-01-01');
INSERT INTO `team` VALUES (1030, '森林狼', '明尼苏达州明尼阿波利斯', '1989-01-01');
INSERT INTO `team` VALUES (1047, 'lina的球队', '洛杉矶', '2021-04-14');
INSERT INTO `team` VALUES (1053, 'lina的球队', '洛杉矶', '2021-04-16');
INSERT INTO `team` VALUES (1054, '开课吧球队1', '洛杉矶', '2021-04-16');
2 pom.xml配置相关依赖
<dependencies>
<!-- 保证会出现 Spring Config 选项,以在 resources 文件下面建立 xml 文件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- 步骤 7 -->
<!-- 配置映射文件的扫描路径 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
3 编写 mybatis 的配置文件
resources/mybatis.xml
<?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">
<!-- mybatis全局配置文件:节点的顺序参考:按住ctrl+configuration节点,进入文件观看 -->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"> </transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssm1? useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
// 步骤 6
<!-- 注册映射文件 -->
<mappers>
<mapper resource="com/kkb/pojo/Team.xml"/>
</mappers>
</configuration>
4 编写 pojo.Team 实体类
package com.kkb.pojo;
import java.util.Date;
public class Team {
private Integer teamId;
private String teamName;
private String location;
private Date createTime;
@Override
public String toString() {
return "Team{" +
"teamId=" + teamId +
", teamName='" + teamName + '\'' +
", location='" + location + '\'' +
", createTime=" + createTime +
'}';
}
public Integer getTeamId() {
return teamId;
}
public void setTeamId(Integer teamId) {
this.teamId = teamId;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
5 编写ORM映射文件
resources/com/kkb/pojo/Team.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.kkb.pojo.Team">
<select id="queryAll" resultType="com.kkb.pojo.Team">
select * from team;
</select>
</mapper>
6 在mybatis.xml文件中注册映射文件
见上。
7 在pom.xml中配置映射文件的扫描路径
见上。
8 编写测试代码
/test/java/com.kkb.test/TestTeam.java
package com.kkb.test;
import com.kkb.pojo.Team;
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.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
public class TestTeam {
//mybatis的配置文件--相当于创建工厂的图纸
private String resource = "mybatis.xml";
@Test
public void testFindAll(){
try{
//1、读取mybatis的配置文件, 注意导包不要导错了
Reader reader = Resources.getResourceAsReader(resource);
//2、创建SqlSessionFactory对象,目的是获取sqlSession--根据图纸创建工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//3、创建可以执行SQL语句的SqlSession--工厂创建产品
SqlSession sqlSession = sqlSessionFactory.openSession();
//4、执行SQL语句
List<Team> teamList = sqlSession.selectList("com.kkb.pojo.Team.queryAll");
//5、循环输出查询的结果
for(Team team: teamList) {
System.out.println(team);
}
//6、关闭SqlSession,释放资源
sqlSession.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
9 测试类运行结果
Team{teamId=1001, teamName='76人', location='宾夕法尼亚州费城', createTime=Sun Jan 01 08:00:00 CST 1967}
Team{teamId=1002, teamName='篮网', location='纽约州布鲁克林', createTime=Sun Jan 01 08:00:00 CST 1967}
Team{teamId=1003, teamName='雄鹿', location='威斯康星州密尔沃基', createTime=Mon Jan 01 08:00:00 CST 1968}
Team{teamId=1004, teamName='黄蜂', location='夏洛特', createTime=Thu Jan 01 08:00:00 CST 2004}
Team{teamId=1005, teamName='老鹰', location='乔治亚州亚特兰大市', createTime=Sat Jan 01 08:00:00 CST 1949}
Team{teamId=1006, teamName='热火', location='佛罗里达州迈阿密', createTime=Fri Jan 01 08:00:00 CST 1988}
Team{teamId=1007, teamName='凯尔特人', location='马赛诸萨州波士顿', createTime=Tue Jan 01 08:00:00 CST 1946}
Team{teamId=1008, teamName='尼克斯', location='纽约州纽约市', createTime=Thu Jan 01 08:00:00 CST 1976}
Team{teamId=1009, teamName='步行者', location='印第安纳波利斯', createTime=Sun Jan 01 08:00:00 CST 1967}
Team{teamId=1010, teamName='公牛', location='伊利诺斯州芝加哥', createTime=Sat Jan 01 08:00:00 CST 1966}
Team{teamId=1011, teamName='猛龙', location='加拿大安大略省多伦多', createTime=Sun Jan 01 08:00:00 CST 1995}
Team{teamId=1012, teamName='骑士', location='俄亥俄州克里夫兰', createTime=Thu Jan 01 08:00:00 CST 1970}
Team{teamId=1013, teamName='奇才', location='华盛顿D.C', createTime=Sun Jan 01 08:00:00 CST 1961}
Team{teamId=1014, teamName='魔术', location='佛罗里达州奥兰多', createTime=Sun Jan 01 08:00:00 CST 1989}
Team{teamId=1015, teamName='活塞', location='密歇根州底特律', createTime=Thu Jan 01 08:00:00 CST 1948}
Team{teamId=1016, teamName='爵士', location='犹他州盐湖城', createTime=Tue Jan 01 08:00:00 CST 1974}
Team{teamId=1017, teamName='太阳', location='亚利桑那州菲尼克斯', createTime=Mon Jan 01 08:00:00 CST 1968}
Team{teamId=1018, teamName='快船', location='洛杉矶', createTime=Thu Jan 01 08:00:00 CST 1970}
Team{teamId=1019, teamName='掘金', location='科罗拉多州丹佛', createTime=Thu Jan 01 08:00:00 CST 1976}
Team{teamId=1020, teamName='湖人', location='加利福尼亚州洛杉矶', createTime=Thu Jan 01 08:00:00 CST 1948}
Team{teamId=1021, teamName='开拓者', location='俄勒冈州波特兰', createTime=Thu Jan 01 08:00:00 CST 1970}
Team{teamId=1022, teamName='独行侠', location='达拉斯', createTime=Tue Jan 01 08:00:00 CST 1980}
Team{teamId=1023, teamName='灰熊', location='田纳西州孟菲斯', createTime=Sun Jan 01 08:00:00 CST 1995}
Team{teamId=1024, teamName='马刺', location='德克萨斯州圣安东尼奥', createTime=Thu Jan 01 08:00:00 CST 1976}
Team{teamId=1025, teamName='勇士', location='加州奥克兰', createTime=Tue Jan 01 08:00:00 CST 1946}
Team{teamId=1026, teamName='鹈鹕', location='新奥尔良', createTime=Fri Jan 01 08:00:00 CST 1988}
Team{teamId=1027, teamName='国王', location='加州萨科拉门托', createTime=Thu Jan 01 08:00:00 CST 1948}
Team{teamId=1028, teamName='雷霆', location='俄克拉荷马州俄克拉荷马城', createTime=Sun Jan 01 08:00:00 CST 1967}
Team{teamId=1029, teamName='火箭', location='德克萨斯州休斯顿', createTime=Sun Jan 01 08:00:00 CST 1967}
Team{teamId=1030, teamName='森林狼', location='明尼苏达州明尼阿波利斯', createTime=Sun Jan 01 08:00:00 CST 1989}
Team{teamId=1047, teamName='lina的球队', location='洛杉矶', createTime=Wed Apr 14 08:00:00 CST 2021}
Team{teamId=1053, teamName='lina的球队', location='洛杉矶', createTime=Fri Apr 16 08:00:00 CST 2021}
Team{teamId=1054, teamName='开课吧球队1', location='洛杉矶', createTime=Fri Apr 16 08:00:00 CST 2021}