Mybatis-delete operation

Source: Internet
Author: User

1. Create a project and database
Project name: mybatis092901
Database Name: mybatis0929
Table Name: Dept
Create Table 'dept '(
'Deptno' int (11) not null,
'Deptname' varchar (30) default null,
'Location' varchar (30) default null,
Primary Key ('deptno ')
) Engine = InnoDB default charset = utf8;
Table Name: EMP
Create Table 'emp '(
'Empno' int (11) not null,
'Ename' varchar (30) default null,
'Hiredate' date default null,
'Job' varchar (30) default null,
'Sal 'double default null,
'Mgr 'varchar (30) default null,
'Comm 'varchar (30) default null,
'Deptno' int (11) default null,
Primary Key ('empno ')
) Engine = InnoDB default charset = utf8;
2. Add a jar package
1. Create the lib directory on the project
/Lib
2. Add jar under the lib directory
Junit-4.10.jar
Mybatis-3.2.2.jar
Mysql-connector-java-5.1.10-bin.jar
3. Add a configuration file
1. Create the conf directory in the project
/Conf
2. Add a configuration file under the conf directory
Profile name: mybatis-config.xml
Configuration File Content:
<? 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>
<Typealias alias = "EMP" type = "cn. jbit. mybatis092901.domain. EMP"/>
</Typealiases>
<Environments default = "development">
<Environment id = "development">
<Transactionmanager type = "JDBC"/>
<Datasource type = "pooled">
<Property name = "driver" value = "com. MySQL. JDBC. Driver"/>
<Property name = "url" value = "JDBC: mysql: // localhost: 3306/mybatis0929"/>
<Property name = "username" value = "root"/>
<Property name = "password" value = "root"/>
</Datasource>
</Environment>
</Environments>
</Configuration>
4. Create an object class
1. Create a package under the src directory
Package name: CN. jbit. mybatis092901.domain
2. Create an object class under the package
Class Name: Dept. Java
Content:
Public class dept implements serializable {
Private integer deptno; // department ID
Private string deptname; // Department name
Private string location; // Department address
// Omit get and set
}
Class Name: EMP. Java
Content:
Public class EMP implements serializable {
// Employee name
Private string empname;
// Employee ID
Private integer empno;
// Employee start time
Private date hiredate;
// Employee position
Private string job;
// Employee salary
Private double salary;
// Manager ID
Private integer Mgr;
// Bonus
Private double comm;
// Department ID
Private integer deptno;
// Omit get and set
}
5. Persistence Layer Design
1. Interface Design
1). Create a package in the src directory
Package name: CN. jbit. mybatis092901.dao
2) create an interface under the package
Interface Name: iempdao. Java
Interface content:
Public interface iempdao {
/**
* Example in 10.2.3: delete an employee according to the employee ID
* @ Param empno
*/
Public void deleteemp (INT empno );
}
6. Add related ing files
1. Add a configuration file under Conf
Ing file name: empdaomapper. xml
Ing file content:
<? 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>
<Mapper namespace = "cn. jbit. mybatis092901.dao. iempdao">
<Delete id = "deleteemp" parametertype = "int">
Delete from EMP where empno =#{ empno}
</Delete>
</Mapper>
VII. Persistence Layer implementation class design
1). Create a package under the src directory
Package name: CN. jbit. mybatis092901.dao. impl
2). Create an implementation class under the package
Implementation Class Name: empdaoimpl. Java
Implementation class content:
/**
* To make the code clear, sqlsession is obtained using the tool class.
*/
@ Override
Public void insertemp (EMP ){
String resource = "mybatis-config.xml ";
Reader reader = NULL;
Sqlsessionfactory factory = NULL;
Sqlsession session = NULL;
Try {
Reader = resources. getresourceasreader (Resource );
Sqlsessionfactorybuilder builder = new sqlsessionfactorybuilder ();
Factory = builder. Build (Reader );
Session = factory. opensession ();
Session. Delete ("cn. jbit. mybatis092901.dao. iempdao. deleteemp", empno );
Session. Commit ();
} Catch (exception E1 ){
E1.printstacktrace ();
} Finally {
Session. Close ();
}
}
8. Add a ing file reference to the configuration file
1. Add mapper in the mybatis-fonfig.xml File
<Mappers>
<Mapper resource = "empdaomapper. xml"/>
</Mappers>
9. Test operations
1. Create the test directory in the project
/Test
2. Create a package under the test directory
CN. jbit. mybatis092901.dao
3. Create a test class under the package
Class Name: empdaotest. Java
Content:
Public class empdaotest {
// Implementation of the employee class Persistence Layer
Private Static iempdao empdao = new empdaoimpl ();
@ Test
Public void testdeleteemp (){
Empdao. deleteemp (3 );
}
}

This article from the "Su Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1559275

Mybatis-delete operation

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.