通用Mapper Spring Mvc

項(xiàng)目地址:https://github.com/mybatis/mybatis-3.git
1、使用
添加Maven依賴

<!-- Spring Mvc使用 -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper</artifactId>
    <version>最新版本</version>
</dependency>

TestExampleMapper

public interface TestExampleMapper extends Mapper<TestExample> {
}

實(shí)體類注解

public class TestExample {
    @Id
    private Integer id;
    @Column
    private String  name;
    private String logo;
    //省略setter和getter方法
}

簡單用法示例

@Resource
private TestExampleMapper  mapper;

Condition condition = new Condition(TestExample .class);
condition .createCriteria().andGreaterThan("id", 100);
List<TestExample > exampleList = mapper.selectByCondition(condition );

spring 配置

<!-- Mybatis 配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="configLocation" value="classpath:conf/mybatis-config.xml"/>
      <!-- 配置mapper.xml文件所在位置-->
      <property name="mapperLocations" value="classpath*:mapper/**/*.xml"/>
      <!-- 需要啟動別名model所在包位置 -->
      <property name="typeAliasesPackage" value="com.orm.model,com.common.dto"/>
      <!--mybatis插件配置-->
      <property name="plugins">
          <array>
              <bean class="com.github.pagehelper.PageHelper">
                  <property name="properties">
                      <value>
                          dialect=mysql
                      </value>
                  </property>
              </bean>
          </array>
      </property>
</bean>
<!--使用通用Mapper 重寫的 MapperScannerConfigurer-->
 <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.orm.mapper"/>
        <property name="properties">
            <value>
                mappers=com.orm.Mapper
            </value>
        </property>
    </bean>

2、自定義

import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;

//定制接口
public interface Mapper<T>
        extends
        BaseMapper<T>,
        ConditionMapper<T>,
        IdsMapper<T>,
        InsertListMapper<T> {
}

//引用
@Autowired
protected Mapper<T> mapper;
//調(diào)用InsertListMapper的insertList方法
public void save(T model){
  mapper.insertList(models);
}

//tk的InsertListMapper接口源碼
public interface InsertListMapper<T> {
    @Options(
        useGeneratedKeys = true,
        keyProperty = "id"
    )
    @InsertProvider(
        type = SpecialProvider.class,
        method = "dynamicSQL"
    )
    int insertList(List<T> var1);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容