• MyBatis笔记----MyBatis数据库表格数据修改更新的两种方法:XML与注解


    继上

    http://www.cnblogs.com/tk55/p/6659285.html

    http://www.cnblogs.com/tk55/p/6660477.html


    注解

    将id:8 name: 小A age:54修改 name:大A age:44

    UserMapper.java

    package com.ij34.model;
    
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    
    public interface UserMapper {
        @Select("select * from users where id=#{id}")
      public User selectUser(int id);
        @Update("update users set name=#{name},age=#{age} where id=#{id}")
      public void updateUser(User user);
    }

    Test.java

    package com.ij34.bean;
    
    import java.io.IOException;
    import java.io.InputStream;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import com.ij34.model.User;
    import com.ij34.model.UserMapper;
    public class Test {
    public static void main(String[] args) throws IOException {
        String resource ="com/ij34/mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession session=sqlSessionFactory.openSession();
       try {
    //       List<User> users=session.selectList("com.ij34.mybatis.UserMapper.selectUser"); 
               UserMapper mapper=session.getMapper(UserMapper.class); 
            User user= mapper.selectUser(8);
            user.setAge(44);
            user.setName("大A");
            mapper.updateUser(user);
            session.commit();
            System.out.println(user);
    } finally {
        // TODO: handle finally clause
       session.close();
    }
    }
    }

    结果

                                                               


    XML

    UserMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
      <mapper  namespace="com.ij34.mybatis.UserMapper">
      <select id="selectUser" parameterType="int" resultType="com.ij34.model.User">
        select * from users where id=#{id};
      </select>
        <update id="updateUser" keyProperty="id">
        update users set name=#{name},age=#{age} where id=#{id}
      </update>
      </mapper>

    Test.java

    package com.ij34.bean;
    
    import java.io.IOException;
    import java.io.InputStream;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import com.ij34.model.User;
    
    public class Test {
    public static void main(String[] args) throws IOException {
        String resource ="com/ij34/mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession session=sqlSessionFactory.openSession();
       try {
           User user=session.selectOne("com.ij34.mybatis.UserMapper.selectUser", 8); //选择
           user.setName("小小A");
           user.setAge(24);
           session.update("com.ij34.mybatis.UserMapper.updateUser", user);    // 更改
           session.commit();
           System.out.println(user);
    } finally {
        // TODO: handle finally clause
       session.close();
    }
    }
    }

    结果

  • 相关阅读:
    文件内部写入及读取(参考疯狂安卓讲义)
    API内部文件读取
    内部存储文件(读)
    内部存储文件(写)
    短信发送器(1.0版)
    按钮点击的三种方法及推广
    struts标签错误:Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
    java中16进制转换10进制
    java project转变成java web project
    oracle,sqlserver,mysql常见数据库jdbc连接
  • 原文地址:https://www.cnblogs.com/tk55/p/6660502.html
Copyright © 2020-2023  润新知