The spring automatic proxy of mybatis function completes the implementation class function of dao


When writing the package, it will be divided into dao layer, service layer, action layer, and implementation class layer.

With mybatis, the implementation class of the dao interface layer does not need to be written, and the spring agent is completed. The steps are as follows

In the spring configuration file The spring.xml configuration is as follows:

1. spring.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="

 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           
      ">
      
      <!-- 1. Configure c3p0 connection pool -->
      <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass" value="Oracle.jdbc.driver.OracleDriver"/>
      <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
      <property name="user" value="scm"/>
      <property name="password" value="666"/>
      </bean>
      
      <!-- 2. Configure sqlsession instead of native mybatisUtil tool class -->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <!-- Load configuration file mybatis.xml -->
      <property name="configLocation" value="classpath:mybatis.xml"/>
      <!-- Import data resource-->
      <property name="dataSource" ref="comboPooledDataSource"/>
      </bean>
      
      <!-- 3. mybatis transaction manager, the bottom layer is jdbc -->
      <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!-- Import data source -->
      <property name="dataSource" ref="comboPooledDataSource"/>
      </bean>


      <!-- Since the namespace of the interface is used, there is no need for the implementation class of dao, nor the sqlSessionTemplate-->
      <!--<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>

 </bean>
       -->
      
      <!-- 4. Configure transaction notifications, how to manage things-->
      <tx:advice id="tx" transaction-manager="dataSourceTransactionManager">
      <tx:attributes>
      <!-- rollback-for="Exception" Rollback exception -->
      <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
      <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
      <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
      <!-- If there is a transaction, it will be executed in the current transaction, and if there is no current transaction, it will be executed without a transaction -->
      <tx:method name="*" propagation="SUPPORTS"/>
      </tx:attributes>
      </tx:advice>
      
      <!-- 5. Configure the aspect of things aop, which methods to intercept -->
      <aop:config>
      <aop:pointcut expression="execution(* com.coffee.scm.service.impl.*.*(..))" id="pointcut"/>
      <aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
      </aop:config>
      
      <!-- register dao -->
     <!--  <bean id="deptDao" class="com.coffee.scm.dao.impl.DeptDao">
      <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
      </bean>   -->
      <!-- Register action (annotation), which contains service, service is annotated, scan -->
      <!-- <context:component-scan base-package="*"/> -->
      
      <!-- Scan to filter out controllers, because they are configured in springmvc, otherwise there will be problems -->
      <context:component-scan base-package="com.coffee">
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      </context:component-scan>
      <!-- Configure the dao interface mapping, and then you don't need to write the dao implementation class
            Configure the converter, for the interface class under the package (including subpackages) set by basePackage, if it is defined in the Mapper.xml file,
               It will be converted into spring's BEAN, and the interface instance can be injected by @Autowired at the place of call
           The implementation of the interface is completed by spring itself
         value="com.coffee.scm.dao" mapping interface full path
         
       -->
      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
      <property name="basePackage" value="com.coffee.scm.dao"></property>
      </bean>
      
      <!-- Notify springioc annotation function-->
      <context:annotation-config />
      
</beans>



2.mapper.xml: is the mapping file corresponding to the entity class

<?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">
<!-- namespace defines the full path of the interface, only need to define the method in the interface
There is no need for the implementation class of the dao layer, and then write the sql statement.
principle:
 -->
<mapper namespace="com.coffee.scm.dao.IDeptDao">
<resultMap type="dept" id="deptResultMap">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
<result property="deptAddress" column="dept_address"/>
</resultMap>

<select id="selectDept" parameterType="Integer" resultMap="deptResultMap">
<!--Parameter writing#{deptID} -->
select * from dept where dept_id =#{deptId}
</select>


<insert id="insert" parameterType="dept">
insert into dept values(#{deptId},#{deptName},#{deptAddress})
</insert>

</mapper>


3. Service implementation layer

package com.coffee.scm.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.coffee.scm.dao.IDeptDao;
import com.coffee.scm.entity.Dept;
import com.coffee.scm.service.IDeptService;


@Service
public class DeptService implements IDeptService {


// This annotation is configured according to the type, just find the type, add this annotation, and then configure the interface mapping to remove the interface implementation of dao, which is completed by spring automatic proxy
@Autowired
private IDeptDao deptDao;


/**
* Insert department information, if an exception is encountered, it will be rolled back, because rollback-for="Exception" is configured in the transaction notification configuration of spring.xml
*/
@Override
public void insertDept(Dept dept) throws Exception {
try {
deptDao.insert(dept);


} catch (Exception e) {
throw new Exception(e);
}
}


}


Note: The method name defined by the dao interface layer should be consistent with the id of the sql tag of mapper.xml

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326436781&siteId=291194637