标签:返回 pojo str 参数 use ssi from void sele
namespace中的包名要和DAO/mapper接口中的包名一致
选择,查询语句
//根据ID查询用户
User getUserById(int id);
<select id="getUserById" resultType="com.ultraBlast.pojo.User" parameterType="int">
select * from mybatis.user where id = #{id};
</select>
3.测试
@Test
public void getUserById() {
try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user.toString());
}
}
@Test
public void insertUser() {
try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int res = userMapper.addUser(new User(5, "UB", "123456"));
if (res > 0) {
System.out.println("添加成功!");
}
sqlSession.commit();
}
}
@Test
public void updateUser() {
try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int res = userMapper.updateUser(new User(2, "赵六", "123456"));
sqlSession.commit();
}
}
@Test
public void deleteUser() {
try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int res = userMapper.deleteUser(1);
sqlSession.commit();
}
}
**注意点**
+ 增删改需要提交事务
标签:返回 pojo str 参数 use ssi from void sele
原文地址:https://www.cnblogs.com/UltraBlast/p/14457286.html