(i) MyBatis insertion in SSM

Source: Internet
Author: User

Learning the SSM framework, their own blind, to see others tutorials as well as their own first try to see better ways to come over to update

I have a user table, a message table, a message table with two fields sender (sender) and recipient (Sendee) is the user's ID

Sendee field can be empty, sender is not empty

The front-end incoming value is the sender, the recipient user name, so you have to query the user based on the user name in order to insert the user ID into the message table

First create the entity class for the User and message table (Getset method omitted)

Package Com.pojo;

Package Com.pojo;
public class Message {
	private int id;
	Private User sender;
	Private User Sendee;
	Private String mess;
	Private String emo;
	Private Timestamp time;
}
Package Com.pojo;
public class User {
	private int id;
	Private String username;
	private String password;
	Private String nickname;
}
Then write the SQL statement, Messagemapper and Usermapper interface omitted
Package Con.mapper

User.xml
	<mapper namespace= "Com.mapper.UserMapper" >
        <select id= "Usernamegetuser" parametertype= " String "resulttype=" Com.pojo.User ">
        	select * from   User  where Username= #{username} 
	</mapper > 
        </select>

message.java
	<mapper namespace= "Com.mapper.MessageMapper" >
        < Insert Id= "Add" parametertype= "Com.pojo.Message" >
            insert INTO Message 
                <if test= "sendee.id!=" "> ( Sender,sendee,mess,emo,time) VALUES (#{sender.id},#{sendee.id},#{mess},#{emo},#{time}) </if>   
        	<if test= "sendee.id==" > (sender,mess,emo,time) VALUES (#{sender.id},#{mess},#{emo},#{time}) </if>
        < /insert>
        
	</mapper>
Insert the value passed in is a Message object with two properties user class object sender and Sendee property ID directly write #{sender.id} on the line, Because sendee.id can be empty, write a judgment, if empty, do not insert the Sendee field, otherwise you will report the foreign key error (a FOREIGN key constraint fails)

Then there is the message and the user's service class.

Com.impl

@Service public
class Messageserviceimpl implements messageservice{

	@Autowired
	Private Messagemapper Messagemapper;
	New message public
	void addmess (Message message) {
		messagemapper.add (message);
	}

}

@Service public
class Userserviceimpl implements userservice{
	
	@Autowired
	private Usermapper Usermapper ;
	
		Put back the user's public by user name
	Usernamegetuser (String username) {User
		Usermapper = usermapper.usernamegetuser ( username);
		If the argument is empty or not found returns an empty user
		if (username==null| | Username.equals ("") | | Usermapper==null) {
			return new User ();
		}
		return usermapper;
	}
}
With spring's automatic assembly, the Addmess () method is called the Add () method of Messagemapper, and the message object is passed in.

The Usernamegetuser () method is to return the user object based on the user name, and return an empty user if it is not found (otherwise the IF Judgment statement in the SQL statement will error: (Id,null))


Then there's the control class.

Com.controller

@Controller
@RequestMapping Public
class Messagecontroller {
	@Autowired
	private Messageserviceimpl Messageserviceimpl;
	@Autowired
	private Userserviceimpl Userserviceimpl;
	Private message message = new Message ();
	
	Add Message
	@RequestMapping ("/addmessage") public
	void Addmess (String sender,string sendee,string mess,string Emo {
		Timestamp time =  new Timestamp (New Date (). GetTime ());		System.out.println (time.tostring ());
		Message.settime (time);
		User-assigned to message
		Message.setsender (sender) according to user name;
		Message.setsendee (Userserviceimpl.usernamegetuser (Sendee));
		Message.setmess (mess);
		Message.setemo (emo);
		messageserviceimpl.addmess (message);
	}
}
This way the request from the AddMessage path can be inserted into a message record, the sender parameter cannot be empty, the Sendee parameter can be empty


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.