Employeemapper.xml example, learn Shangang teacher's MyBatis course, record the employeemapper.xml, comments in detail

Source: Internet
Author: User
Tags xml example

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Mapper
Public "-//mybatis.org//dtd Mapper 3.0//en"
"Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "Com.atguigu.mybatis.dao.EmployeeMapper" >
<!--
NameSpace: namespace, specified as the full class name of the interface
ID: Unique identifier
Resulttype: Return value type
#{id}: Remove the ID value from the passed parameter
This is the method inside the interface, get the data object: Public Employee Getempbyid (Integer ID);

-

<select id= "Getempbyid" resulttype= "Com.atguigu.mybatis.bean.Employee" databaseid= "MySQL" >
Select ID, last_name LastName, email from tbl_employee where id = #{id}
</select>

<select id= "Getempbyid" resulttype= "Com.atguigu.mybatis.bean.Employee" databaseid= "Oracle" >
Select employee_id ID, last_name lastName, GENDER GENDER, email
From EMPLOYEE where employee_id = #{id}
</select>

<select id= "selectemp" resulttype= "Com.atguigu.mybatis.bean.Employee" databaseid= "MySQL" >
Select ID, last_name LastName, email, gender from tbl_employee where id = #{id}
</select>

<!--public void Addemp (employee employee); -
<!--Gets the value of the self-increment primary key:
MySQL supports self-increment primary key, self-increment primary key value obtains, MyBatis also uses Statement.getgeneratedkeys () to obtain
Add usegeneratedkeys= "True" to the insert tag; use the self-increment primary key to get the primary key value policy
Keyproperty: Used to specify the value assigned to the obtained primary key value, which can be encapsulated in the employee.id of JavaBean
-
<!--usegeneratedkeys= "true" keyproperty= "id"--
<insert id= "addemp" databaseid= "MySQL" >
INSERT into Tbl_employee (ID, last_name, gender, email) VALUES (#{id}, #{lastname}, #{gender}, #{email})
</insert>
<!--Oracle does not support self-increment, but Oracle can use sequences to simulate the self-increment
The primary key value of each inserted data is the value obtained from the sequence;
-
<!--keyproperty: An attribute ID that identifies the primary key and encapsulates it to JavaBean
Order = "Before" specifies that the current SQL runs before the insert operation
= "After" is specified after the current SQL
Resulttype: What type of primary key to encapsulate is found, which needs to be specified here
Before run Order: SQL that runs the Selectkey query ID first: id attribute for ID encapsulation to JavaBean is isolated
Run the inserted SQL again to remove the value of the id attribute
After run order: First run the inserted SQL (remove the new value from the sequence as the ID
Run the Selectkey isolated ID again sql-->
<insert id= "addemp" databaseid= "Oracle" >
<!--<selectkey keyproperty= "id" order = "before" >
Select Employee_seq.nextval from dual
</selectKey>
<!--the primary key to insert is taken from the sequence.
<!--before--><!--, Jdbctype=null--
Insert into EMPLOYEE (employee_id, Last_name,gender,email) VALUES (#{id}, #{lastname}, #{gender}, #{email})
</insert>

<!--can also write this--
<!--after--
<!--
<insert id= "addemp" databaseid= "Oracle" >
<selectkey keyproperty= "id" order = "after" >
Select Employee_seq.currval from dual</selectkey>
-
<!--INSERT INTO EMPLOYEE (employee_id, Last_name,gender,email)
VALUES (Employee_seq.nextval, #{lastname}, #{gender}, #{email})--
<!--multi-parameter query--
<select id= "Getempbyidandlastname" resulttype= "Com.atguigu.mybatis.bean.Employee" >
SELECT * from EMPLOYEE where employee_id = #{id} and last_name = #{lastname}
</select>
<!--multi-parameter query, using map to encapsulate parameters--
<select id= "Getempbymap" resulttype= "Com.atguigu.mybatis.bean.Employee" >
SELECT * from EMPLOYEE where employee_id = #{id} and last_name = #{lastname}
</select>
<!--public void deleteemp (Integer id); -
<delete id= "Deleteempbyid" >
Delete from EMPLOYEE where employee_id = #{id}
</delete>

<!--public void Updateemp (employee employee); -
<update id= "Updateemp" >
Update EMPLOYEE
Set last_name = #{lastname}, EMAIL = #{email}, GENDER = #{gender}
WHERE id = #{id}
</update>

</mapper>

Employeemapper.xml example, learn Shangang teacher's MyBatis course, record the employeemapper.xml, comments in detail

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.