MyBatis (Increase and deletion check)

Source: Internet
Author: User

1. Introduction of the package in the project:

mybatis-3.2.7, Mysql-connector-java-5.1.22-bin

2. Add configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><Configuration>    <typealiases>         <Typealiasalias= "Person"type= "Com.example.mdemo.model.Person"/>    </typealiases>     <Environmentsdefault= "Development">        <EnvironmentID= "Development">        <TransactionManagertype= "JDBC"/>            <DataSourcetype= "Pooled">            < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver"/>            < Propertyname= "url"value= "Jdbc:mysql://127.0.0.1:3306/demo01" />            < Propertyname= "username"value= "root"/>            < Propertyname= "Password"value= "root"/>            </DataSource>        </Environment>    </Environments>        <mappers>        <MapperResource= "Com/example/mdemo/model/person.xml"/>    </mappers></Configuration>

3. Define the model and interface Java classes as follows:

 Public classPerson {PrivateString FirstName; PrivateString LastName; Private intAge ;  PublicString Getfirstname () {returnFirstName; }     Public voidsetfirstname (String firstName) {firstName=FirstName; }     PublicString Getlastname () {returnLastName; }     Public voidsetlastname (String lastName) {lastName=LastName; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) { Age=Age ; }    }
 Public Interface ipersonoperation {        public person  selectpersonsbyfirstname (String FirstName);          Public List<person> selectpersons (String FirstName);          Public void Addperson (person person);          Public void Updateperson (person person);          Public void Deleteperson (String FirstName);    }

4. Configure the corresponding XML file:

<?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 "><Mappernamespace= "Com.example.mdemo.service.IPersonOperation">    <SelectID= "Selectpersonsbyfirstname"ParameterType= "String"Resulttype= "Person">select * from ' persons ' where FirstName = #{firstname} limit 1</Select>        <SelectID= "Selectpersons"ParameterType= "string"Resultmap= "Resultlistpersons">SELECT * from persons where FirstName like #{firstname}</Select>        <Resultmaptype= "Person"ID= "Resultlistpersons">        <resultcolumn= "FirstName" Property= "FirstName" />        <resultcolumn= "LastName" Property= "LastName" />        <resultcolumn= "Age" Property= "Age" />    </Resultmap>        <InsertID= "Addperson"ParameterType= "Person">INSERT into persons (Firstname,lastname,age) values (#{firstname},#{lastname},#{age}) </Insert>        <UpdateID= "Updateperson"ParameterType= "Person" >update persons set Firstname=#{firstname},lastname=#{lastname},age=#{age} where Firstname=#{firstname} </Update>        <DeleteID= "Deleteperson"ParameterType= "String">Delete from persons where Firstname=#{firstname}</Delete>       </Mapper>

5. The test is used as follows:

public class Test {private static sqlsessionfactory sqlsessionfactory;     private static reader reader;            static{try{reader = Resources.getresourceasreader ("Config/configuration.xml");        Sqlsessionfactory = new Sqlsessionfactorybuilder (). build (reader);        }catch (Exception e) {e.printstacktrace ();    }} public static Sqlsessionfactory getsession () {return sqlsessionfactory;       } public static void Main (string[] args) {sqlsession session = Sqlsessionfactory.opensession (); /* try {person user = (person) session.selectone ("Com.example.mdemo.model.PersonMapper.selectPersonsByFirstNam            E "," Fppau ");            System.out.println (User.getfirstname ());            System.out.println (User.getlastname ());        System.out.println (User.getage ());        } finally {session.close (); }*/try{ipersonoperation personoperation = Session.getmapper (ipersonopeRation.class);            Person person = personoperation.selectpersonsbyfirstname ("Fppau");            System.out.println (Person.getfirstname ());            System.out.println (Person.getlastname ());                                    System.out.println (Person.getage ()); List< Person> Persons = Personoperation.selectpersons ("Fppau");                for (person p:persons) {System.out.println (P.getfirstname ());                System.out.println (P.getlastname ());            System.out.println (P.getage ());                                    } System.out.println (Persons.size ());            Person pp = new person ();            Pp.setfirstname ("Xu");            Pp.setlastname ("OK");                        Pp.setage (40);            Personoperation.addperson (PP);                        Session.commit ();            Person PPP = new person ();            Ppp.setfirstname ("Xu");            Ppp.setlastname ("OK");                        Ppp.setage (20);            Personoperation.updateperson (PPP);                        Session.commit ();            Personoperation.deleteperson ("Fppau");                                             Session.commit ();                               Session.commit (); }finally{Session.close ();       }        }} 

MyBatis (add and revise)

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.