(1)添加用户(添加User记录)
<!--插入用户 --> <insert id="insertUser" parameterType="com.songyan.demo1.User"> insert into users (username,birthday,address,sex) values (#{username},#{birthday},#{address},#{sex}) </insert>
//创建用户 User u=new User(); u.setAddress("useradd"); u.setBirthday(null); u.setSex("男"); u.setUsername("张三"); //插入用户 int count=sqlSession.insert("", u);
sqlSession.commit();
(2)添加用户后紧跟着返回id
//创建用户 User u=new User(); u.setAddress("useradd"); u.setBirthday(new Date()); u.setSex("男"); u.setUsername("张三"); //执行Sql语句 int account = sqlSession.insert("user.insertUser", u); sqlSession.commit(); //插入数据后返回id System.out.println(u.getId());