Mybatis 的学习之路3

Mybatis 增删改查

一:解析 UserMapper.xml (上篇文章提到的)

  • namespace
    namespace 中的报名要和 Dao/Mapper 接口的包名一致
  • select:选择,查询语句:
    · id:就是对应的 namespace 中的方法名
    · resultType: sql 语句的返回值
    · parameterType: 参数类型
    · #{}:用来接收参数
  • update:更新语句
  • insert:插入语句
  • delete:删除语句

二:Mybatis 的增删改查

  1. 增: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();
    }
}
  1. 删: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();
    }
}
  1. 改: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();
    }
}
  1. 查: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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值