mybatis-spring注解标识dao层

如果使用mybatis作为持久层的开发:

1、新建一个注解用作dao扫描

package com.test.base.database;
/**
 * 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描。
 * 
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Component
public @interface SecurityBatisRepository {
	String value() default "";
}

2、新建一个dao接口,并添加注解@SecurityBatisRepository

@SecurityBatisRepository
public interface YhMmxxDao {
    int delete(String yhid);

    int insert(YhMmxx yhMmxx);

    YhMmxx select(String yhid);

    List<YhMmxx> selectByYhMmxx(YhMmxx yhMmxx);

    int updateByYhMmxx(YhMmxx yhMmxx);

}

3、在datasource-mybatis.xml文件中配置MapperScannerConfigurer这个bean的属性,启动spring的时候扫描@SecurityBatisRepository

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<description>Spring 数据库配置 Config</description>
	<bean id="security_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:/mybatis/test-infrastructure-config.xml</value>
		</property>
		<property name="dataSource" ref="security_dataSource" />
		<property name="mapperLocations" value="classpath*:/mybatis/*Mapper.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.test.**.dao" />
		<property name="annotationClass" value="com.test.base.database.SecurityBatisRepository" />
		<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> sqlSessionFactory注入会导致spring 
			先加载sqlSessionFactory,再加载properties配置文件,这样jdbc的变量不会被替换 改用sqlSessionFactoryBeanName,用名称进行注入 -->
		<property name="sqlSessionFactoryBeanName" value="security_sqlSessionFactory" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="gov.test.**.dao" />
		<property name="annotationClass" value="com.test.base.database.SecurityBatisRepository" />
		<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> sqlSessionFactory注入会导致spring 
			先加载sqlSessionFactory,再加载properties配置文件,这样jdbc的变量不会被替换 改用sqlSessionFactoryBeanName,用名称进行注入 -->
		<property name="sqlSessionFactoryBeanName" value="security_sqlSessionFactory" />
	</bean>
	<bean id="security_txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="security_dataSource" />
	</bean>
	<bean id="security_sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="security_sqlSessionFactory" />
	</bean>
	<bean id="security_jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource">
			<ref bean="security_dataSource" />
		</property>
	</bean>
	<tx:annotation-driven transaction-manager="security_txManager"
		proxy-target-class="true" />
</beans>

4、新建一个mapper来实现dao接口

<?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.*****.YhMmxxDao">

    <sql id="columns">
    id,
    name
  </sql>
    <sql id="properties">
    #{id},
    #{name}
  </sql>
 <select id="findAllListed" resultMap="testDataResult" parameterType="TestData">
        SELECT *
        FROM test_test
        <where>
            <if test="id!=null and id!=0">
                and id=#{id}
            </if>
            <if test="name!=null and name!=''">
               and name=#{name}
            </if>
        </where>
     </select>
</mapper>

如果没有使用mybatis作为dao层的开发,标识dao层可以直接使用spring的@Component注解

@Component("testMongoDao")
public class testMongoDao extends MongodbBaseDAOImpl<testVo>{

   /**
	 * 创建
	 * @param vo
	 */
	public void addTestVo(testVo vo){
		if(vo != null){
			super.insert(vo);
		}
	}
}
参考: Mybatis实现DAO层------------自动实现dao接口


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值