存在一张简单的数据表
mybatis对数据库进行增删改查操作步骤:
编写接口
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据id查询用户
User getUserById(int id);
//插入一个用户
int addUser(User user);
//修改用户
int update(User user);
//map修改用户
int update2(HashMap<Object, Object> map);
//删除用户
int delete(int id);
}
实现接口的UserMapper.xml
<?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绑定一个对应的接口-->
<mapper namespace="com.tt.Dao.UserMapper">
<!--id对应绑定接口的方法, resultType对应实体类-->
<select id="getUserList" resultType="com.tt.pojo.User">
select * from mybatis.user
</select>
<!-- 根据id查用户信息-->
<select id="getUserById" parameterType="int" resultType="com.tt.pojo.User">
select * from mybatis.user where id=#{id}
</select>
<!-- 添加一个用户-->
<insert id="addUser" parameterType="com.tt.pojo.User">
insert into mybatis.user (id,name,pwd) value (#{id},#{name },#{pwd})
</insert>
<!-- 修改用户-->
<update id="update" parameterType="com.tt.pojo.User">
update mybatis.user set name =#{name},pwd=#{pwd} where id=#{id}
</update>
<!-- map修改用户-->
<update id="update2" parameterType="map">
update mybatis.user set name =#{username},pwd=#{password} where id=#{userId}
</update>
<!-- 删除用户-->
<delete id="delete" parameterType="int">
delete from mybatis.user where id=#{id}
</delete>
</mapper>
test目录下编写测试类
public class UserMapperTest {
@Test
public void Test(){
// 第一步,获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//执行sql
UserMapper userdao = sqlSession.getMapper(UserMapper.class);
List<User> userList = userdao.getUserList();
//第二步,遍历输出
for (User user : userList) {
System.out.println(user);
System.out.println("-------------------------------");
}
//关闭sqlSession
sqlSession.close();
}
//根据id查询
@Test
public void testgetUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
//插入用户
@Test
public void testAddUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(4, "乌拉", "4545454"));
//事务提交
sqlSession.commit();
sqlSession.close();
}
//修改用户
@Test
public void testUpdate(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.update(new User(4,"嘎达","666666"));
//事务提交
sqlSession.commit();
sqlSession.close();
} //map修改用户
@Test
public void testUpdate2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<Object, Object> map = new HashMap<>();
map.put("username","康师傅");
map.put("password","111111");
map.put("userId",3);
mapper.update2(map);
sqlSession.commit();
sqlSession.close();
}
//修改用户
@Test
public void testDelete(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.delete(4);
//事务提交
sqlSession.commit();
sqlSession.close();
}
}
注意:增删改需要提交事务