写点什么

13- 注解增删改查

  • 2022 年 5 月 05 日
  • 本文字数:827 字

    阅读完需:约 3 分钟

public interface UserMapper {


//方法存在多个参数,所有的参数前面必须加上 @Param()注解


@Select("select * from user where id =#{id}")


User getUserById(@Param("id") int id);


@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{password})")


int addUser(User user);


@Update("update user set name=#{name},pwd=#{password} where id=#{id}")


int updateUser(User user);


@Delete("delete from user where id=#{uid}")


int deleteUser(@Param("uid")int id);


}


测试


import com.tian.dao.UserMapper;


import com.tian.pojo.User;


import com.tian.utils.MybatisUtils;


import 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 org.apache.ibatis.session.SqlSession;


import org.junit.Test;


import java.util.HashMap;


import java.util.List;


public class UserMapperTest {


@Test


public void test1(){


SqlSession sqlSession = MybatisUtils.getSqlSession();


UserMapper userMapper = sqlSession.getMapper(UserMapper.class);


User userById = userMapper.getUserById(1);


System.out.println(userById);


//关闭 sqlsession


sqlSession.close();


}


@Test


public void test2(){


SqlSession sqlSession = MybatisUtils.getSqlSession();


UserMapper userMapper = sqlSession.getMapper(UserMapper.class);


int i = userMapper.addUser(new User(6, "he", "456"));


System.out.println(i);


sqlSession.close();


}


@Test


public void test3(){


SqlSession sqlSession = MybatisUtils.getSqlSession();


UserMapper userMapper = sqlSession.getMapper(UserMapper.class);


userMapper.updateUser(new User(6, "hel", "456"));


sqlSession.close();


}


@Test


public void test4(){


SqlSession sqlSession = MybatisUtils.getSqlSession();


UserMapper userMapper = sqlSession.getMapper(UserMapper.class);


userMapper.deleteUser(6);

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
13-注解增删改查_Java_爱好编程进阶_InfoQ写作社区