• mybatis 快速开发


      在应用开发过程中,单表操作永远占据我们工作的一部分,初略估计我觉得大概应该占到40%或者更多,这些操作千篇一律,简单而且繁琐,DB增删字段,对于mapper.xml要做大量的修改,稍微不慎,bug百出。解决了这个问题我觉得工作效率会提升很多,因为以前我很厌烦数据库增删字段,然而现在这个问题已经不复存在,因为增删字段只需要修改同步增删实体类字段而已。

      项目业务功能大概分Controller,service,serviceImpl,dao,mapper,entity,model这些层来处理,一般情况下通过mybatis generator生成dao,mapper,entity其中mapper已经写好了大量的配置文件,这些东西增删一个字段要修改好多地方,也就是说不动则已一动则很麻烦。而且不同工程师创建的service名不能知其意,往往同一功能被多人开发。

      现在通过生成Controller之外的所有层,且生成的文件够干净,dao通过集成通用BaseDao<T>来扩展dao功能,service通过继承通用IService<T>来扩展service功能,让生成的类天生带有常用的功能,其他的和以前一样。

      这样针对所有单表操作的功能实现都能得到封装,当我们使用的时候不用写Service层及以下层相关代码。但是事务问题不好处理,不过我们可以事务上移到controller层,这样做我不觉得有啥缺点,同时能够满足所有的要求。

      相对mybatis依赖较少

    <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.young4j</groupId>
      <artifactId>my_mybatis</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <packaging>war</packaging>
      <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <spring.version>4.2.5.RELEASE</spring.version>
            <junit.version>4.12</junit.version>
            <druid.version>1.1.0</druid.version>
            <fastjson.version>1.2.8</fastjson.version>
            <mybaitsplus.version>2.1-gamma</mybaitsplus.version>
            <mysql.version>5.1.38</mysql.version>
            <log4j.version>1.2.17</log4j.version>
            <slf4j.version>1.7.19</slf4j.version>
            <aspectjweaver.version>1.8.8</aspectjweaver.version>
            <fileupload.version>1.3.1</fileupload.version>
            <velocity.version>1.7</velocity.version>
            <jstl.version>1.2</jstl.version>
        </properties>
      <dependencies>
            <!-- JUnit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
    
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
    
            <!-- Spring MVC -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
    
            <!-- AOP -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${aspectjweaver.version}</version>
            </dependency>
    
            <!-- FileUpload -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${fileupload.version}</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
    
            <!-- Mybatis-Plus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>${mybaitsplus.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity</artifactId>
                <version>${velocity.version}</version>
            </dependency>
            <!-- Mysql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
    
            <!-- Druid -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
    
            <!-- FastJson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>
    
            <!-- Log -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>my_mybatis</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    分页就是如此简单

    @RequestMapping("/testDb")
        public Object testDb(Integer id){
            StringBuffer sb = new StringBuffer();
            
            Lables lable = lablesService.selectById(id);
            log.info("selectById" + lable.toString());
            sb.append(lable.toString());
            
            List<Lables> lables =lablesService.selectList(new EntityWrapper<Lables>());
            log.info("selectBatchIds" + lables.toString());
            sb.append(lables);
            
            Page<Lables> page = lablesService.selectPage(new Page<Lables>(1,10),new EntityWrapper<Lables>());
            List<Lables> records = page.getRecords();
            int pageCount = lablesService.selectCount(new EntityWrapper<Lables>());
            log.info("records" + records.toString());
            log.info("selectPage" + page.toString());
            log.info("selectCount" + pageCount);
            sb.append(page).append(pageCount).append(records);
            return sb.toString();
        }
    import com.young4j.entity.Area;
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    
    /**
     * <p>
      * 区 Mapper 接口
     * </p>
     *
     * @author yangfei
     * @since 2017-09-08
     */
    public interface AreaDao extends BaseMapper<Area> {
    
    }
    <?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.young4j.dao.AreaDao">
    
    </mapper>
    package com.young4j.service;
    
    import com.young4j.entity.Area;
    import com.baomidou.mybatisplus.service.IService;
    
    /**
     * <p>
     * 区 服务类
     * </p>
     *
     * @author yangfei
     * @since 2017-09-08
     */
    public interface AreaService extends IService<Area> {
        
    }
    package com.young4j.service.impl;
    
    import com.young4j.entity.Area;
    import com.young4j.dao.AreaDao;
    import com.young4j.service.AreaService;
    import com.baomidou.mybatisplus.service.impl.ServiceImpl;
    import org.springframework.stereotype.Service;
    
    /**
     * <p>
     * 区 服务实现类
     * </p>
     *
     * @author yangfei
     * @since 2017-09-08
     */
    @Service
    public class AreaServiceImpl extends ServiceImpl<AreaDao, Area> implements AreaService {
        
    }
  • 相关阅读:
    subprocess模块小记 分类: python Module 2013-12-04 17:13 287人阅读 评论(0) 收藏
    去除文件中的重复元素后写入文件 分类: python 小练习 open()文件读写 2013-12-03 11:21 356人阅读 评论(0) 收藏
    结尾非零数的奇偶性 分类: python 小练习 2013-12-02 18:04 371人阅读 评论(0) 收藏
    获取数字特定因子元素的个数 分类: python 小练习 2013-12-02 16:57 250人阅读 评论(0) 收藏
    输出 结尾0的个数 分类: python 小练习 2013-12-02 16:46 393人阅读 评论(0) 收藏
    查看JVM参数
    线程池配置合理线程数
    线程池的手写和拒绝策略
    死锁及定位分析
    Callable接口用法
  • 原文地址:https://www.cnblogs.com/yangfei-beijing/p/7511078.html
Copyright © 2020-2023  润新知