MyBatis additions and deletions to search

Source: Internet
Author: User

MyBatis additions and deletions to search

When writing this check all the users, JavaBean must write a parameterless construction method, or query all users will be error.

 PackageDemo2; Public classUser {Private intID; PrivateString name; Private intAge ;  PublicUser (intID, String name,intAge ) {        Super();  This. ID =ID;  This. Name =name;  This. Age =Age ; }         PublicUser () {} Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {return"User [id=" + ID + ", name=" + name + ", age=" + Age + "]"; }            }
<?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= "Demo2.usermapper">    <!--get a User object based on ID query -     <SelectID= "GetUser"ParameterType= "int"Resulttype= "Demo2." User ">SELECT * from users where id=#{id}</Select>     <!--Add a user -     <InsertID= "Insertuser"ParameterType= "Demo2." User ">INSERT into users (name,age) values (#{name},#{age}); </Insert>     <!--Modify a user's information -     <UpdateID= "UpdateUser"ParameterType= "Demo2." User ">update users set name=#{name},age=#{age} where id = #{id}</Update>          <DeleteID= "DeleteUser"ParameterType= "int">Delete from users where id = #{id}</Delete>         <SelectID= "Selectallusers"Resulttype= "Demo2." User ">SELECT * from Users</Select>                           </Mapper>
<?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>    <PropertiesResource= "Db.properties"/>        <!--Development: Development mode work: Working mode -    <Environmentsdefault= "Development">        <EnvironmentID= "Development">            <TransactionManagertype= "JDBC" />            <DataSourcetype= "Pooled">                < Propertyname= "Driver"value= "${driver}" />                < Propertyname= "url"value= "${url}" />                < Propertyname= "username"value= "${name}" />                < Propertyname= "Password"value= "${password}" />            </DataSource>        </Environment>    </Environments>        <mappers>                <MapperResource= "Demo2/usermapper.xml"/>            </mappers></Configuration>
 PackageTest;ImportJava.io.InputStream;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;Importjava.util.List;ImportDemo2. User; Public classTest { Public Static voidMain (string[] args) {String resouse= "Conf.xml"; //read the above-mentioned. config fileInputStream is = Test.class. getClassLoader (). getResourceAsStream (Resouse); Sqlsessionfactory Factory=NewSqlsessionfactorybuilder (). Build (IS); Sqlsession Session= Factory.opensession (true);//here, if configured to True, auto-commitString statement = "Demo2.userMapper.getUser"; //User user = (user) Session.selectone (statement, 1);String statement2 = "Demo2.userMapper.updateUser"; intUpdate = Session.update (Statement2,NewUser (1, "Zhang San", 14)); //Delete UserString Statement3 = "Demo2.userMapper.deleteUser"; intDelete = Session.delete (Statement3, 1); //get all the usersString statement4= "Demo2.userMapper.selectAllUsers"; List<User> list=session.selectlist (STATEMENT4);    SYSTEM.OUT.PRINTLN (list); }}

MyBatis additions and deletions

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.