MyBatis implementation of User table operation (complex type)

Source: Internet
Author: User

Increase

@Test Public voidaddtest () {String resource= "Mybatis-config.xml"; Sqlsession sqlsession=NULL; Try{User User=NewUser (); User.setusercode ("AAAA"); User.setusername ("XXXX"); InputStream is=Resources.getresourceasstream (Resource); Sqlsessionfactory Factory=NewSqlsessionfactorybuilder (). Build (IS); Sqlsession=factory.opensession (); Sqlsession.insert ("Cn.bdqn.dao.UserMapper.add", user);                Sqlsession.commit (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace ();        Sqlsession.rollback (); }finally{sqlsession.close (); }            }

Usermapper.xml

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE mapperpublic "-//mybatis.org//dtd Mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><!--If I want to call the mapper file, I'll get the name of the Namespace+id (method name) to access -<Mappernamespace= "Cn.bdqn.dao.UserMapper">    <!--ID to be unique, usually the method name of the drop-down class -    <!--what type of int is returned -    <SelectID= "Count"Resulttype= "int">Select COUNT (1) from user</Select>    <!--Increase -    <InsertID= "Add"ParameterType= "User">INSERT INTO User (Usercode,username,userpassword) VALUES (#{usercode},#{username},#{userpassword}) </Insert>    <!--Modify -    <UpdateID= "Update"ParameterType= "User">update user Set Usercode=#{usercode},username=#{username}, Userpassword=#{userpassword} where Id=#{id} </Update>    <!--Delete -    <DeleteID= "Delete"ParameterType= "User">Delete from user where Id=#{id}</Delete></Mapper>

Parametertype= "Cn.bdqn.pojo.User" in Usermapper.xml add alias to User so it's easier

<!--Introducing jdbc.properties Files -    <PropertiesResource= "Jdbc.properties"/>    <!--Alias aliases -    <typealiases>        <Typealiastype= "Cn.bdqn.pojo.User"alias= "User"/>    </typealiases>

MyBatis implementation of User table operation (complex type)

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.