通用mapper

1.1使用parent工程控制版本

1.1.1創建Maven工程的pom工程qhrgd-parent來控制項目jar的版本.

1.1.2 pom.xml的配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.qhrgd</groupId>

<artifactId>qhrgd-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>pom</packaging>

 

<properties>

<junit.version>4.12</junit.version>

<spring.version>4.1.3.RELEASE</spring.version>

<mybatis.version>3.2.7</mybatis.version>

<mybatis.spring.version>1.2.2</mybatis.spring.version>

<mybatis.paginator.version>1.2.15</mybatis.paginator.version>

<mysql.version>5.1.32</mysql.version>

<slf4j.version>1.6.4</slf4j.version>

<jackson.version>2.4.2</jackson.version>

<druid.version>1.0.9</druid.version>

<jolbox.version>0.8.0.RELEASE</jolbox.version>

<jstl.version>1.2</jstl.version>

<servlet-api.version>2.5</servlet-api.version>

<jsp-api.version>2.0</jsp-api.version>

<joda-time.version>2.5</joda-time.version>

<commons-lang3.version>3.3.2</commons-lang3.version>

<commons-io.version>1.3.2</commons-io.version>

<commons-net.version>3.3</commons-net.version>

<pagehelper.version>3.7.5</pagehelper.version>

<mapper.version>2.3.4</mapper.version>

<jsqlparser.version>0.9.1</jsqlparser.version>

<commons-fileupload.version>1.3.1</commons-fileupload.version>

<commons-codec.version>1.9</commons-codec.version>

<jedis.version>2.7.2</jedis.version>

<solrj.version>4.10.3</solrj.version>

<dubbo.version>2.5.3</dubbo.version>

<zookeeper.version>3.4.7</zookeeper.version>

<zkclient.version>0.1</zkclient.version>

<activemq.version>5.12.0</activemq.version>

<freemarker.version>2.3.23</freemarker.version>

<quartz.version>2.2.2</quartz.version>

<uediter.version>1.1.1</uediter.version>

<json.version>20160212</json.version>

<fastdfs_client.version>1.25</fastdfs_client.version>

</properties>

 

<dependencyManagement>

<dependencies>

<!-- 單元測試 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>${junit.version}</version>

<scope>test</scope>

</dependency>

 

<!-- Spring -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring.version}</version>

</dependency>

 

<!-- Mybatis -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>${mybatis.spring.version}</version>

</dependency>

 

<!-- 通用Mapper -->

<dependency>

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

<artifactId>mapper</artifactId>

<version>${mapper.version}</version>

</dependency>

 

<!-- 分頁助手 -->

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

<version>${pagehelper.version}</version>

</dependency>

<dependency>

<groupId>com.github.jsqlparser</groupId>

<artifactId>jsqlparser</artifactId>

<version>${jsqlparser.version}</version>

</dependency>

 

<!-- MySql -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql.version}</version>

</dependency>

 

<!-- 日誌 -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>${slf4j.version}</version>

</dependency>

 

<!-- Jackson Json處理工具包 -->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>${jackson.version}</version>

</dependency>

 

<!-- 連接池 -->

<dependency>

<groupId>com.jolbox</groupId>

<artifactId>bonecp-spring</artifactId>

<version>${jolbox.version}</version>

</dependency>

 

<!-- JSP相關 -->

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>${jstl.version}</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>${servlet-api.version}</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jsp-api</artifactId>

<version>${jsp-api.version}</version>

<scope>provided</scope>

</dependency>

 

<!-- 時間操作組件 -->

<dependency>

<groupId>joda-time</groupId>

<artifactId>joda-time</artifactId>

<version>${joda-time.version}</version>

</dependency>

 

<!-- Apache工具組件 -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>${commons-lang3.version}</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-io</artifactId>

<version>${commons-io.version}</version>

</dependency>

 

<!-- 文件上傳組件 -->

<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>${commons-fileupload.version}</version>

</dependency>

 

<!-- dubbo相關 -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>dubbo</artifactId>

<version>${dubbo.version}</version>

<exclusions>

<exclusion>

<groupId>org.springframework</groupId>

<artifactId>spring</artifactId>

</exclusion>

<exclusion>

<groupId>org.jboss.netty</groupId>

<artifactId>netty</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>${zookeeper.version}</version>

</dependency>

<dependency>

<groupId>com.github.sgroschupf</groupId>

<artifactId>zkclient</artifactId>

<version>${zkclient.version}</version>

</dependency>

 

<!-- 加密解密 -->

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>${commons-codec.version}</version>

</dependency>

 

<!-- 定時任務Quartz -->

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>2.2.1</version>

</dependency>

 

<!-- ActiveMQ依賴 -->

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-all</artifactId>

<version>${activemq.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

<version>${spring.version}</version>

</dependency>

 

<!-- 靜態化freemarker -->

<dependency>

<groupId>org.freemarker</groupId>

<artifactId>freemarker</artifactId>

<version>${freemarker.version}</version>

</dependency>

 

<!-- Redis客戶端 -->

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>${jedis.version}</version>

</dependency>

 

<!-- solr客戶端 -->

<dependency>

<groupId>org.apache.solr</groupId>

<artifactId>solr-solrj</artifactId>

<version>${solrj.version}</version>

</dependency>

 

<!-- 百度編輯器 -->

<dependency>

<groupId>com.baidu</groupId>

<artifactId>ueditor</artifactId>

<version>${uediter.version}</version>

</dependency>

<dependency>

<groupId>org.json</groupId>

<artifactId>json</artifactId>

<version>${json.version}</version>

</dependency>

 

<dependency>

<groupId>com.alibaba.fastdfs</groupId>

<artifactId>fastdfs_client</artifactId>

<version>${fastdfs_client.version}</version>

</dependency>

 

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

<version>4.3.5</version>

</dependency>

</dependencies>

 

</dependencyManagement>

 

<build>

<finalName>${project.artifactId}</finalName>

<plugins>

<!-- 資源文件拷貝插件 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-resources-plugin</artifactId>

<version>2.7</version>

<configuration>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<!-- java編譯插件 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.2</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

</plugins>

<pluginManagement>

<plugins>

<!-- 配置Tomcat插件 -->

<plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat7-maven-plugin</artifactId>

<version>2.2</version>

</plugin>

</plugins>

</pluginManagement>

<resources>

<!-- 使用Maven部署的時候,xml和properties配置文件也一起部署到Tomcat -->

<resource>

<directory>src/main/java</directory>

<includes>

<include>**/*.properties</include>

<include>**/*.xml</include>

</includes>

<filtering>false</filtering>

</resource>

<!-- 默認是以下配置 -->

<resource>

<directory>src/main/resources</directory>

<includes>

<include>**/*.properties</include>

<include>**/*.xml</include>

</includes>

<filtering>false</filtering>

</resource>

</resources>

</build>

</project>

1.2 創建通用mapper工程

1.2.1 創建如圖所示的Maven工程

1.2.2 pom.xml的配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>

    <groupId>com.qhrgd</groupId>

    <artifactId>qhrgd-parent</artifactId>

    <version>0.0.1-SNAPSHOT</version>

  </parent>

  <groupId>qhrgd-mybatis-mapper</groupId>

  <artifactId>qhrgd-mybatis-mapper</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  

  <dependencies>

<!-- 單元測試 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

<!-- 通用mapper -->

<dependency>

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

<artifactId>mapper</artifactId>

</dependency>

<!-- Mybatis -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

</dependency>

 

<!-- MySql -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

 

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

</dependency>

<!-- Apache工具組件 -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-io</artifactId>

</dependency>

 

</dependencies>

</project>

1.2.3 使用通用Mapper實現CRUD

1.2.3.1 配置通用mepper插件

在resource目錄下,創建mybatis-config.xml配置文件

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

<!-- 在同一層目錄下,掃描數據庫的配置文件 -->

<properties resource="jdbc.properties" />

 

<!-- 配置通用Mapper插件 -->

<plugins>

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

<!-- 獲取數據源 -->

<environments default="development">

<environment id="development">

<transactionManager type="JDBC" />

<dataSource type="POOLED">

<property name="driver" value="${jdbc.driver}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

</dataSource>

</environment>

</environments>

<!-- 掃描配置完成的通用mapper -->

<mappers>

<!-- 基於XML的實現

<mapper resource="UserMapper.xml" />-->

<!-- 引入單個通用接口

<mapper class="com.qhr.mybatis.mapper.NewUserMapper"/>-->

<!-- 引用多個通用接口 -->

<package name="com.qhr.mapper"/>

</mappers>

</configuration>

數據庫的配置文件jdbc.properties的配置,如下:

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis-mapper?useUnicode=true&characterEncoding=utf8&autoReconnect=true

jdbc.username=root

jdbc.password=123456

1.2.3.2創建pojo進行數據庫的映射

package com.qhr.pojo;

 

import javax.persistence.Column;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

//表名

@Table(name="user")

public class User {

//主鍵

@Id

//自增長

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

//字段,倘若括號裏面什麼都沒寫,數據相應的要以username爲字段名

@Column(name="username")

private String username;

@Column(name="password")

private String password;

@Column(name="phone")

private String phone;

@Column(name="email")

private String email;

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

@Override

public String toString() {

return "User [id=" + id + ", username=" + username + ", password=" + password + ", phone=" + phone + ", email="

+ email + "]";

}

 

}

 

1.2.3.3 創建通用mapper的接口

package com.qhr.mapper;

 

import com.github.abel533.mapper.Mapper;

import com.qhr.pojo.User;

 

public interface UserMapper extends Mapper<User>{

 

}

 

1.2.3.4 完成的目錄結構

 

1.3 通用Mapper的測試

創建NewUserMapper測試類,如下:

package com.qhrgd;

 

import java.io.IOException;

import java.io.InputStream;

import java.util.List;

 

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.Before;

import org.junit.Test;

 

import com.github.abel533.entity.Example;

import com.qhr.mapper.UserMapper;

import com.qhr.pojo.User;

 

public class NewUserMapperTest {

private SqlSessionFactory sqlSessionFactory;

 

    private SqlSession sqlSession;

 

    private UserMapper newUserMapper;

 

    @Before

    public void init() {

        String resource = "mybatis-config.xml";

        InputStream inputStream = null;

        try {

            inputStream = Resources.getResourceAsStream(resource);

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

 

        sqlSession = sqlSessionFactory.openSession(true);

 

        newUserMapper = sqlSession.getMapper(UserMapper.class);

 

    }

    /**

     * 根據條件查詢相應的用戶,返回值爲用戶實體,即User

     */

    @Test

    public void testSelectOne() {

        User user = new User();

        user.setId(1L);

        User user2 = this.newUserMapper.selectOne(user);

        System.out.println(user2);

    }

    

    /**

     * 根據條件查詢相應的用戶集合,返回值爲用戶集合,即List<User>

     */

    @Test

    public void testSelect() {

        

        List<User> list = this.newUserMapper.select(null);

        

        for (User user2 : list) {

            System.out.println(user2);

        }

    }

    /**

     * 根據條件查詢符合條件的用戶實體數量,返回值爲int

     */

    @Test

    public void testSelectCount() {

        User user = new User();

        user.setUsername("zhangsan");

        int count = this.newUserMapper.selectCount(null);

        System.out.println(count);

    }

    /**

     * 根據主鍵查詢特定用戶,返回值類型爲User

     */

    @Test

    public void testSelectByPrimaryKey() {

        User user = new User();

        user.setId(1L);

        User user2 = this.newUserMapper.selectByPrimaryKey(user);

        System.out.println(user2);

    }

    /**

     * 插入特定用戶

     */

    @Test

    public void testInsert() {

        User  user = new User();

        user.setId(null);

        user.setUsername("張三丰2");

        user.setPassword("12345123");

        this.newUserMapper.insert(user);

    }

 

    @Test

    public void testInsertSelective() {

        User  user = new User();

        user.setId(null);

        user.setUsername("張2");

        user.setPassword("123");

        this.newUserMapper.insertSelective(user);

    }

    /**

     * 根據條件刪除用戶信息

     */

    @Test

    public void testDelete() {

        User user = new User();

        user.setUsername("張2");

        user.setPassword("123");

        this.newUserMapper.delete(user);

    }

    /**

     * 根據主鍵刪除用戶信息

     */

    @Test

    public void testDeleteByPrimaryKey() {

        User user = new User();

        user.setId(1L);

        this.newUserMapper.deleteByPrimaryKey(user);

    }

    /**

     * 根據主鍵修改用戶信息

     */

    @Test

    public void testUpdateByPrimaryKey() {

        User user = new User();

        user.setId(12L);

        user.setUsername("張三2");

        user.setPassword("123123");

        this.newUserMapper.updateByPrimaryKey(user);

        sqlSession.commit();

    }

    /**

     * 根據主鍵修改用戶信息

     */

    @Test

    public void testUpdateByPrimaryKeySelective() {

        User user = new User();

        user.setId(12L);

        user.setUsername("張三");

        user.setPassword("3333333");

        this.newUserMapper.updateByPrimaryKeySelective(user);

        sqlSession.commit();

    }

    

    /**

     * 查詢1到10主鍵的用戶實體數量,返回值爲int

     */

    @Test

    public void testSelectCountByExample() {

        Example example = new Example(User.class);

        example.createCriteria().andBetween("id", 1L, 10L);

        int countByExample = this.newUserMapper.selectCountByExample(example);

        System.out.println(countByExample);

    }

    /**

     * 刪除10到12爲主鍵的用戶信息,返回值爲int

     */

    @Test

    public void testDeleteByExample() {

        Example example = new Example(User.class);

        example.createCriteria().andBetween("id", 10L, 12L);

        int countByExample = this.newUserMapper.deleteByExample(example);

        System.out.println(countByExample);

    }

    /**

     * 查詢1到10的用戶信息,返回值爲用戶集合List<User>

     */

    @Test

    public void testSelectByExample() {

        Example example = new Example(User.class);

        example.createCriteria().andBetween("id", 1L, 10L);

         List<User> list = this.newUserMapper.selectByExample(example);

         for (User user : list) {

            System.out.println(user);

        }

    }

    /**

     *

     */

    @Test

    public void testUpdateByExampleSelective() {

        Example example = new Example(User.class);

        example.createCriteria().andEqualTo("username", "asdf");

        User user = new User();

        user.setUsername("test");

        int count = this.newUserMapper.updateByExampleSelective(user,example);

    }

    /**

     *

     */

    @Test

    public void testUpdateByExample() {

        Example example = new Example(User.class);

        example.createCriteria().andEqualTo("username", "張三丰");

        User user = new User();

        user.setUsername("asdf");

        user.setPassword("asdfasdf");

        int count = this.newUserMapper.updateByExample(user,example);

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章