mysql插入数据自动生成主键uuid

DemoMapper.java

//注意方法的返回值必须是void;

void add(Demo demo);

==============================================

demoMapper.xml:

<insert id="add" parameterType="com.demo.pojo.Demo">

      <selectKey keyProperty="demoId" resultType="java.lang.String" order="BEFORE">
            select replace(uuid(),'-','')
      </slectKey> 

    INSERT INTO  

            db_demo(

                    demo_id

                    ,name   

                    ,age

            ) 

    VALUES

            (

                #{demoId}

                ,#{name}

                ,#{age}

    )

</insert>

==============================================

DemoServiceImpl.java :

@Autowired

private demoMapper demoMapper;

 

public void add(Demo demo){

    demoMapper.add(demo);

    System.out.println(demo.getDemoId);

}

posted @ 2019-11-19 16:07  那些年的代码  阅读(8933)  评论(0编辑  收藏  举报