spring+mybitis+tk+pagehelper+lombok使用

  1. 使用maven管理添加依赖
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.0.3</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
        </dependency>
         <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

添加配置类

    @Bean(name = "sqlSessionFactoryBean")
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        Interceptor[] interceptors = new Interceptor[] {pageInterceptor()};
        sqlSessionFactoryBean.setPlugins(interceptors);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:mapper/*.xml");
        sqlSessionFactoryBean.setMapperLocations(resources);
        return sqlSessionFactoryBean;
    }

    @Bean
    public PageInterceptor pageInterceptor(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("dialect", "mysql");
        properties.setProperty("reasonable", "true");
        pageHelper.setProperties(properties);
        PageInterceptor pageInterceptor = new PageInterceptor();
        pageInterceptor.plugin(pageHelper);
        return pageInterceptor;
    }

在扫描类上添加注解@tk.mybatis.spring.annotation.MapperScan,比如我的是spring boot的启动类上。
然后我们再配置一个mybatis-generator,这个是mybits逆向工程生成实体类和mapper,首先需要在项目资源目录下配置一个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>
    <!-- 数据库驱动-->
    <context id="DB2Tables" targetRuntime="MyBatis3Simple">
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <!-- mapper接口中继承的接口 -->
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.MySqlMapper"/>
            <property name="useMapperCommentGenerator " value="true"/>
            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
            <property name="caseSensitive" value="true"/>
            <property name="generateColumnConsts" value="`"/>
            <!-- 在bean中添加@data lombok -->
            <property name="lombok" value="Data"/>
        </plugin>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://ip:port/schema"
                        userId="userId"
                        password="passwd">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.base.data.dto"
                            targetProject="./src/main/java">
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="resources/mapper"
                         targetProject="src/main">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.base.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="tableName" domainObjectName="TableName"/>
    </context>
</generatorConfiguration>

然后在需要运行逆向工程的模块pom中添加下面的build

 <build>
        <plugins>
            <!--mybatis自动生成代码插件  用mysql8.0的依赖以及驱动包会报错,可以切换mysql5.8版本以下的依赖-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!-- generatorConfig.xml文件路径 ${basedir}表示项目根目录,即包含pom.xml文件的目录,是maven内置属性-->
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <!-- 是否覆盖,true表示会替换生成的JAVA文件,false则不覆盖 -->
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.45</version>
                    </dependency>
                    <!-- 通用mapper依赖  假如需要使用通用mapper,放开注释 -->
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>              
                </dependencies>
            </plugin>
        </plugins>
    </build>

然后使用maven运行插件
spring+mybitis+tk+pagehelper+lombok使用_第1张图片
在代码中使用分页如下

        Example example = new Example(SystemParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", code);
        PageHelper.startPage(1, 10);
        List<SystemParam> list = systemParamMapper.selectByExample(example);
        PageInfo<SystemParam> page = new PageInfo<>(list);

你可能感兴趣的:(java)