First, Getmapper () interface
Parsing: getmapper () interface Idept.class defines an interface,
Mount a method that is not implemented, special, borrow the building any method must be consistent with the id attribute in the small configuration
By proxy: Generate the implementation class name of the interface, maintain the name at the bottom of the MyBatis $ $Dept _abc,selectdeptbyno ()
is equivalent to a strong type
Eg
First step: Define an interface in Cn.happy.dao
Package Cn.happy.dao;
Import java.util.List;
Import cn.happy.entity.Dept;
Public interface Ideptdao {
//view all---------getalldept to be the same as the ID inside the small configuration public
list<dept> getalldept ();
}
Step Two: Idept.xml configuration small configuration
Parsing: The id attribute in the Select is the same as the interface method name inside the interface; The namespace property package name for Mapper is the Cn.happy.dao.IDeptDao interface
<?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.happy.dao.IDeptDao" >
<select id= "Getalldept" Cn.happy.entity.Dept ">
select * from Dept
</select>
</mapper>
Step Three: Test class
Parsing: There are two ways to view all of the information
1) session.selectlist ("cn.happy.dao.IDeptDao.getAllDept");-------entity classes. The ID name ============ string inside the small configuration
2) Ideptdao mapper = Session.getmapper (ideptdao.class); equivalent to implementation class, Getmapper is a strongly typed
01 View all information Getmapper () The method name of the interface class is the same as the ID of the small configuration
@Test public
void Testselectall () {
sqlsession session = Factory.opensession ();
Use a weakly typed ======== entity class. The ID name inside the small configuration ============ string
/*list<dept> List = Session.selectlist (" Cn.happy.dao.IDeptDao.getAllDept ");
for (Dept dept:list) {
System.out.println (Dept.getdeptname ());
} ///
using Getmapper method hibernate help us to broker an interface in memory the implementation class = = = = = = = = = = =
//mapper is an implementation class object
Ideptdao mapper = Session.getmapper (ideptdao.class);
list<dept> list = Mapper.getalldept ();
for (Dept dept:list) {
System.out.println (Dept.getdeptname ());
}
Fourth step: Full text unified with a large configuration
<?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> <!--alias alias small Configuration The attribute value of type is changed to alias--> <typeAliases> <typealias type= " Cn.resultMap.enetity.Emp "alias=" Emp "/> </typeAliases> <environments default=" Development "> < Environment id= "Development" > <transactionmanager type= "JDBC"/> <datasource type= "Pooled" > < Property name= "Driver" value= "Oracle.jdbc.OracleDriver"/> <property name= "url" value= "jdbc:oracle:thin:@ Localhost:1521:orcl "/> <property name= username" value= "sa"/> <property name= "password" value= "1"/> &
Lt;/datasource> </environment> </environments> <!--mapping file: Describes the corresponding relationship between an entity and a database table--> <mappers> <mapper resource= "Cn/resultmap/enetity/emp.xml"/> </mappers> </configuration>
Second, Resultmap label
Parsing: The scenario used is when the attributes of the entity class do not match the database, the attributes of the entity class and the database must be resultmap. (previously used entity classes)
EG search all employees, and subordinate departments
First step: Create an interface
Package Cn.resultMap.dao;
Import java.util.List;
Import CN.RESULTMAP.ENETITY.EMP;
Public interface Iempdao {
//Retrieve all employees, and subordinate department public
List<emp> Getallemps ();
}
Step Two: Configure the properties inside the small configuration
Parsing: One side of the employee's point of view, each of the attributes of a party embedding a association is associated (if the association is removed it is the basis of the Resultmap)
<?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.resultMap.dao.IEmpDao" >
<resultmap type= "CN.RESULTMAP.ENETITY.EMP" id = "Empmap" >
<id property= "empId" column= "EmpId"/> "<result" property= "EmpName"
column= "EmpName"/ >
<result property= "empcity" column= "empcity"/>
<!--an employee with more than one side of the angle, use association-->
<association property= "dept" javatype= "cn.resultMap.enetity.Dept" >
<result property= "Deptname" "column=" Deptname "/> <result property=" DeptNo "column=" DeptNo "/>
</association>
</resultMap>
<select id= "Getallemps" resultmap= "Empmap" >
select e.*,d.* from Emp e,dept d
where E.deptno=d.deptno
</select>
</mapper>
Step Three: Test class
Resultmap: The property name of the entity and the field name of the table are guaranteed to be consistent with Resultmap
//If the report nullexception view a small configuration of the Mapping association RESULTMAP configure
@Test public
Void Testallemp () {
sqlsession session=factory.opensession ();
Iempdao mapper = Session.getmapper (iempdao.class);
list<emp> allemps = Mapper.getallemps ();
for (Emp emp:allemps) {
System.out.println (emp.getempname () + "\ \ Subordinate Department" +emp.getdept (). Getdeptname ())
;
Session.close ();
}
Fourth step: Introducing a small configuration in a large configuration
Third, the extraction of SQL columns
Parsing: SQL tags simplify code in small configuration to write
<!--the use of SQL tags-->
<sql id= "Columns" >
d.deptno,d.deptname
</sql>
<!-- Use of SQL tags-->
<select id= "Getallemps" resultmap= "Empmap" >
Select e.*,<include refid= "Columns"/ >from Emp e,dept D
where E.deptno=d.deptno
</select>
Four, alias aliases
Parsing: Write on a large configuration so that you can reference aliases in small configurations
<!--alias alias small configuration of the property value of the type is changed to alias-->
<typeAliases>
<typealias type= "cn.resultMap.enetity.Emp "alias=" emp/>
</typeAliases>
V. Dynamic operation
Parsing: The elements used to implement dynamic SQL include:
If
choose (when,otherwise)
where
Eg View the people in the Beijing city
First step: interface
Package Cn.resultMap.dao;
Import java.util.List;
Import CN.RESULTMAP.ENETITY.EMP;
Public interface Iempdao {
//Retrieve all employees, and subordinate department public
List<emp> Getallemps ();
}
Step two: Small match <?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.resultMap.dao.IEmpDao" > <resultmap type= "cn.resultMap.enetity.Emp" id= "Empmap" > < ID property= "empId" column= "EmpId"/> <result property= "EmpName" column= "EmpName"/> <result property= " Empcity "column=" empcity/> <!--one side of the employee's angle, the individual attributes of the embedded side Please use association--> <association property= "Dept" Javatype= "Cn.resultMap.enetity.Dept" > <result property= "deptname" column= "Deptname"/> <result "DeptNo" column= "DeptNo"/> </association> </resultMap> <select id= "Getallemps" resultmap= "Empmap" > select e.*,d.* from Emp e,dept D where E.deptno=d.deptno </select> <!--query Dynamic query--> <select id= "Testalle Mpbuselect "parametertype=" cn.resultMap.enetity.Emp "resulttype=" CN.RESULTMAP.ENETITY.EMP "> select * from Emp <
where> <if test= "Empid!=null" > and Empid=#{empid} </if><if test= "Empname!=null" > and Empname=#{empname} </if> <if test= "Empcity!=null" > and empcity=#{ empcity} </if> </where> </select> </mapper>
Step Three: Test
Dynamic Query
@Test public
void Testselect () {
sqlsession session=factory.opensession ();
EMP emp=new EMP ();
Emp.setempname ("331");
Emp.setempcity ("sh");
list<emp> list = session.selectlist ("Cn.resultMap.dao.IEmpDao.testAllEmpBuSelect", Emp);
for (Emp emps:list) {
System.out.println (Emps.getempname ());
}
Session.close ();
}
Fourth step: Introducing a small configuration in a large configuration
EG Modify Department information
First step: interface
Step Two: Small configuration
<?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.resultMap.dao.IDeptDao" >
<resultmap type= "Cn.happy.entity.Dept" Deptresultmap ">
<id property=" DeptNo "column=" DeptNo "/>" <result property= "Deptname" column= "
Deptname "/>
</resultMap>
<select id= getalldept" resultmap= "Deptresultmap" >
select D. *,e.* from Dept d,emp e
where D.deptno=e.deptno and D.deptno=#{deptno}
</select>
<!--modify Dynamic queries- >
<select id= "testupdate" parametertype= "int" resulttype= "cn.resultMap.enetity.Dept" >
update Dept
<set>
<if test= "Deptno!=null" >
deptno=#{deptno},
</if>
<if test= "Deptname!=null" >
deptname=#{deptname},
</if>
</set>
where deptno=#{ DeptNo}
</select>
Step Three: Test
/**
* Dynamic Modification
*/
@Test public
void Testupdate () {
sqlsession session=factory.opensession ();
Dept dept=new Dept ();
Dept.setdeptname ("Finance Department");
Dept.setdeptno (1);
int count = session.update ("Cn.resultMap.dao.IDeptDao.testUpdate", dept);
Session.commit ();
System.out.println (count);
Session.close ();
}
The above is a small set to introduce the detailed MyBatis getmapper () interface, Resultmap tags, alias aliases, as far as possible to extract SQL column, dynamic operation, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!