SSM SSM] [MyBatis framework of: two mapping schemes mapper mappers

mappers mapper two mapping schemes:

To define the mapping of SQL statements must first tell MyBatis where to find them. Java does not provide a good way in this regard, so the best way is to tell MyBatis where to find the map file mappers mapper. mappers mapper mapping scheme provides two, one is designated corresponding mapper .xml file directly, two interfaces are specified with the associated mapper .xml. E.g:

<mappers>
  <!--指定mapper.xml的相对路径-->
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  <!--指定mapper.xml的绝对路径-->
  <mapper url="file:///var/mappers/AuthorMapper.xml"/>
</mappers>
<mappers>
  <!--指定相关联的接口的全限定路径-->
  <mapper class="org.mybatis.builder.AuthorMapper"/>
  <!--指定包下所有相关联的接口-->
  <package name="org.mybatis.builder"/>
</mappers>

All the foregoing first embodiment is used, a first embodiment of the DAO for conventional packages. Here's what the second option, the second option is Mybatis recommended solution because it has many advantages, the most notable advantages:

  • Sql command is invoked, the method of direct call interface, automatic association mapper.xml sql command file corresponding to the internal program, so do not write lengthy namespace;
  • Only a parameter passed in the first embodiment, the call sql command, if a plurality of parameters have to be passed with which set of objects or packages, while the second scenario since the call is a method declared in the interface Since it is a method that can be passed in a number of parameters.

But the second scenario has the following two requirements:

  • Interfaces and mapping files must be in the same package;
  • Interfaces and mapping files to remove the file suffix the file name must be identical.

The second option for dynamic proxy DAO development, is Mybatis recommended protocol for dynamic proxy interfaces and mapping file also has several requirements:

  • The method name must be declared in the interface and mapping file corresponding sql id of the same;
  • The method must be declared in the interface parameters and the corresponding mapping file consistent with the sql parameterType;
  • Declared in the interface method return value must be consistent with the sql resultType and corresponding mapping file.

Example:

1, construction of the table in MySQl, the src directory under the appropriate web project, the new package with the entity class:
here with a previous blog of flower table, the package name "cn.jingpengchong.pojo" and Flower under the package. the Java
2, in the src directory, create a file package and mapper.xml:
package name "cn.jingpengchong.mapper", the file "FlowerMapper.xml" file as follows:

<?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.jingpengchong.mapper.FlowerMapper">
	<!-- 根据价格和产地查找花 -->
	<select id="selByPriPro" resultType="flower">
		<!-- 用#{0}/#{param1}接收第一个参数,用#{1}/#{param2}接收第一个参数 -->
		select * from flower where price = #{0} and production = #{1}
	</select>
</mapper>

3, in the src directory, create XML mapping configuration file:
file "mybatis.xml" file as follows:

<?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>
	<!--给Flower类设置别名,这样在声明该类类型时直接写“Flower”或“flower”即可-->
	<typeAliases>
		<package name="cn.jingpengchong.pojo"/>
	</typeAliases>
	<environments default="default">
		<environment id="default">
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssm"/>
				<property name="username" value="root"/>
				<property name="password" value="1234"/>
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<!--将该包下文件名相同的接口的方法和xml文件的sql命令一一关联起来-->
		<package name="cn.jingpengchong.mapper"/>
	</mappers>
</configuration>

4, the file is located under the package mapper.xml new interface, the interface name to the file name with the corresponding mapper.xml same:
the package "cn.jingpengchong.mapper" under the new Interface "FlowerMapper.java":

package cn.jingpengchong.mapper;

import java.util.List;

import cn.jingpengchong.pojo.Flower;

public interface FlowerMapper {
	/**
	 * 根据价格和产地查询花
	 * selByPriPro:方法名要与对应xml文件中对应方法的id相同
	 * pri:价格price
	 * pro:产地production
	 */
	List<Flower> selByPriPro(double pri, String pro);

}

5, a new test class:
here created a "Test.java" file in the "cn.jingpengchong.test" package:

package cn.jingpengchong.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.jingpengchong.mapper.FlowerMapper;
import cn.jingpengchong.pojo.Flower;

public class Test {

	public static void main(String[] args) throws IOException {

		InputStream is = Resources.getResourceAsStream("mybatis.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		FlowerMapper mapper = session.getMapper(FlowerMapper.class);
		List<Flower> list = mapper.selByPriPro(4, "南美阿根廷");
		for (Flower flower : list) {
			System.out.println(flower);
		}
	}
}

Results are as follows:
Here Insert Picture Description

Annex: Parameter mapper.xml SQL command file received

1, # {0}, {# 1}, {2} # ...: ... for acquiring parameter 1,2,3;
2, the param1 {#} # {param2}, ...} # {Param3: with 1,2,3 ... to the acquisition parameter;
3, above $ # {} {} is not replaced, because in addition to getting a property value set in Map elements or objects, $ {} the contents will be sql as a simple string concatenation;
4, can be used "@Param ()" encapsulating annotation parameters set to Map, if you want to acquire the following method parameters:

<select id="selByPriPro" resultType="flower">
	select * from flower where price = #{price} and production = #{production}
</select>

The method declared in the interface should be added corresponding notes:

List<Flower> selByPriPro(@Param("price") double pri, @Param("production") String pro);

You are finished running the test class is still correct, the results are as follows:
Here Insert Picture Description
in this way is the annotation passed as a parameter key, the back of the package as a parameter value into a Map collection passed mapper.xml file, but even so, the $ # {} to {} still go wrong:
Here Insert Picture Description
you can see when receiving a single string parameter in the log / double quote is missing, which is in development by # {} instead of {} $ reason!

Published 128 original articles · won praise 17 · views 2737

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104065578