zoukankan      html  css  js  c++  java
  • 02_mybatis的CRUD操作

    1、XML 的实现

    1.1 定义SQL映射xml文件

    1. <insert id="insertUser" parameterType="cn.imentors.mybatis.test1.User">
    2. insert into users(name, age) values(#{name}, #{age});
    3. </insert>
    4. <delete id="deleteUser" parameterType="int">
    5. delete from users where id=#{id}
    6. </delete>
    7. <update id="updateUser" parameterType="cn.imentors.mybatis.test1.User">
    8. update users set name=#{name},age=#{age} where id=#{id}
    9. </update>
    • parameterType="cn.imentors.mybatis.test1.User",这个参数类型是一个对象,所以占位符必须是对象的属性#{name}

    1.2 在config.xml 中注册这个映射文件

    1. <mappers>
    2. <mapper resource="cn/imentors/mybatis/test1/UserMapper.xml"/>
    3. </mappers>
    4. </configuration>

    1.3 MyBatisUtils

    为了后面测试的方便,将conf.xml文件的加载,写成一个工具类
    1. package cn.imentors.mybatis.util;
    2. public class MybatisUtils {
    3. public static SqlSessionFactory getFactory() {
    4. String resource = "conf.xml";
    5. InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
    6. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
    7. return factory;
    8. }
    9. }
    1.4 测试CRUD操作的XML实现
    • add
    1. @Test
    2. public void testAdd() {
    3. SqlSessionFactory factory = MybatisUtils.getFactory();
    4. //默认是手动提交的
    5. SqlSession session = factory.openSession();
    6. String statement = "cn.imentors.mybatis.test2.userMapper.insertUser";
    7. int insert = session.insert(statement , new User(3, "KK", 23));
    8. //提交
    9. session.commit();
    10. session.close();
    11. System.out.println(insert);
    12. }
    • update
    1. @Test
    2. public void testUpate() {
    3. SqlSessionFactory factory = MybatisUtils.getFactory();
    4. //默认是手动提交的
    5. SqlSession session = factory.openSession(true);
    6. String statement = "cn.imentors.mybatis.test2.userMapper.updateUser";
    7. int update = session.update(statement, new User(3, "KK444", 25));
    8. session.close();
    9. System.out.println(update);
    10. }
    • delete
    1. @Test
    2. public void testDelete() {
    3. SqlSessionFactory factory = MybatisUtils.getFactory();
    4. // 默认是手动提交的
    5. SqlSession session = factory.openSession(true);
    6. String statement = "cn.imentors.mybatis.test2.userMapper.deleteUser";
    7. int delete = session.delete(statement, 3);
    8. session.close();
    9. System.out.println(delete);
    10. }

    2、注解的实现

    2.1 定义SQL映射的接口

    1. public interface UserMapper {
    2. @Insert("insert into users(name, age) values(#{name}, #{age})")
    3. public int insertUser(User user);
    4. @Delete("delete from users where id=#{id}")
    5. public int deleteUserById(int id);
    6. @Update("update users set name=#{name},age=#{age} where id=#{id}")
    7. public int updateUser(User user);
    8. @Select("select * from users where id=#{id}")
    9. public User getUserById(int id);
    10. @Select("select * from users")
    11. public List<User> getAllUser();
    12. }

    2.2 在conf.xml中注册这个映射接口

    1. <mapper class="cn.imentors.mybatis.test3.UserMapper"/>

    2.3 测试

    1. @Test
    2. public void testAdd() {
    3. SqlSessionFactory factory = MybatisUtils.getFactory();
    4. //默认是手动提交的
    5. SqlSession session = factory.openSession(true);
    6. //加载UserMapper接口
    7. UserMapper mapper = session.getMapper(UserMapper.class);
    8. int add = mapper.add(new User(4, "SS", 45));
    9. System.out.println(add);
    10. session.close();
    11. }
    捐赠我们
        良师益友工作室一直在致力于帮助编程爱好更加快速方便地学习编程,如果您对我们的成果表示认同并且觉得对你有所帮助,欢迎您对我们捐赠^_^。
        
  • 相关阅读:
    云计算下一浪潮,腾讯云抢占 Serverless 制高点
    Serverless + GitHub Actions 完美自动化部署静态网站
    Serverless 技术在格灵深瞳的落地实践
    万物皆可 Serverless 之我的 Serverless 之路
    LeetCode 哈希表 136:只出现一次的数字(计数哈希表,异或)
    Spring 01 IOC
    LeetCode 数组:1.两数之和 11. 盛最多水的容器
    LeetCode 链表:21合并两个有序链表 19删除链表的倒数第N个节点
    Maybatis的一些总结(三:增删改查)
    Maybatis的一些总结(一:了解)
  • 原文地址:https://www.cnblogs.com/imentors/p/4809284.html
Copyright © 2011-2022 走看看