• Mybatis_插入数据


    插入一条数据

    <insert id = "insert" parameterType="xxx.x.Person">
            insert into person (person_id, gender, person_addr, birthday)
            values(#{personId},#{name},#{gender},#{personAddr},#{birthday})
        </insert>

    java语句

    SqlSessionFactory sessionFactory;
        public void setUp() throws Exception {
            InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
            sessionFactory = new SqlSessionFactoryBuilder().build(in);
        }
        public void insert() {
            //创建SqlSession
            SqlSession session = sessionFactory.openSession();
            try {
                Person p = new Person();
                p.setName("李四");
                p.setGender(1);
                p.setAddress("上海");
                p.setBirthday(new Date());
                int count = session.insert("xxx.x.mapper.PersonTestMapper.insert",p); //此处有一个返回值,是影响的行数
                session.commit(); //数据库的变更(增删改)都要提交事务
                System.out.println(count);
            }catch (Exception e) {
                e.printStackTrace();
                session.rollback();
            }finally {
                session.close();
            }
            
        }

    返回主键插入:

    <!--
            selectKey:是做主键返回的
            keyProperty:接收返回主键的属性
            order:insert语句和生成主键的sql的执行顺序mysql是AFTER,oracle是BEFORE
            resultType:返回主键的数据类型
            生成主键的sql select LAST_INSERT_ID()
        -->        
            <selectKey keyProperty="personId" order="AFTER" resultType="java.lang.Integer">
                select LAST_INSERT_ID()
            </selectKey>
            insert into person (person_id, gender, person_addr, birthday)
            values(#{personId},#{name},#{gender},#{personAddr},#{birthday})
        </insert>
  • 相关阅读:
    无题
    无题
    Windows NT 和 VMS: 其余的故事 (The Rest of the Story)
    Lachesis Shield Released!
    最近几年来看到的最强的照片
    有关 Nintendo GameCube
    那些带给我欢乐的游戏
    习惯了 .#
    Rootkits
    我写的IDA插件发布了
  • 原文地址:https://www.cnblogs.com/lonske/p/8981547.html
Copyright © 2020-2023  润新知