开始

1. 在接口中增加一个方法

public List<t1Item> getT1ById(@Param("id") int id);

2. 在mapper配置文件中编写对应的标签


<select id="getT1ById" resultType="com.example.mybatis.t1Item">
     select * from t1 where id=#{id} limit 1;
 </select>

3. 测试

@Test
	void contextLoads() throws IOException {
		SqlSessionFactory sessionFactory = SessionFactory.getSessionFactory();
		try (SqlSession session = sessionFactory.openSession()) {
			t1Mapper mapper = session.getMapper(t1Mapper.class);
			mapper.getT1ById(1).forEach(System.out::println);
		}
	}

4. 结果

t1Item{id=1, name='a1', password='1234'}

注意

  • 方法中的参数在xml中用#{}取出
  • 方法中可以用注解 @Param(“name”) 改变名字

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

基本依赖 上一篇
传递Map类型参数 下一篇