The company decided to use the new project MyBatis, although this has been learned but has not been used to forget almost, and the project is relatively tight, there is no time to learn a system point, had to be very sketchy Baidu to reach the extent of the line.
Which involves the insertion of the entity required to return the primary key ID problem, previously with SSH with Oracle is easy to implement, because there are sequence, and MySQL is basically useless, so had to go to Baidu.
This content is still relatively good to check, as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
"Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "Com.dao.UserDao" >
VALUES (#{username},#{classes}) </insert>
Originally we generally configure a do not need to return the primary key, and the ID is self-increment, only need the above configuration, the values in the curly braces field and ParameterType point to the Entity class field case remember to correspond, Then the path of the namespace corresponds to the interface method path, and then calls the SQL interface method simply as follows:
Public Interface userdao{ /** * New student @param User */ Public void insertuser (user user);
If you need to return the value of the self-increment primary key when inserting, the above method needs to be changed slightly, where the XML file that holds the database statement needs to change the configuration accordingly, and the return value type of the interface method needs to be changed as follows:
XML method: Remember to add Usegeneratedkeys and Keyproperty configuration, the former refers to whether to use the JDBC Getgenereatedkeys method to get the primary key and assign a value to the properties of the Keyproperty settings, The latter is the entity class primary key field
VALUES (#{username},#{
</insert>
The interface method return value is changed to int, because MySQL also has only int type value to support self-increment, but has not tried the return value for string is not able to return successfully, I guess it should be possible
/** * New student @param user * /insertuser (user user);
So far in fact there is no problem, it is easy to Baidu to, specifically in the call, I accidentally entered a pit, my program code snippet similar to this:
User user=new User (); User.setusername ("Acfun");
User.setclasses ("Solitary One Class");
int Id=userdao. Insertuser (user);
System.out.println (ID)
As a result I found that no matter how the return is 1, even if the database of the primary key has been increased to 10 ...
Later only to know that the posture is not correct, should be as follows:
User user=new User (); User.setusername ("Acfun"); User.setclasses ("Solitary One Class"); userdao.insertuser (user); System.out.println (user.getuserid);
This is the self-increment ID of the return.
Only blame himself did not understand the meaning, in fact, they insert the back of the primary key is directly assigned to you as the parameter of the entity ID inside, as for the method return value should be the number of affected rows, the Usegeneratedkeys said very clearly, Use the JDBC Getgenereatedkeys method to get the primary key and assign a value to the properties of the Keyproperty settings, but because of the urgency of the hasty according to Baidu to see how people configure how to configure, take it for granted.
So the first question I also water a lot of words also really embarrassed, because know their level is limited, it is written in detail, there is no reason for anything, because some things in the familiar people seem to be simple, but there is no need to mention, But actually look at your blog post may be a small white, like I am looking for minasocket aspects of the use of the method when feeling deeply hurt, tears, to find the basic is to say the various principles of the source of what, I would like to see the results Bo Master one sentence: This kind of code is simple, no need to repeat ... Look, I was just ... Nor blame others, indeed different positions of different views.
Now the project is too tight, there is no time to learn a good system, the basic is to use what to find out how to use the method, also do not go to the principle of anything, today rare a little idle down a little bit to send a no-depth blog, I hope some and I like small white do not fall the same pit.
has been working overtime for one consecutive months until 9:30 and the weekend is free of the elementary program ape left ...
There may be some error in the code, because it is copied directly from the company project code, and then changed to the user, there may be some path aspects are not correct. After the project a little over, idle down, and then go to their own frame of SSM, to a slightly specific point of learning.
About MyBatis when using MySQL, insert return the self-increment primary key problem