關于mapper.xml中的一點問題
為什么在 mapper java文件中的這些方法,例如updateByPrimaryKeySelective,
insertSelective
在? sql? 的 xml 中并沒有返回主鍵id,請問但是缺能接收到,請問是如何做到的?或者說哪里實現的?謝謝老師,這是我能夠提問的最后連個積分了,剩下還有問題我都放到評論區里面了,希望老師能夠回答下,不吝賜教。謝謝!


為什么在 mapper java文件中的這些方法,例如updateByPrimaryKeySelective,
insertSelective
在? sql? 的 xml 中并沒有返回主鍵id,請問但是缺能接收到,請問是如何做到的?或者說哪里實現的?謝謝老師,這是我能夠提問的最后連個積分了,剩下還有問題我都放到評論區里面了,希望老師能夠回答下,不吝賜教。謝謝!


舉報
2019-02-17
為啥log.info("code={} msg={} data={} ",code,msg,data); 這句話輸出沒有一個 {} 花括號
別放在評論,不然其他想告訴你的不方便,在這個花括號是占位符,輸出是,后面的參數依次替換前面的花括號,我用的logback好像最多只能有兩個花括號(原諒我答非所問)
本問題的答案:
這個問題問的比較深,我回答不了
但是還是想跟你說下,方法的返回值跟主鍵沒有關系,增刪改返回值是影響的數據庫操作的條數數據,具體實現我解釋不了;
插入返回主鍵的id在插入數據的實體類中,不是方法的返回值,實現SQL是
SELECT @@IDENTITY ,框架具體實現我也解釋不了
希望能給你增加一點理解
2019-02-17
1、insertxxxx調用 會返回主鍵是因為 .xml 里面配置了這個:
useGeneratedKeys="true"?keyColumn="id"2、但是要獲取主鍵,需要調用時這樣做:對象.insertxxx()之后,對象.getId() 就是返回的主鍵;而 對象.insertxxx() 的返回值是操作數據庫后受影響的行數!
2019-02-17
回復 wonderq_ubuntu:log.info("響應結果:{} ",res);
原因可能是日志jar包要導入有問題:
<!--屬性-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? ? ? <version>1.18.4</version>
? ? ? ? </dependency>
? ? ? ? <!--日志-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? ? ? <artifactId>slf4j-api</artifactId>
? ? ? ? ? ? <version>1.7.25</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>ch.qos.logback</groupId>
? ? ? ? ? ? <artifactId>logback-core</artifactId>
? ? ? ? ? ? <version>1.2.3</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>ch.qos.logback</groupId>
? ? ? ? ? ? <artifactId>logback-classic</artifactId>
? ? ? ? ? ? <version>1.2.3</version>
? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <artifactId>slf4j-api</artifactId>
? ? ? ? ? ? ? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? </exclusions>
? ? ? ? </dependency>
在類上使用注解:@Slf4j
就可以使用log對象了,你試試
lombok了解一下
2019-02-17
<insert?id="add" ????????useGeneratedKeys="true"?keyColumn="id"?keyProperty="id" ????????parameterType="user"> ????INSERT?INTO?`user`?(`name`,sex,register_ts)?VALUES?(#{name},#{sex},#{registerTs})??? ?????<selectKey?resultType="int"?keyProperty="id"?order="AFTER"> ????????SELECT?LAST_INSERT_ID()????</selectKey> </insert>2019-02-17
插入返回主鍵是這樣寫的 <insert?id="add" ????????useGeneratedKeys="true"?keyColumn="id"?keyProperty="id" ????????parameterType="user"> ????INSERT?INTO?`user`?(`name`,sex,register_ts)?VALUES?(#{name},#{sex},#{registerTs})????<selectKey?resultType="int"?keyProperty="id"?order="AFTER"> ????????SELECT?LAST_INSERT_ID()????</selectKey> </insert>