Learning Notes spring and mybatis Integrate Dynamic Agent Knowledge Point Arrangement

1.mybatis multi-table query one-to-one

One-to-one (one containing another instance)

<!-- One-to-one Association query-resultMap -->
	<resultMap type="order" id="order_user_map">
		<!-- id Tags are used to bind primary keys -->
		<id property="id" column="id"/>
		<!-- Use result Binding common fields -->
		<result property="userId" column="user_id"/>
		<result property="number" column="number"/>
		<result property="createtime" column="createtime"/>
		<result property="note" column="note"/>
		
		<!-- association:Configure one-to-one associations
			 property:Binding user attributes
			 javaType:Attribute data type, supporting aliases
		-->
		<association property="user" javaType="com.itheima.mybatis.pojo.User">
			<id property="id" column="user_id"/>
			
			<result property="username" column="username"/>
			<result property="address" column="address"/>
			<result property="sex" column="sex"/>
		</association>
	</resultMap>
	<!-- One-to-one Association query-Use resultMap -->
	<select id="getOrderUser2" resultMap="order_user_map">
		SELECT
		  o.`id`,
		  o.`user_id`,
		  o.`number`,
		  o.`createtime`,
		  o.`note`,
		  u.`username`,
		  u.`address`,
		  u.`sex`
		FROM `order` o
		LEFT JOIN `user` u
		ON u.id = o.`user_id`
	</select>

2.mybatis multi-table one-to-many

Implementing one-to-many Association queries (one contains multiple instances)

The source code is as follows:

  • userMapper.xml source code
<?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="com.holt.springmybatis.mapper.UserMapper">

<sql id="head">
	SELECT 	id, 
		username, 
		birthday, 
		sex, 
		address
		from 
		mybatis.user
	</sql>
	<select id="getUserById" parameterType="int" resultType="com.holt.springmybatis.pojo.User">
	 <include refid="head"/>

	<where>
		id=#{id}
	</where>	 	
	</select>
	
	
	
	<!-- resultMap One to many  -->
	<resultMap type="com.holt.springmybatis.pojo.User" id="userOder_Map">
		<id property="id" column="id"/>
		<result property="username" column="username"/>
		<result property="birthday" column="birthday" />
		<result property="address" column="address" />
		<result property="sex" column="sex" />
		
		
	<!-- 	collection:Configuring one-to-many relationships
			 property:User following order attribute
			 ofType:property Data type, supporting aliases
	 -->
		<collection property="orders" ofType="com.holt.springmybatis.pojo.Order">
			<!-- id Tags are used to bind primary keys -->
			<id property="id" column="oid"/>
			<!-- Use result Binding common fields -->
			<result property="userId" column="id"/>
			<result property="number" column="number"/>
			<result property="createtime" column="createtime"/>
		</collection>
	</resultMap>
	<!-- Associated one-to-many queries  -->
	<select id="getUserOrder" resultMap="userOder_Map">
		SELECT
		u.`id`,
		u.`username`,
		u.`birthday`,
		u.`sex`,
		u.`address`,
		o.`id` oid,
		o.`number`,
		o.`createtime`
		FROM `user` u
		LEFT JOIN `order` o
		ON o.`user_id` = u.`id`
	</select>

</mapper>

  • applicationContext.xml source code
<!--Loading number jdbc configuration file  -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
<!--Configure connection pool  -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		
	</bean>
	
<!--To configure sqlSessionFactory  -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
	<!--Configure Alias Pack Scan  -->
	<!-- <property name="typeAliasesPackage" value="com.holt.springmybatis.pojo"/> -->
	</bean>
	
	
	<!--The first way of dynamic proxy  -->
	<bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
		<property name="mapperInterface" value="com.holt.springmybatis.mapper.UserMapper"/>
	</bean>
		
		
	<!-- basePackage: Configuration mapping wrapper scan for multiple packages","or";"Separate -->
	<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.holt.springmybatis.mapper"/>
	</bean>
	 -->
  • Be careful:
    There are two ways to configure dynamic agents: one is to configure them in two ways
    The first is:
    1). Implementation with org.mybatis.spring.mapper.MapperFactoryBean
    This is a direct inheritance of the MapperFactoryBean interface.
<bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
		<property name="mapperInterface" value="com.holt.springmybatis.mapper.UserMapper"/>
	</bean>

2. Configure mapper.MapperFactoryBean first and then inherit it

<!-- To configure MapperFactoryBean-->
	<bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
     	<property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

    <!-- inherit MapperFactoryBean -->
    <bean parent="baseMapper">
      <property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper" />
   	</bean>

Second: Packet scanning is simple and fast (recommended)

<!-- basePackage: Configuration mapping wrapper scan for multiple packages","or";"Separate -->
	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.holt.springmybatis.mapper"/>
	</bean>
``s`

Keywords: Mybatis Spring JDBC xml

Added by project168 on Tue, 01 Oct 2019 19:47:02 +0300