MyBatis Study of two additions and deletions to investigate

Source: Internet
Author: User

1. Configuring the Mapping File Grilsmapper.xml

<?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= "mapper. Grilsmapper "> <select id=" getgril "parametertype=" int "resulttype=" domain. Grils ">Select* FROM Grils where id=#{id}</select> <resultmap type= "domain.         Grils "id=" Grilsmap "> <id column=" id "property=" id "/> <result column=" name "property=" name "/> <result column= "Nationality" property= "nationality"/> </resultMap> <select id= "Getgrils" ResU ltmap= "Grilsmap" >Select*From grils</select> <insert id= "addgril" parametertype= "domain. Grils ">INSERT into grils values (#{id},#{name},#{nationality})</insert> <update id= "updategril" parametertype= "domain. Grils ">Update grils set name=#{name},nationality=#{nationality} where id=#{id}</update> <delete id= "Deletegril" parametertype= "int" >Delete from grils where ID=#{id}</delete></mapper>
Grilsmapper.xml

The mapping file configuration is simple and configures what you need. The corresponding select Update delete tag, and so on.

2. Add the corresponding method in the interface Grilsmapper

 Packagemapper;Importjava.util.List;Importdomain. Grils; Public InterfaceGrilsmapper {//This method name must be the same as the ID of the mapping file!!!      PublicGrils Getgril (intID);  Public voidAddgril (Grils gril);  PublicList<grils>getgrils ();  Public voidUpdategril (Grils gril);  Public voidDeletegril (intID);}

3. Create a test class GrilsTest2

 Packagetest;ImportJava.io.InputStream;Importjava.util.List;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;Importdomain. Grils;ImportMapper. Grilsmapper; Public classGrilsTest2 {Private Staticsqlsession sqlsession; Private StaticGrilsmapper Grilsmapper; Static{String Resource= "Conf.xml"; InputStream in= Grilstest.class. getClassLoader (). getResourceAsStream (Resource); //Create SqlsessionfactorySqlsessionfactory sessionfactory =NewSqlsessionfactorybuilder (). build (in); Sqlsession=sessionfactory.opensession (); Grilsmapper= Sqlsession.getmapper (grilsmapper.class); }     Public Static voidMain (string[] args) {GrilsTest2 grilsTest2=NewGrilsTest2 ();        Grilstest2.getgril ();        Grilstest2.addgril ();        Grilstest2.getgrils ();        Grilstest2.updategril ();        Grilstest2.getgrils ();        Grilstest2.deletegril ();            Grilstest2.getgrils (); }        //Query a     Public voidGetgril () {Grils Gril= Grilsmapper.getgril (1); System.out.println ("Query a record:" +gril); }        //Add a     Public voidAddgril () {Grils Gril=NewGrils (); Gril.setid (2); Gril.setname (Zhang Ziyi); Gril.setnationality (China);        Grilsmapper.addgril (Gril);        Sqlsession.commit (); System.out.println ("Add a record:" +gril); }        //Search All     Public voidgetgrils () {List<Grils> Grils =grilsmapper.getgrils (); System.out.println ("Query all:" +grils); }        //Update a record     Public voidUpdategril () {Grils Gril=NewGrils (); Gril.setid (1); Gril.setname ("Ms. Cang"); Gril.setnationality (Japan);        Grilsmapper.updategril (Gril);        Sqlsession.commit (); System.out.println ("A record was updated:" +gril); }        //Delete     Public voidDeletegril () {Grilsmapper.deletegril (2); System.out.println ("A record was deleted");    Sqlsession.commit (); }}
GrilsTest2

4. Printing results

Query a record: Grils [id=1, Name= Cang jing Empty, nationality= Japan]
Add a record: Grils [id=2, Name= Zhang Ziyi, nationality= China]
Search all: [Grils [Id=1, Name= Cang jing Empty, nationality= Japan], Grils [id=2, Name= Zhang Ziyi, nationality= China]]
Updated a record: Grils [Id=1, Name=, nationality= Japan]
Search all: [Grils [Id=1, Name=, nationality= Japan], Grils [id=2, Name= Zhang Ziyi, nationality= China]]
A record was deleted
Search all: [Grils [Id=1, Name=, nationality= Japan]

5. It took about half an hour to read the basic knowledge.

MyBatis Study of two additions and deletions to investigate

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.