namespace设置包名,需要与mapper接口的包名一致
<mapper namespace="com.qq.mapper.UserMapper">
id:对应包名中的方法
resultType:对应Sql语句执行的返回值
parameterType:参数类型
1.select
<select id="getUserById" resultType="com.qq.entity.User" parameterType="int">
select * from user where id = #{id}
</select>
2.Insert
<insert id="insertUser" parameterType="com.qq.entity.User">
insert into user(id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
3.Update
<update id="updateUser" parameterType="com.qq.entity.User">
update user set name=#{name},pwd=#{pwd} where id = #{id}
</update>
4.Delete
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
注: 增删改需要提交事务
sqlSession.commit();
@org.junit.Test
public void insertUser(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int res = userMapper.insertUser(new User(4,"Luwei","weilu"));
if(res>0){
System.out.println("success");
}
sqlSession.commit();
sqlSession.close();
}