MyBatis Advanced (1) (Introductory review)

Source: Internet
Author: User

First start case ( and get new record current ID)

1. Create a Mybatis-config.xml file

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><Configuration>    <!--using aliases -    <typealiases>        <!--alias is used for all classes under the package, by default it is a simple class name -        < Packagename= "Cn.cnsdhzzl.entity" />        <!--use aliases by type name for a single class -        <!--<typealias type= "cn.cnsdhzzl.entity.Student" alias= "Student"/> -    </typealiases>    <!--Development Model -    <Environmentsdefault= "Development">        <EnvironmentID= "Development">            <!--transactions that use JDBC -            <TransactionManagertype= "JDBC" />            <!--using your own connection pool -            <DataSourcetype= "Pooled">                < Propertyname= "Driver"value= "Oracle.jdbc.driver.OracleDriver" />                < Propertyname= "url"value= "Jdbc:oracle:thin: @localhost: 1521:orcl" />                < Propertyname= "username"value= "BDQN" />                < Propertyname= "Password"value= "BDQN" />            </DataSource>        </Environment>    </Environments>    <!--Mapping Small Configurations -    <mappers>        <MapperResource= "Cn/cnsdhzzl/dao/studentdao.xml" />    </mappers></Configuration>

2. Create a layered architecture

3. Create Studentdao.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 "><Mappernamespace= "Cn.cnsdhzzl.dao">    <!--Add Action -    <InsertID= "Insertstudent"ParameterType= "Student">INSERT into student (Id,name,address,sex) VALUES (Seq_ssm.nextval,#{name},#{address},#{sex})<SelectkeyKeyproperty= "id"Resulttype= "int">Select Seq_ssm.currval from dual</Selectkey>    </Insert></Mapper>

4. Implementing the Interface method

@Override      Public Integer addstudent (Student stu) {        = sessionutil.getsqlsession ();         int result = Sqlsession.insert ("insertstudent", Stu);        Sqlsession.commit ();        Sqlsession.close ();        System.out.println ("Save result" + result);         return result;    }

5. Testing

@Test      Public void Frist () {        new Student ("BBB", "Beijing", "male");         New Studentdaoimpl ();        Dao.addstudent (Stu);        System.out.println ("Success");    }

Result diagram

Precautions:

Cannot add '; ' after statement (semicolon), SQL exception is reported

MyBatis Advanced (1) (Introductory review)

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.