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);