Three questions about the use of MyBatis in Springboot
Reprint Please specify Source address : http://www.cnblogs.com/funnyzpc/p/8495453.html
Originally to talk about PostgreSQL some of the learning summary, unfortunately, the recent period of progress are some similar to subtraction, displacement, type conversion of some of the slightly more pediatrics, the amount ~ (? .?.? ?), this is not a problem, just feel that there is no end point and difficult to talk about, but also temporarily skipped ~, here first say sorry Ah, follow if there is any use difficult or interesting place must come out to talk about ????)? , the beginning of each time to tell a bunch of seemingly useless content, what is the use, what is the use of,??, think for a while, I think some use----is a habit, always record the mood of the recent paragraph. The mood of this thing is unpredictable, in fact, the development is also very important, such as the birth of the periodic table ha ...
Since the beginning of the beginning of the use of springboot, the general problem here is the springboot framework of the problem, this time I say three points, also I encountered in the actual development of the Oh ( ̄﹏ ̄)
1> Press primary key query times DAO type cannot be converted
2> Mapper and DAO methods when querying with tables
3> Insert data return a primary key configuration method
First question: " query the Times by primary key DAO type cannot be converted ".
Well, can be seen as a mybatis bug, currently only in the TK version of the MyBatis , here first out of code and error messages (??? ) :
1 @RequestMapping ({"DAO", "Dao2"})2public zwpaylog Dao (httpservletrequest Request) {3 zwpaylog pl=zwpaylogmapper.selectbyprimarykey (long.valueof ( -1)); 4 Log.info ("testpay->get=>" + json.tojsonstring (PL)); 5 return Pl; 6 }
The code is in fact a line (above the red emphasis), here the "Selectbyprimarykey" method is the TK version of MyBatis implementation, I just borrowed; when I first used mybatis, I didn't report this error, A little touch of the brain, then a good time to find out the problem of their own code, after the search in StackOverflow someone briefly outlined the problem, The general meaning is the TK version and SpringBoot1.5 compatibility issues, personally think that the TK version of MyBatis and Springboot did not complete the problem, and it is a bug it, here for the benefit of the vast number of friends into the pit, put out a simple solution
Create a new Meta-inf folder in the resources directory of the project (or module) and put a spring-devtools.properties file in the folder for example:
Then this configuration file inside add such a sentence: "restart.include.companycommonlibs=tk/mybatis.*", there is only one sentence, here I can not, OK, problem solving (?? ˇ?ˇ?).
Next, the second question summarizes: " Mapper and DAO methods when you are querying a table ."
This question was thought very simple, the fact is not, the first look at the boss or the online sample is to do so, in the DAO interface directly inherit mapper<entity> such a generic mapper can be, concise to even do not have to declare the method inside, like this ( ?° ?? ?°)? :
1 Public Interface extends Mapper<zwcisreport> {2 }
But the actual demand is not a separate table additions and deletions, but a table query, may be the TK version of MyBatis Insight, a moment of dizzy, crazy in Google search related content, attempted ... , and finally, I use the native method to solve.
Here I demonstrate the main code, the first in the mapper XML file to write the corresponding query statement, such as there are two query statement statements (Getproject and getorder) + =
(P2pEyeMapper.sql.xml)
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd ">3 <Mappernamespace= "*.security.admin.mapper.p2peyedao">4 <SelectID= "getproject"Resulttype= "*.security.admin.po.p2peye.p2peyeproject"ParameterType= "Java.util.Map">5 <!--here specific SQL omitted -6 </Select>7 <SelectID= "getorder"Resulttype= "*.security.admin.po.p2peye.p2peyeorder"ParameterType= "Java.util.Map">8 <!--here specific SQL omitted -9 </Select>Ten </Mapper>
And then declare the ID names of the two SQL queries in the mapped DAO, and remember that the namespace also corresponds , well, continue to show the code in P2peyedao.java (DAO).
1 " Span style= "COLOR: #000000" > @Mapper 2 Public interface P2peyedao { 3 // Get project information (superscript) 4 list<p2peyeproject> getproject (map<string,object> params); 5 6 // get bid information 7 list<p2peyeorder> GetOrder (Map<string,object> params); 8 }
Well, here are three points to note:
1> For example, in the Getproject method to put the >1 parameter is recommended to use map to pass the parameter, well, if you can write a PO to encapsulate parameters, so the best, if you want to simple, such as Getproject (param,param2 It is easy to report parameters not found (params is not fund) error
2> If you are using the encapsulated parameter body to pass the parameter, you need to declare the parameter value of "ParameterType" in the corresponding select (XML) Tag .
3>Resulttype This return value is required and must be consistent with the method declared in DAO
As for how to use, here no longer repeat it, the reader understands (???).
Tell the third question: " insert data to return the primary key configuration method ."
This question is not a problem, here I just provide a concise solution to the TK version. First of all, what are the general solution ideas that netizens give you, in the native mybatis is probably such a son:
<insert id= "insertproduct" parametertype= "Domain.model.ProductBean" > <selectkey resulttype= " Java.lang.Long "order=" after "keyproperty=" ProductId "> Select last_insert_id () </selectkey > INSERT into t_product (Productname,productdesrcible,merchantid) VALUES (#{productname},#{ Productdesrcible},#{merchantid}); </insert>
This is the case in DAO:
1 @Mapper 2 Public Interface xxdao{3 Integer insertproduct (Productbean pb); 4 }
Well, I think the only thing to notice here is that the integer returned after calling "Insertproduct" is not the primary key, not the primary key , not the PRIMARY KEY!!! , the second is that in executing this inserted "Productbean" type of PB object, it is called after the insertion has been automatically written to the primary key.
The above is the general solution, if using my big TK way (here is said only in a single table mapper), can be much simpler.
Since TK uses a generic mapper to implement DAO and XML, XML only uses the declaration of the corresponding entity and table mapping parameters can be, the specific SQL implementation is not written (here need generic mapper generics ), this time DAO is as simple as this:
1 Public Interface extends mapper<zwcisreport> {2 }
But the field that automatically generates the ID when it's actually used must be declared like this.
1 /** 2 * Primary Key ID3 */ 4 @Id5 @GeneratedValue (strategy = generationtype.identity)// Sets the primary key to self-increment to write back the main key 6 private Long ID;
Well, in fact, the focus is a "@GeneratedValue" annotation ●ω●, this is the way the framework provides, you need to look at the exact self-search, as for the generated primary key is also in the call after the entity inside.
OK, the end of this chapter!
Now is: 2018-03-03 20:19:54 good evening, everyone.
Three questions about the use of MyBatis in Springboot