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?