<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- MyBatis 根據實體類自動生成dao層配置--> <!-- MyBatis Generator --> <targetJavaProject>${basedir}/src/main/java</targetJavaProject> <!-- Java接口 --> <targetMapperPackage>com.jiafly.falsework.dao</targetMapperPackage> <!-- 實體類 --> <targetModelPackage>com.jiafly.falsework.entity</targetModelPackage> <!-- XML生成路徑 --> <targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject> <targetXMLPackage>mapper</targetXMLPackage> </properties> <!-- mybatis 通用mapper配置 --> <!-- Spring Boot Mybatis 依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>RELEASE</version> </dependency> <!-- MySQL 鏈接驅動依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> <scope>runtime</scope> </dependency> <!-- mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- mybatis-generator-maven-plugin --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>false</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> </dependencies> </plugin> </plugins> </build>
spring: # 數據庫相關配置 datasource: url: jdbc:mysql://localhost:3306/jiafly?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useTimezone=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true username: root password: root driver-class-name: com.mysql.jdbc.Driver initialSize: 1 minIdle: 1 maxActive: 5 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 180000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false # mybaties 配置 mybatis: mapperLocations: classpath:mapper/*.xml typeAliasesPackage: com.jiafly.libra.entity # 通用mapper 配置 自動生成dao層代碼 mapper: plugin: tk.mybatis.mapper.generator.MapperPlugin Mapper: tk.mybatis.mapper.common.Mapper not-empty: false identity: MYSQL
<?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> <properties resource="application.yml"/> <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/jiafly" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"> <!-- 是否容許子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 是否對model添加 構造函數 --> <property name="constructorBased" value="true"/> <!-- 是否對類CHAR類型的列的數據進行trim操做 --> <property name="trimStrings" value="true"/> <!-- 創建的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER" > <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- tableName是數據庫的表名,domainObjectName是生成的實體類名稱 --> <table tableName="user_info" domainObjectName="UserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
實體類java
package com.jiafly.libra.entity; import java.util.Date; public class UserInfo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String userId; private String mobile; private String email; private String openId; private String unionId; private String userName; private String nickName; private Byte gender; private Integer age; private String city; private String province; private String country; private String password; private Byte vip; private Byte delFlag; private Date createTime; private Date updateTime; }
接口mysql
package com.jiafly.libra.mapper; import com.jiafly.libra.entity.UserInfo; import tk.mybatis.mapper.common.Mapper; public interface UserInfoMapper extends Mapper<UserInfo> { }
xmlspring
<?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.jiafly.libra.mapper.UserInfoMapper" > </mapper>