5 使用Mapper的接口编写Mybatis项目
张创琦 2022.03.12
- 编写TeamMapper
package com.kkb.mapper;
import com.kkb.pojo.Team;
import java.util.List;
/**
* ClassName: TeamMapper
* Mapper接口
* @author wanglina
* @version 1.0
*/
public interface TeamMapper {
List<Team> queryAll();
Team queryById(int teamId);
int add(Team team);
int add2(Team team);
int update(Team team);
int del(int teamId);
}
- TeamMapper.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">
<!--namespace=“完全限定名,接口的完全限定名”-->
<mapper namespace="com.kkb.mapper.TeamMapper">
<!-- 在这里直接复制Team.xml的内容即可 -->
<!-- 查询所有结果 -->
<select id="queryAll" resultType="com.kkb.pojo.Team">
select * from team;
</select>
<!-- 根据ID查询 -->
<!-- parameterType="参数的类型",目前只支持一个参数 -->
<!-- #{id}表示参数 id-自定义, 只需要符合命名规范即可, 没有实际对应意义 -->
<select id="queryById" parameterType="int" resultType="com.kkb.pojo.Team">
select * from team where teamId=#{id} ;
</select>
<!-- 删除一个球队 -->
<delete id="del">
delete from team where teamId=#{id};
</delete>
<!-- 删除多个球队-测试ok -->
<delete id="del2">
delete from team where location=#{loc};
</delete>
<!-- 根据球队编号更新信息 -->
<update id="update" parameterType="com.kkb.pojo.Team">
update team set teamName=#{teamName}, location=#{location} where teamId=#{teamId}
</update>
<!-- 添加一个球队 -->
<!-- parameterType="com.kkb.pojo.Team" 将对象作为参数.
注意 `` 符号的输入
#{值} 值必须是实体类中的属性名称,其实就是占位符 ? -->
<insert id="add" parameterType="com.kkb.pojo.Team">
INSERT INTO `team` (`teamName`, `location`, `createTime`)
VALUES (#{teamName}, #{location}, #{createTime})
</insert>
<insert id="add2" parameterType="com.kkb.pojo.Team">
INSERT INTO `team` (`teamId`,`teamName`, `location`, `createTime`)
VALUES (#{teamId}, #{teamName}, #{location}, #{createTime})
</insert>
</mapper>
- mybatis.xml 配置文件中注册映射文件
<!-- 注册映射文件 -->
<mappers>
<mapper resource="com/kkb/pojo/Team.xml"/>
<mapper resource="com/kkb/mapper/TeamMapper.xml"></mapper>
</mappers>
- 编写测试类
getMapper 方法获取代理对象
package com.kkb.test;
import com.kkb.dao.TeamDao;
import com.kkb.dao.TeamDaoImpl;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.Team;
import com.kkb.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.Date;
import java.util.List;
/**
*ClassName: TeamDaoTest
*TeamMapper的测试类
*@author wanglina
*@version 1.0
*/
public class TeamMapperTest {
// 调试成功,但是注意add和add2函数的利用,因为主键唯一的原因
// 乱码问题尚未解决
private SqlSession sqlSession = MybatisUtil.getSqlSession();
@Test
public void test01(){
TeamMapper teamDao = sqlSession.getMapper(TeamMapper.class);
//添加
Team team = new Team();
team.setTeamId(1001);
team.setTeamName("勇士");
team.setLocation("金州");
team.setCreateTime(new Date());
int num = teamDao.add(team);
sqlSession.commit(); // 必须提交才能让增删改生效
System.out.println(num);
//更新
Team team1 = teamDao.queryById(1001);
team1.setTeamName("lina的球队");
num = teamDao.update(team1);
sqlSession.commit();//必须提交才能让增删改生效
System.out.println(num);
//删除
num = teamDao.del(1001);
sqlSession.commit();//必须提交才能让增删改生效
System.out.println(num);
//查询所有
List<Team> teams = teamDao.queryAll();
teams.forEach(t-> System.out.println(t));
}
}