For the use of MyBatis in Mapper instrances, another way to operate through the session

Source: Internet
Author: User

1String resource = "Mybatis-config.xml";2InputStream InputStream =NULL;3         Try {4             //Get Sqlsessionfactory5InputStream =Resources.getresourceasstream (Resource);6Sqlsessionfactory factory =NewSqlsessionfactorybuilder (). Build (InputStream);7             //Get sqlsession8sqlsession session =factory.opensession ();9             Try {Ten                 //method1 One                 //Student Student = Session.selectone ("Cn.cgq.demo.mybatis.mapper.StudentMapper.selectStudent", ten); A                 //method2 -Studentmapper Mapper=session.getmapper (Studentmapper.class); -Student Student=mapper.selectstudent (NewLong (10)); the                 //Output Results - System.out.println (student.tostring ()); -}finally { - session.close (); +             } -}Catch(IOException e) { + e.printstacktrace (); A}

Mapped SQL statements can be executed directly from the sqlsession instance in method1

And in the use of METHOD2 is a more straightforward way, there are many advantages, first it is not based on string constants, it will be more secure;

Second, if your IDE has code completion, you can take advantage of this feature on the basis of the mapped SQL statement.

With interfaces that can reasonably describe parameters and return values for a given statement (for example, Blogmapper.class), you can now execute more clear and type-safe code without worrying about the error-prone string literals and coercion of type conversions.

First, you need to create a Studentmapper interface class, which is defined by the id attribute of the <select> tag in the Studentmapper.xml map file, as the method name, Resulttype as the return value

So the code is right, but the execution will be an error, report what is wrong?

Type interface Cn.cgq.demo.mybatis.bean.mapper.StudentMapper is not known to the mapperregistry.

This means that the interface is not registered, then you need to add the mapping to the Mybatis-config.xml configuration file.

In <mappers> add <mapper class= "Your interface class's full package name + class name"/>//because it is class, so unlike resource, you have to use "."

It's not enough because you're going to get an error.

Invalid bound statement (not found): cn.cgq.demo.mybatis.bean.mapper.StudentMapper.selectStudent

That's it, says no way to find it. In other people's blogs, there are four things that may be wrong in these four cases.

1.Mapper's namespace write the wrong!!! Note the changes.

2.The method of Userdao is not in Userdao.xml, and then the method that executes Userdao will report this

3. the Userdao method return value is List<user>, while the SELECT element is not configured properly resultmap, or only resulttype! is configured

4. If you do not confirm the above problem, please modify the corresponding XML file, such as delete a blank line, save. Problem solving

However, look, you will find it all right, nothing wrong. Then maybe you just forgot to add annotations to the method.

@Select ("select * from Student where Sid=#{sid}") public Student selectstudent (Long SID);

You can not find this method, so I will tell you (not found), that Tianjin Love is the annotation, there is no problem, it is normal to display

It takes a few steps to summarize.

1.

1 studentmapper mapper=session.getmapper (studentmapper.  Class); 2 Student student=mapper.selectstudent (new Long (10));

Of course, getting sqlsession is a must.

2. Create the interface class, define the method, the method name is the same as the Studentmapper.xml profile <select> ID, the return value is Resulttype

3. Adding annotations to a method

4. Add a mapping for adding classes in the Mybaits-config.xml file

For the use of MyBatis in Mapper instrances, another way to operate through the session

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.