DCRU
namespace(命名空间)
namespace中的包名要和Dao/mapper接口的包名一致!
Select
选择,查询语句;
id,就是对应的namesce中的方法名;
resultType:Sql语句执行的返回值!
parameterType:参数类型
1.编写接口
//根据id查询用户
User getUserById(int id);
2.编写对应的mapper中的sql语句
<select id="getUserById" parameterType="int" resultType="com.bubbles.pojo.User">
select * from mybatis.user where id =#{id}
</select>
3.测试
@Test
public void getUserById(){
SqlSession sqlSession = Mybatisutils.getSqlSession ();
UserDao mapper = sqlSession.getMapper (UserDao.class);
User user = mapper.getUserById (1);
System.out.println (user);
sqlSession.close ();
}
insert
<!--对象中的属性,可以直接取出来-->
<insert id="addUser" parameterType="com.bubbles.pojo.User">
insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
update
<update id="updateUser" parameterType="com.bubbles.pojo.User">
update mybatis.user set name=#{name },pwd=#{pwd} where id = #{id};
</update>
delete
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id};
</delete>
注意点:
增删改需要提交事务!