• mybatis05


    namespace

    1. 将上面案例中的UserMapper接口改名为 UserDao;

    2. 将UserMapper.xml中的namespace改为为UserDao的路径 .

    3. 再次测试

    结论:

    配置文件中namespace中的名称为对应Mapper接口或者Dao接口的完整包名,必须一致!

    select

    • select标签是mybatis中最常用的标签之一

    • select语句有很多属性可以详细配置每一条SQL语句

      • SQL语句返回值类型。【完整的类名或者别名】

      • 传入SQL语句的参数类型 。【万能的Map,可以多尝试使用】

      • 命名空间中唯一的标识符

      • 接口中的方法名与映射文件中的SQL语句ID 一一对应

      • id

      • parameterType

      • resultType

    需求:根据id查询用户

    1、在UserMapper中添加对应方法

    public interface UserMapper {
       //查询全部用户
       List<User> selectUser();
       //根据id查询用户
       User selectUserById(int id);
    }

    2、在UserMapper.xml中添加Select语句

    <select id="selectUserById" resultType="com.kuang.pojo.User">
    select * from user where id = #{id}
    </select>

    3、测试类中测试

    @Test
    public void tsetSelectUserById() {
       SqlSession session = MybatisUtils.getSession();  //获取SqlSession连接
       UserMapper mapper = session.getMapper(UserMapper.class);
       User user = mapper.selectUserById(1);
       System.out.println(user);
       session.close();
    }
  • 相关阅读:
    《超级迷宫》需求规格说明
    超级迷宫冲刺个人计划安排
    审评(HelloWorld团队)
    C语言中的++与*
    a、b交换
    微服务架构浅析及实践心得
    Servlet版本冲突引起的Error
    并发编程:一个100%会发生死锁的程序
    单元测试与Mockito
    Java基础:HashMap假死锁问题的测试、分析和总结
  • 原文地址:https://www.cnblogs.com/huaobin/p/14908664.html
Copyright © 2020-2023  润新知