创建一个springboot项目,然后添加依赖,pom.xml如下
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.lvpeilei</groupId> 7 <artifactId>springboot-mybatis</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>springboot-mybatis</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.2.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-jdbc</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.mybatis.spring.boot</groupId> 38 <artifactId>mybatis-spring-boot-starter</artifactId> 39 <version>1.3.2</version> 40 </dependency> 41 42 <dependency> 43 <groupId>mysql</groupId> 44 <artifactId>mysql-connector-java</artifactId> 45 <scope>runtime</scope> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-test</artifactId> 50 <scope>test</scope> 51 </dependency> 52 <!-- alibaba的druid数据库连接池 --> 53 <dependency> 54 <groupId>com.alibaba</groupId> 55 <artifactId>druid-spring-boot-starter</artifactId> 56 <version>1.1.0</version> 57 </dependency> 58 </dependencies> 59 60 <build> 61 <plugins> 62 <plugin> 63 <groupId>org.springframework.boot</groupId> 64 <artifactId>spring-boot-maven-plugin</artifactId> 65 </plugin> 66 <!-- mybatis generator 自动生成代码插件 --> 67 <plugin> 68 <groupId>org.mybatis.generator</groupId> 69 <artifactId>mybatis-generator-maven-plugin</artifactId> 70 <version>1.3.2</version> 71 <configuration> 72 <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> 73 <overwrite>true</overwrite> 74 <verbose>true</verbose> 75 </configuration> 76 </plugin> 77 </plugins> 78 </build> 79 80 81 </project>
然后配置各种属性
application.yml
1 server: 2 port: 8080 3 4 spring: 5 datasource: 6 name: test 7 url: jdbc:mysql://192.168.20.30:3607/us_sys 8 username: root 9 password: root 10 # 使用druid数据源 11 type: com.alibaba.druid.pool.DruidDataSource 12 driver-class-name: com.mysql.jdbc.Driver 13 filters: stat 14 maxActive: 20 15 initialSize: 1 16 maxWait: 60000 17 minIdle: 1 18 timeBetweenEvictionRunsMillis: 60000 19 minEvictableIdleTimeMillis: 300000 20 validationQuery: select 'x' 21 testWhileIdle: true 22 testOnBorrow: false 23 testOnReturn: false 24 poolPreparedStatements: true 25 maxOpenPreparedStatements: 20 26 mybatis: 27 mapper-locations: classpath:mapping/*.xml 28 type-aliases-package: com.lvpeilei.model 29 30 #pagehelper分页插件 31 pagehelper: 32 helperDialect: mysql 33 reasonable: true 34 supportMethodsArguments: true 35 params: count=countSql
自动生成代码工具的相关配置
generatorConfig.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <generatorConfiguration> 6 <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包--> 7 <classPathEntry location="D:\maven\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/> 8 <context id="DB2Tables" targetRuntime="MyBatis3"> 9 <commentGenerator> 10 <property name="suppressDate" value="true"/> 11 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 12 <property name="suppressAllComments" value="true"/> 13 </commentGenerator> 14 <!--数据库链接URL,用户名、密码 --> 15 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.20.30:3607/us_sys" userId="root" password="root"> 16 </jdbcConnection> 17 <javaTypeResolver> 18 <property name="forceBigDecimals" value="false"/> 19 </javaTypeResolver> 20 <!-- 生成模型的包名和位置--> 21 <javaModelGenerator targetPackage="com.lvpeilei.model" targetProject="src/main/java"> 22 <property name="enableSubPackages" value="true"/> 23 <property name="trimStrings" value="true"/> 24 </javaModelGenerator> 25 <!-- 生成映射文件的包名和位置--> 26 <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> 27 <property name="enableSubPackages" value="true"/> 28 </sqlMapGenerator> 29 <!-- 生成DAO的包名和位置--> 30 <javaClientGenerator type="XMLMAPPER" targetPackage="com.lvpeilei.mapper" targetProject="src/main/java"> 31 <property name="enableSubPackages" value="true"/> 32 </javaClientGenerator> 33 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> 34 <table tableName="us_sys.tb_sys_person" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> 35 </context> 36 </generatorConfiguration>