原创转载请注明出处:
乐观锁Version图示
Project Directory
Maven Dependency
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>HelloSpring</groupId> 8 <artifactId>org.fool.spring</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>1.5.22.RELEASE</version> 15 </parent> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-web</artifactId> 21 <exclusions> 22 <exclusion> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-tomcat</artifactId> 25 </exclusion> 26 </exclusions> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-jetty</artifactId> 31 <exclusions> 32 <exclusion> 33 <groupId>org.eclipse.jetty.websocket</groupId> 34 <artifactId>websocket-server</artifactId> 35 </exclusion> 36 <exclusion> 37 <groupId>org.eclipse.jetty.websocket</groupId> 38 <artifactId>javax-websocket-server-impl</artifactId> 39 </exclusion> 40 </exclusions> 41 </dependency> 42 43 <dependency> 44 <groupId>org.mybatis.spring.boot</groupId> 45 <artifactId>mybatis-spring-boot-starter</artifactId> 46 <version>1.3.5</version> 47 </dependency> 48 49 <dependency> 50 <groupId>com.alibaba</groupId> 51 <artifactId>druid-spring-boot-starter</artifactId> 52 <version>1.1.20</version> 53 </dependency> 54 55 <dependency> 56 <groupId>mysql</groupId> 57 <artifactId>mysql-connector-java</artifactId> 58 </dependency> 59 60 <dependency> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-starter-test</artifactId> 63 <scope>test</scope> 64 </dependency> 65 66 <dependency> 67 <groupId>org.mybatis.generator</groupId> 68 <artifactId>mybatis-generator-core</artifactId> 69 <version>1.3.7</version> 70 <scope>test</scope> 71 </dependency> 72 </dependencies> 73 74 <build> 75 <plugins> 76 <plugin> 77 <groupId>org.springframework.boot</groupId> 78 <artifactId>spring-boot-maven-plugin</artifactId> 79 </plugin> 80 <plugin> 81 <groupId>org.mybatis.generator</groupId> 82 <artifactId>mybatis-generator-maven-plugin</artifactId> 83 <version>1.3.7</version> 84 <configuration> 85 <configurationFile>sql/generatorConfig.xml</configurationFile> 86 <verbose>true</verbose> 87 <overwrite>true</overwrite> 88 </configuration> 89 </plugin> 90 </plugins> 91 </build> 92 </project>
application.properties
1 server.port=9999 2 3 spring.datasource.url=jdbc:mysql://localhost:3306/test 4 spring.datasource.username=root 5 spring.datasource.password=123456 6 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 7 8 mybatis.type-aliases-package=org.fool.spring.model 9 mybatis.mapper-locations=classpath:mapper/**/*.xml
ddl.sql
1 CREATE TABLE `goods` ( 2 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 3 `name` varchar(32) NOT NULL COMMENT '商品名称', 4 `state` tinyint(4) unsigned NOT NULL COMMENT '1.正常;2.缺货', 5 `version` bigint(20) unsigned NOT NULL, 6 `create_time` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), 7 `update_time` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), 8 PRIMARY KEY (`id`) 9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 10 11 insert into goods(name, state, version) values('iPhone', 1, 1); 12 insert into goods(name, state, version) values('iMac', 1, 1); 13 insert into goods(name, state, version) values('iPad', 1, 1); 14 insert into goods(name, state, version) values('iWatch', 1, 1);
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 <classPathEntry 7 location="/Users/${user.name}/.m2/repository/mysql/mysql-connector-java/5.1.48/mysql-connector-java-5.1.48.jar"/> 8 9 <context id="test" targetRuntime="MyBatis3"> 10 11 <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> 12 <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/> 13 14 <commentGenerator> 15 <property name="suppressAllComments" value="true"/> 16 </commentGenerator> 17 18 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 19 connectionURL="jdbc:mysql://localhost:3306/test" 20 userId="root" 21 password="123456"> 22 </jdbcConnection> 23 24 <javaModelGenerator targetPackage="org.fool.spring.model" targetProject="src/main/java"> 25 <property name="enableSubPackages" value="true"/> 26 <property name="trimStrings" value="true"/> 27 </javaModelGenerator> 28 29 <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> 30 <property name="enableSubPackages" value="true"/> 31 </sqlMapGenerator> 32 33 <javaClientGenerator targetPackage="org.fool.spring.dao.mapper" targetProject="src/main/java" 34 type="XMLMAPPER"> 35 <property name="enableSubPackages" value="true"/> 36 </javaClientGenerator> 37 38 <table tableName="goods" domainObjectName="Goods" 39 enableCountByExample="false" enableUpdateByExample="false" 40 enableDeleteByExample="false" enableSelectByExample="false" 41 selectByExampleQueryId="false"> 42 </table> 43 44 </context> 45 46 </generatorConfiguration>
自动生成model和mapper
1 mybatis-generator:generate -e