2017-08-25 · 许进 · · 字数:1176·阅读时间 3 分钟 阅读量

1. mybatis-gennerator

1.1 mybatis-gennerator自动生成代码

1.1.1 mybatis-gennerator的安装

Mybatis-Generator的下载地址:https://github.com/mybatis/generator/releases

  1. 在Eclipse安装mybatis-gennerator插件。

https://marketplace.eclipse.org/content/mybatis-generator

2.在项目中安装Maven插件

<build> 
  <finalName>janus-admin</finalName>  
  <pluginManagement> 
    <plugins> 
      <plugin> 
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-compiler-plugin</artifactId>  
        <configuration> 
          <source>1.8</source>  
          <target>1.8</target> 
        </configuration>  
        <version>3.3</version> 
      </plugin>  
      <plugin> 
        <groupId>org.mybatis.generator</groupId>  
        <artifactId>mybatis-generator-maven-plugin</artifactId>  
        <version>1.3.2</version>  
        <dependencies> 
          <dependency> 
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>6.0.6</version> 
          </dependency> 
        </dependencies>  
        <configuration> 
          <!--配置文件的路径 -->  
          <configurationFile>${basedir}/generatorConfig.xml</configurationFile>  
          <overwrite>true</overwrite> 
        </configuration> 
      </plugin> 
    </plugins> 
  </pluginManagement> 
</build>

使用命令:mybatis-generator:generate生成

推荐安装IDE插件的方式或者Maven插件的方式,生成的代码直接刷新工程即可

generatorConfig.xml示例

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >  
<generatorConfiguration>  
    <!-- 数据库驱动包位置 -->  
    <classPathEntry  
        location="D:\develop\apache-maven-3.5.0\res\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar" />  
    <context id="context1">  
        <commentGenerator>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!-- 数据库链接URL、用户名、密码 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
            connectionURL="jdbc:mysql://主机:3306/数据库" userId="用户名" password="密码" />  
        <!-- 生成模型的包名和位置 -->  
        <javaModelGenerator targetPackage="org.xujin.janus.admin.entity" targetProject="janus-admin/src/main/java" />  
        <!-- 生成的映射文件报名和位置 -->  
        <sqlMapGenerator targetPackage="mapper" targetProject="janus-admin/src/main/resources" />  
        <!-- 生成DAO的包名和位置 -->  
        <javaClientGenerator targetPackage="org.xujin.janus.admin.mapper" targetProject="janus-admin/src/main/java" type="XMLMAPPER" />  
        
        <!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) -->  
        <table schema="janus_admin" tableName="cluster" domainObjectName="Cluster" enableCountByExample="false" enableUpdateByExample="false"  
            enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">  
        </table>  
        
         <table schema="janus_admin" tableName="route_info" domainObjectName="RouteInfo" enableCountByExample="false" enableUpdateByExample="false"  
            enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">  
        </table>
        <!-- 
           或者使用 tableName="%"通配全部生成
         <table tableName="%" enableCountByExample="false" enableUpdateByExample="false"  
            enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <!--mysql 配置-->
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
            <!--oracle 配置-->
            <!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
       </table>  
       -->
    </context>  
</generatorConfiguration>  

二.Mybatis分页插件

2.1 引入maven依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.1.3</version>
</dependency>

2.2 使用pagehelper

public interface ExRouteInfoMapper extends RouteInfoMapper {

    public List<RouteInfo> selectAll();

    /**
     * 分页查询数据
     * @return
     */
    Page<RouteInfo> findByPage();

}

业务层使用代码示li

@Override
public PageInfo<RouteInfo> findByPage(int pageNo, int pageSize) {
    PageHelper.startPage(pageNo, pageSize);
    Page<RouteInfo> pagelist = routeMapper.findByPage();
    PageInfo<RouteInfo> pageInfo = new PageInfo<>(pagelist);
    return pageInfo;
}

三.Mybatis的Mapper继承

3.1 为什么使用Mapper继承

使用mybatis-gennerator自动生成代码,每次自动生成代码都会覆盖之前的Mapper文件,为了将自定义的Mapper和接口与生成的Mapper与接口分开,使用继承方式处理。

3.2 代码示例处理

1.mybatis-gennerator自动生成的Mapper接口

public interface RouteInfoMapper {
    int deleteByPrimaryKey(Long id);

    int insert(RouteInfo record);

    int insertSelective(RouteInfo record);

    RouteInfo selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(RouteInfo record);

    int updateByPrimaryKey(RouteInfo record);
}

2.自动生成的Mapper文件

<?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="org.xujin.janus.admin.mapper.RouteInfoMapper">
  <resultMap id="BaseResultMap" type="org.xujin.janus.admin.entity.RouteInfo">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="domain_id" jdbcType="INTEGER" property="domainId" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="name" jdbcType="VARCHAR" property="name" />
   
  </resultMap>
  <!-- 其余省略 -->
</mapper>

3.继承RouteInfoMapper扩展的接口

public interface ExRouteInfoMapper extends RouteInfoMapper {

    public List<RouteInfo> selectAll();

    /**
     * 分页查询数据
     * @return
     */
    Page<RouteInfo> findByPage();

}

4.扩展的Mapper文件

<?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="org.xujin.janus.admin.mapper.ex.ExRouteInfoMapper">
  <resultMap id="BaseResultMap" type="org.xujin.janus.admin.entity.RouteInfo">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="domain_id" jdbcType="INTEGER" property="domainId" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="name" jdbcType="VARCHAR" property="name" />
   
  </resultMap>
  <!-- 其余省略 -->
</mapper>

5.Service层依赖注入ExRouteInfoMapper使用

@Service
@Transactional(readOnly = true)
public class RouteInfoServiceImpl implements RouteInfoService {

    @Autowired
    private ExRouteInfoMapper routeMapper;

    @Override
    @Transactional
    public void insert(RouteInfoModel routeInfoModel) {
        RouteInfo RouteInfo = BeanMapper.map(routeInfoModel, RouteInfo.class);
        routeMapper.insert(RouteInfo);
    }
}