当前位置:   article > 正文

mybatis学习笔记 2.1 增删改_mybatisp 2.1

mybatisp 2.1

(从有道云笔记搬运,博客做备份)


这章很简单,直接贴代码

mapper

public interface PlayersDao {
	//查询选手信息列表
	List<Players> getPlayersList();
	
	//查询选手信息,通过id
	Players getPlayersById(String id);
	
	//插入选手信息
	int insertPlayersInfo(Players player);
	
	//更新选手信息,通过id
	int updatePlayersInfoById(Players player);
	
	//删除选手信息,通过id
	int deletePlayersInfoById(String id);
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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对应的就是dao,namespace就是对应dao层的地址,绑定接口 -->
<mapper namespace="com.study.mybatis.dao.PlayersDao">
	<!-- id是对应方法的名字,resultType是返回类型 -->
	<select id="getPlayerList" resultType="com.study.mybatis.pojo.Players">
		<!-- 这里面写的是sql -->
		select * from players;
	</select>
	
	<select id="getPlayersById" resultType="com.study.mybatis.pojo.Players" parameterType="java.lang.String">
		select * from players where id = #{id};
	</select>
	
	<insert id="insertPlayersInfo" parameterType="com.study.mybatis.pojo.Players">
		insert into players(id,team,situation) values(#{id}, #{team}, #{situation});
	</insert>
	
	<update id="updatePlayersInfoById" parameterType="com.study.mybatis.pojo.Players">
		update players set team = #{team}, situation = #{situation} where id=#{id};
	</update>
	
	<delete id="deletePlayersInfoById" parameterType="java.lang.String">
		delete from players where id = #{id};
	</delete>
	
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

测试类

@Test
public void test() {
    //获取sqlSession
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //获取mapper(Dao)
    PlayersDao playersDao = sqlSession.getMapper(PlayersDao.class);
    //执行SQL
    /*
    * List<Players> players = playersDao.getPlayersList(); for (Players p :
    * players) { System.out.println(p.getTeam() + "-" + p.getSituation() + "-"
    * +p.getId()); }
    */
    //增删改查
    //这里写mapper,方便理解(现在已经理解了,就不用dao来命名了)
    PlayersDao mapper = playersDao;
    //查
    Players p = mapper.getPlayersById("BurNing");
    System.out.println(p.getTeam() + "-" + p.getSituation());
    //add
    int res = mapper.insertPlayersInfo(new Players("yueru", "DK", "二号位"));
    //成功,res = 1
    if (res > 0) {
        System.out.println("插入成功,提交事务");
        sqlSession.commit();
    }else {
        System.out.println("插入失败,回滚事务");
        sqlSession.rollback();
    }	
    //改
    res = mapper.updatePlayersInfoById(new Players("yueru", "DK", "二号位"));
    //成功,res = 1
    if (res > 0) {
        System.out.println("插入成功,提交事务");
        sqlSession.commit();
    }else {
        System.out.println("插入失败,回滚事务");
        sqlSession.rollback();
    }
    //删
    res = mapper.deletePlayersInfoById("2009");
    if (res > 0) {
        System.out.println("删除成功,提交事务");
        sqlSession.commit();
    }else {
        System.out.println("删除失败,回滚事务");
        sqlSession.rollback();
    }
    //关闭sqlSession
    sqlSession.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

注意

insert和update没有返回类型
增删改需要提交事务(commit)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/949381
推荐阅读
相关标签
  

闽ICP备14008679号