Mybatis 增删改查
一:解析 UserMapper.xml (上篇文章提到的)
- namespace
namespace 中的报名要和 Dao/Mapper 接口的包名一致 - select:选择,查询语句:
· id:就是对应的 namespace 中的方法名
· resultType: sql 语句的返回值
· parameterType: 参数类型
· #{}:用来接收参数 - update:更新语句
- insert:插入语句
- delete:删除语句
二:Mybatis 的增删改查
- 增:insert
搭建好环境之后,Mybatis 对数据库的操作只需要在接口文件 UserMapper.java 编写接口,之后再在接口实现类也就是 UserMapper.xml 中使用 sql 实现对数据库的操作即可。
接口:
public interface UserMapper {
//添加一个用户
int addUser(User user);
}
接口实现:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.kuang.dao.UserMapper">
<insert id="addUser" parameterType="com.kuang.pojo.User">
insert into user (id, name, pwd)values (#{id},#{name},#{pwd});
</insert>
</mapper>
测试:
public class UserDaoTest {
//增删改需要提交事务
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.addUser(new User(4,"麻子","123456"));
sqlSession.commit(); //提交事务
sqlSession.close();
}
}
- 删:delete
接口:
public interface UserMapper {
//删除用户信息
int deleteUser(int id);
}
接口实现:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.kuang.dao.UserMapper">
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id};
</delete>
</mapper>
测试:
public class UserDaoTest {
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}
- 改:update
接口:
public interface UserMapper {
//修改用户信息
int updateUser(User user);
}
接口实现:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.kuang.dao.UserMapper">
<update id="updateUser" parameterType="com.kuang.pojo.User">
update user set name=#{name},pwd=#{pwd} where id=#{id};
</update>
</mapper>
测试:
public class UserDaoTest {
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"麻子","654321"));
sqlSession.commit();
sqlSession.close();
}
}
- 查:select
接口:
public interface UserMapper {
//根据id查询用户
User getUserById(int id);
}
接口实现:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.kuang.dao.UserMapper">
<select id="getUserById" parameterType="int" resultType="com.kuang.pojo.User">
select * from user where id = #{id};
</select>
</mapper>
测试:
public class UserDaoTest {
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
}