mybaits 配置insert 主键自动增长失效原因

xiaoxiao2021-02-28  199

初次涉猎mybaits 框架和mysql 数据库导致框架使用出现很多的低级失误,在此分享给大家避免此类错误:

配置文件

<insert id="insertUser1" parameterType="domain.User" useGeneratedKeys="true" keyProperty="ID"> </insert>

javabean

public class User { private Integer id ; private String username; private Date birthday; private int sex; private String address; . . .

测试类

public void insertData() throws Exception{ //通过sqlSessionFactory工厂来生成sqlSessioin SqlSession sqlSession = sqlSessionFactory.openSession(); //namespace+sql语句的ID User user = new User(); user.setUsername("NB1121"); user.setBirthday(new Date()); user.setSex(1); //user.setId(1231); user.setAddress("辽宁沈阳"); sqlSession.insert("test.insertUser", user); System.out.println(user.getId()); sqlSession.commit(); sqlSession.close(); System.out.println(total); }

* 出现了 以下两种错误*

mybaits 设置为主键自增长,但是每次插入插入的主键都是0导致主键冲突插入时主键为空。
第一种情况 是因为mysql数据库建表的时候没有设置为主键自动增长 2.插入主键为空的原因是因为id 主键的类型为Integer 不是int原生类型,插入时没能自动拆箱、所以就将主键设置为空了。(应该是这样)
转载请注明原文地址: https://www.6miu.com/read-39704.html

最新回复(0)