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)