Mybatis-using where to dynamically stitch SQL

Source: Internet
Author: User

I. Creating Projects and Databases
Project Name: mybatis092901
Database name: mybatis0929
Table Name: Dept
CREATE TABLE ' Dept ' (
' DeptNo ' int (one) is not NULL,
' Deptname ' varchar (+) DEFAULT NULL,
' Location ' varchar (+) DEFAULT NULL,
PRIMARY KEY (' DeptNo ')
) Engine=innodb DEFAULT Charset=utf8;
Table Name: EMP
CREATE TABLE ' emp ' (
' Empno ' int (one) is not NULL,
' ename ' varchar (+) DEFAULT NULL,
' HireDate ' Date DEFAULT NULL,
' Job ' varchar (+) DEFAULT NULL,
' Sal ' Double DEFAULT NULL,
' Mgr ' varchar (+) DEFAULT NULL,
' Comm ' varchar (+) DEFAULT NULL,
' Deptno ' int (one) DEFAULT NULL,
PRIMARY KEY (' empno ')
) Engine=innodb DEFAULT Charset=utf8;
Second, add Jar package
1. Create a Lib directory on the project
/lib
2. Add the jar in the Lib directory
Junit-4.10.jar
Mybatis-3.2.2.jar
Mysql-connector-java-5.1.10-bin.jar
Third, add the configuration file
1. Create the Conf directory on the project
/conf
2. Add a configuration file under the Conf directory
Configuration file name: Mybatis-config.xml
Configuration file Contents:
<?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>
Iv. Creating an Entity class
1. Create the package in the SRC directory
Package Name: Cn.jbit.mybatis092901.domain
2. Create an entity class under a package
Class Name: Dept.java
Content:
public class Dept implements Serializable {
Private Integer deptno;//Department number
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 number
Private Integer EmpNo;
Employee onboarding Hours
Private Date hiredate;
Staff positions
Private String job;
Employee wages
Private Double salary;
Manager number
Private Integer Mgr;
Bonus
Private Double comm;
Department number
Private Integer DeptNo;
Omit Get and set
}
Five, durable layer design
1. Interface design
1). Create a package in the SRC directory
Package Name: Cn.jbit.mybatis092901.dao
2). Create an interface under a package
Interface Name: Iempdao.java
Contents of the interface:
Public interface Iempdao {
/**
* Use where
* @param emp
* @return
*/
Public list<emp> findempbyexamplewhere (EMP emp);
}
Vi. adding related mapping files
1. Add a configuration file under Conf
Mapping file name: Empdaomapper.xml
Map File Contents:
<?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" >
<resultmap id= "Empresultmap" type= "CN.JBIT.MYBATIS092901.DOMAIN.EMP" >
<id property= "EmpNo" column= "EmpNo"/>
<result property= "EmpName" column= "ename"/>
<result property= "HireDate" column= "HireDate"/>
<result property= "Job" column= "job"/>
<result property= "Salary" column= "Sal"/>
<result property= "comm" column= "comm"/>
</resultMap>
<!--
Using where for dynamic SQL stitching
-
<select id= "Findempbyexamplewhere" parametertype= "Emp" resultmap= "Empresultmap" >
SELECT * from EMP
<where>
<if test = "DeptNo! = null" >
and deptno= #{deptno}
</if>
<if test = "EmpName! = null" >
and ename like #{empname}
</if>
</where>
</select>
</mapper>
Seven. Persistence layer Implementation Class design
1). Create the package in the SRC directory
Package Name: Cn.jbit.mybatis092901.dao.impl
2). Create the implementation class under the package
Implementation class Name: Empdaoimpl.java
Implement the contents of the class:
/**
* In order to use the code to look clear, Sqlsession's acquisition uses the tool class
*/
@Override
public void Insertemp (EMP 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 ();
list<emp> emplist = new ArrayList ();
Emplist = Session.selectlist ("cn.jbit.mybatis092901.dao.IEmpDao.findEmpByExampleIf", EMP);
} catch (Exception E1) {
E1.printstacktrace ();
}finally {
Session.close ();
}
}
Viii. Adding a mapping file reference in a configuration file
1. Add mapper in the Mybatis-fonfig.xml file
<mappers>
<mapper resource= "Empdaomapper.xml"/>
</mappers>
Nine, test operation
1. Create the test catalog in the project
/test
2. Create a package in the test directory
Cn.jbit.mybatis092901.dao
3. Create a test class under a package
Class Name: Empdaotest.java
Content:
public class Empdaotest {
Persistence layer implementations for employee classes
private static Iempdao Empdao = new Empdaoimpl ();
/**
* Test where
*/
@Test
public void Testwhere () {
EMP emp = new EMP ();
Emp.setdeptno (9);
Emp.setempname ("Zhang Ping U");
list<emp> emps = Empdao.findempbyexamplewhere (EMP);
Printemplist (Emps);
}
private void Printemplist (list<emp> emplist) {
for (Emp emp:emplist) {
Printemp (EMP);
}
}
}

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

Mybatis-using where to dynamically stitch SQL

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.