SpringBoot+pageHelper+通用mapper环境搭建+逆向工程

如何搭建环境

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.czp</groupId>
	<artifactId>o2o</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>o2o</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<mysql-version></mysql-version>
		<mapper.version>4.0.3</mapper.version>
		<mybatis-generator.version>1.4.0</mybatis-generator.version>
		<mybatis-tk.version>2.0.3</mybatis-tk.version>
		<pagehelper.version>1.2.1</pagehelper.version>
		<mysql-version>5.1.6</mysql-version>
		<pagehelper.version>1.2.1</pagehelper.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${
     mysql-version}</version>
		</dependency>
		<!--pageHelper-->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.12</version>
		</dependency>
		<!--通用mapper-->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper-spring-boot-starter</artifactId>
			<version>2.1.5</version>
			<exclusions>
				<exclusion>
					<artifactId>mybatis-spring</artifactId>
					<groupId>org.mybatis</groupId>
				</exclusion>
				<exclusion>
					<artifactId>mybatis</artifactId>
					<groupId>org.mybatis</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<!--逆向工程-->
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>${
     mybatis-generator.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<!-- MyBatis 逆向工程 插件 -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.6</version>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>${
     mysql-version}</version>
					</dependency>
					<dependency>
						<groupId>tk.mybatis</groupId>
						<artifactId>mapper</artifactId>
						<version>${
     mapper.version}</version>
					</dependency>
				</dependencies>
				<configuration>
					<!-- 允许移动生成的文件 -->
					<verbose>true</verbose>
					<!-- 是否覆盖 -->
					<overwrite>true</overwrite>
					<!-- 配置文件 -->
					<configurationFile>
						${
     basedir}/src/main/resources/generator/generatorConfig.xml
					</configurationFile>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

注意事项

通用mapper starter 和pagehelper starter中有依赖冲突,这时使用IDEA右键->Digarams->show dependencies(上面的依赖已排除)
将mybatis和mybatis-spring右键Exclude排除一个即可
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200318142151830.JPG)

###接下来配置application.proerties

#设置springBoot的日志级别
#设置打印sql
logging.level.com.czp.o2o.dao=debug
logging.level.com=error
#配置数据库连接池
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/o2o?serverTimezone=GMT%2B8&charsetEncoding=utf8
spring.datasource.username=root
spring.datasource.password=sa

#Generator 配置通用mapper逆向工程
#重写mapper接口的生成根目录
generator.dao.targetProject=src/main/java
#基于根目录的实际目录
generator.mappers=com.czp.o2o.util.IBaseMapper
#基于根目录的实体类生成的实际目录
generator.javaModel-targetPackage=com.czp.o2o.entity
#重写xml的sql文件生成的目录
generator.mapper.targetProject=src/main/resources
#配置mapper.xml的文件生成位置
generator.sqlMap-targetPackage=mapper
#配置mapper接口的生成位置
generator.javaClient-targetPackage=com.czp.o2o.dao

#配置pageHelper分页插件
#配置数据库类型
pagehelper.helperDialect=mysql
#参数合理化
pagehelper.reasonable=true
#支持通过 Mapper 接口参数来传递分页参数
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.autoRuntimeDialect=true

#配置mybatis
# mapper.xml所在的包
mybatis.mapper-locations=classpath:resources/mapper/*Mapper.xml
#开启驼峰命名规则
mybatis.configuration.map-underscore-to-camel-case=true

在resources下创建generator目录并创建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>
    <!-- 引入 application.properties -->
    <properties resource="application.properties"/>

    <!-- MyBatis3Simple:不生成 Example相关类及方法 -->
    <context id="mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>
        <!-- 指定生成 Mapper 的继承模板 -->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="${generator.mappers}"/>
        </plugin>

        <!-- 生成 JavaBean 对象重写 toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!-- 生成 JavaBean 对象继承 Serializable 类 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>

        <!-- 生成 JavaBean 对象重写 equals 和 hashCode 方法 -->
        <!-- <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> -->

        <!-- jdbc 连接配置 -->
        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        </jdbcConnection>

        <!--生成实体类-->
        <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}"
                            targetProject="${generator.dao.targetProject}"/>
        <!--生成xml文档-->
        <sqlMapGenerator targetPackage="${generator.sqlMap-targetPackage}"
                         targetProject="${generator.mapper.targetProject}"/>

        <!--生成dao接口-->
        <javaClientGenerator targetPackage="${generator.javaClient-targetPackage}"
                             targetProject="${generator.dao.targetProject}" type="XMLMAPPER"/>

        <!-- Mysql 配置 -->
        <!-- <generatedKey column="id" sqlStatement="Mysql" identity="true" /> -->
        <!-- Oracle 配置 -->
        <!-- <generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/> -->

        <!-- tableName:数据库表名,domainObjectName:生成文件名 ,schema:数据源-->
        <table tableName="tbl_area" domainObjectName="Area"></table>
        <table tableName="tbl_head_line" domainObjectName="HeadLine"></table>
        <table tableName="tbl_local_auth" domainObjectName="LocalAuth"></table>
        <table tableName="tbl_person_info" domainObjectName="Person"></table>
        <table tableName="tbl_product" domainObjectName="Product"></table>
        <table tableName="tbl_product_category" domainObjectName="ProductCategory"></table>
        <table tableName="tbl_product_img" domainObjectName="ProductImg"></table>
        <table tableName="tbl_product_img" domainObjectName="ProductImg"></table>
        <table tableName="tbl_shop" domainObjectName="Shop"></table>
        <table tableName="tbl_shop_category" domainObjectName="ShopCategory"></table>
        <table tableName="tbl_user_product_map" domainObjectName="UserProductMap"></table>
        <table tableName="tbl_user_shop_map" domainObjectName="UserSHopMap"></table>
        <table tableName="tbl_product_sell_daily" domainObjectName="ProductSellDaily"></table>
    </context>
</generatorConfiguration>

通用mapper需要一个接口(放置在util包下 IBaseMapper)

package com.czp.o2o.util;

import tk.mybatis.mapper.additional.insert.InsertListMapper;
import tk.mybatis.mapper.common.Mapper;

@org.apache.ibatis.annotations.Mapper
public interface IBaseMapper<T> extends Mapper<T>, InsertListMapper {
     

}

最后切换pom.xml

点击侧栏maven->mybatis-generator->mybatis-generator:generate就会自动地将数据库中地表映射到实体类和mapper接口了

注意事项

在springboot的启动类记着添加注解@MapperScan这样就可以使用@autowired注解来注入dao了

package com.czp.o2o;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication

@MapperScan(basePackages = "com.czp.o2o.dao")
public class O2oApplication {
     

	public static void main(String[] args) {
     
		SpringApplication.run(O2oApplication.class, args);
	}

}

手脚架源码提取链接bmr9

你可能感兴趣的:(mybatis,spring,java)