What's the use of Mapper's namespace in MyBatis?

Source: Internet
Author: User

Original: Http://zhidao.baidu.com/link?url=ovFuTn7-02s7Qd40BOnwHImuPxNg8tXJF3nrx1SSngNY5e0CaSP1E4C9E5J6Xv5fI9P_ dtmqhebrgoid9bk9icy1o9h6o21l6rhrawj_km3

--------------------------------------------------------------------------------------------------------------- ----------------------------------------

lanmo970| Browse 3,381 times programming language Java Database
I define a SQL statement configuration myself but I don't know what's the use of namespace here.
<mapper namespace= "Com.myweb.domain.Article" >
<select id= "Selectallarticle" resulttype= "article" >
SELECT t.* from t_article t WHERE t.flag = ' 1 ' ORDER by T.createtime DESC
</select>
The second related question I define the implementation class
@Repository
public class Articledaoimpl implements Articledao {

@Autowired
Private Sqlsessiontemplate sqlsessiontemplate;
Public list<article> FindAll () {
Return Sqlsessiontemplate.selectlist ("selectallarticle");
}
}
May I ask Sqlsessiontemplate.selectlist ("selectallarticle"); Why do you call the selectallarticle of the Artice.xml file, which is called by ID? If the same ID is selectallarticle in other XML files, which should be called by the program? I think it should be an error, so I would like to ask about the role of namespace here. I now change the namespace to any of the values, such as the COM.MYWEB.DOMAIN.AA program, without error.
Read about the MyBatis documentation that says namespaces make interface binding possible but how to understand it here. Ask a master to help explain


The questioner is adopted by
Landlord:
In MyBatis, the namespace in the mapping file is used to bind the DAO interface, which is interface-oriented programming.
When your namespace binds the interface, you can not write the interface implementation class, MyBatis will automatically
to help you find the corresponding SQL statement to execute, as follows:
Assuming that the Iarticedao interface is defined
public interface Iarticledao
{
List<article> selectallarticle ();
}

For the mapping file, the following:
<mapper namespace= "Iarticledao" >
<select id= "Selectallarticle" resulttype= "article" >
SELECT t.* from t_article t WHERE t.flag = ' 1 ' ORDER by T.createtime DESC
</select>
Note that the method in the interface corresponds to the ID one by one of the SQL statement in the mapping file.
You can use the Iarticedao interface programming directly in your code without having to write the implementation class.

Have questions welcome to ask questions, satisfaction Please accept, thank you!

What's the use of Mapper's namespace in MyBatis?

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.