CRUD
- 增加(Create)、检索(Retrieve)、更新(Update)和删除(Delete)
namespace
- namespace中的包名要和dao/mapper接口的包名一致
select(查询)
选择,查询语句:
-
id :就是对应的namespace中的方法
-
resultType:sql语句执行的返回值
-
parameterType:参数类型
(1)编写接口
User getUserById(int id);
(2)编写接口对应的mapper中的语句
<select id="getUserById" parameterType="int" resultType="com.gt.pojo.User">
select * from mybatis.users where id = #{id}
</select>
(3)测试
@Test
public void getUserById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);