本章测试:Mybatis注解开发
POM.xml文件如下:
<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/maven-v4_0_0.xsd">
1.8
1.==demo==
CREATE TABLE `tb_team` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL COMMENT '球队名称',
`coach` varchar(20) DEFAULT NULL COMMENT '球队主教练',
`stadium` varchar(10) DEFAULT NULL COMMENT '球队主场',
`address_id` int DEFAULT NULL COMMENT '注册地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3
insert into tb_team values(1,'Juventus F.C.','LiBai','E',2),
(2,'Manchester United F.C.','LiuZongYuan','A',1);
2.mybatis-config代码如下:
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
3.Team.java的代码如下:
package com.example.mybatis.entity;
public class Team {
private int id;
private String name;
private String coach;
private String stadium;
private int addressId;
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCoach() {
return coach;
}
public void setCoach(String coach) {
this.coach = coach;
}
public String getStadium() {
return stadium;
}
public void setStadium(String stadium) {
this.stadium = stadium;
}
@Override
public String toString() {
return "Team [id=" + id + ", name=" + name + ", coach=" + coach + ", stadium=" + stadium + ", addressId="
+ addressId + "]";
}
}
4.TeamDao 代码如下:
package com.example.mybatis.mapper;
import com.example.mybatis.entity.Team;
import org.apache.ibatis.annotations.*;
import java.util.List;
// 声明这是一个Mapper接口,MyBatis会扫描到这个注解并为其生成实现类
@Mapper
public interface TeamDao {
// 查询所有团队信息
@Select("SELECT * FROM tb_team")
List
// 根据ID查询特定团队信息
@Select("SELECT * FROM tb_team WHERE id = #{id}")
Team selectTeamById(@Param("id") int id);
// 插入新的团队信息,假设id是自增主键
@Insert("INSERT INTO tb_team(name, coach, stadium, address_id) VALUES(#{name}, #{coach}, #{stadium}, #{addressId})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insertTeam(Team team);
// 更新团队信息
@Update("UPDATE tb_team SET name=#{name}, coach=#{coach}, stadium=#{stadium}, address_id=#{addressId} WHERE id=#{id}")
int updateTeam(Team team);
// 根据ID删除团队信息
@Delete("DELETE FROM tb_team WHERE id = #{id}")
int deleteTeam(@Param("id") int id);
}
// 假设Team类已经在其他地方定义,且包含id, name, coach, stadium, addressId等属性和对应的getter/setter方法
// 例如:
/*
public class Team {
private Integer id;
private String name;
private String coach;
private String stadium;
private Integer addressId;
// 省略getter和setter方法...
}
*/
5.为了验证上述配置,在测试类TeamAnnotationTest中编写测试代码,主要是实现:插入新的团队信息的代码,要求代码完整,只有JUnit测试框架,并没有springframework,请生成完整可以调试的代码。
package com.example.mybatis.test;
public class TeamAnnotationTest {
}