通用Mapper使用(Maven+SSM)


1.在pom.xml中導入jar包(引入Maven依賴)

              <!-- 通用mapper -->

              <dependency>

                     <groupId>com.github.abel533</groupId>

                     <artifactId>mapper</artifactId>

                     <version>2.3.4</version>

              </dependency>

 

2.在resources源文件中創建mybatis.xml配置通用mapper的攔截器

<?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>

     <plugins>

              <!-- 通用mapper -->

              <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">

                     <!--主鍵自增回寫方法,默認值MYSQL,詳細說明請看文檔 -->

                     <property name="IDENTITY" value="MYSQL" />

                     <!--通用Mapper接口,多個通用接口用逗號隔開 -->

                     <property name="mappers" value="com.github.abel533.mapper.Mapper" />

              </plugin>

       </plugins>

</configuration>

 

3.在mybatis配置文件(applicationContent-mybatis.xml)中引入mybatis.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:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

      

       <!-- 配置SqlSessionFactory -->

       <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

              <!-- 數據源 -->

              <property name="dataSource" ref="dataSource"/>

              <!-- mapper映射文件 -->

              <property name="mapperLocations" value="classpath:com/zhiyou100/kfs/mapper/*.xml"/>

             

              <!-- 引入mybatis文件 -->

              <property name="configLocation" value="classpath:mybatis.xml"/>

             

              <!-- pagehelp攔截器 -->

              <property name="plugins">

                  <array>

                    <bean class="com.github.pagehelper.PageInterceptor">

                      <property name="properties">

                        <!--使用下面的方式配置參數,一行配置一個 -->

                        <value>

                          reasonable=true

                        </value>

                      </property>

                    </bean>

                  </array>

              </property>

       </bean>

      

       <!-- 接口生成實現類:用MapperScannerConfigurer使dao接口的包和SqlSessionFactory對象中的mapper對應 -->

       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

              <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>

              <property name="basePackage" value="com.zhiyou100.kfs.dao"/>

       </bean>

 

</beans>

 

4.實體類要寫相應的注解來對應數據庫表和字段

package com.zhiyou100.kfs.bean;

 

import javax.persistence.Column;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

 

@Table(name="tbl_user")//@Table:設置對應的表格;name:表格名;

public class User {

       @Id//@Id:這個屬性是主鍵

       @GeneratedValue(strategy=GenerationType.IDENTITY)//設置MYSQL數據庫主鍵遞增(其他數據庫有相應的字符串表示遞增)

    private Integer userId;

       @Column(name="user_name")//這個屬性userName和數據庫user_name字段對應(屬性名為userName會自動對應數據庫中的user_name,駝峰命名大寫會在數據庫轉化為下划線,若屬性名轉化后和數據字段名對應則@Column可以不寫)

    private String userName;

    private Integer userAge;

    public Integer getUserId() {

        return userId;

    }

    public void setUserId(Integer userId) {

        this.userId = userId;

    }

    public String getUserName() {

        return userName;

    }

    public void setUserName(String userName) {

        this.userName = userName == null ? null : userName.trim();

    }

    public Integer getUserAge() {

        return userAge;

    }

    public void setUserAge(Integer userAge) {

        this.userAge = userAge;

    }

       @Override

       public String toString() {

              return "User [userId=" + userId + ", userName=" + userName + ", userAge=" + userAge + "]";

       }

}

 

5.dao接口繼承通用mapper的接口(mapper<T>,T表示泛型,輸入什么就變成什么),這個接口必須寫,后面可以加映射文件(如UserMapper.xml)和其他的方法,但映射文件的方法的id不能和通用mapper方法的方法名一致

package com.zhiyou100.kfs.dao;

 

import com.github.abel533.mapper.Mapper;

import com.zhiyou100.kfs.bean.User;

 

public interface UserMapper extends Mapper<User>{

   

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM