namespace中的包名要和Dao/mapper接口的包名一致!
选择,查询语句;
1.编写接口
//根据id查询用户 User getUserById(int id);
​ 2.编写对应的mapper.xml中的sql语句
​ 3.测试
@Test public void getUserLike(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); ListuserList = mapper.getUserLike("李"); for (User user : userList) { System.out.println(user); } sqlSession.close(); }
insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd});
update mybatis.user set name = #{name},pwd=#{pwd} where id = #{id};
delete from mybatis.user where id = #{id};
注意点:
增删改需要提交事务(sqlSession.commit())
到此这篇关于详解Mybatis中的CRUD的文章就介绍到这了,更多相关Mybatis的CRUD内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!