环境配置:
IDE:基于eclipse的spring开发工具STS3.7.0——Spring Tool Suite;
Java:jdk1.8;
时间:2018-3-30 13:50:13;
搭建过程
1、STS创建spring boot项目,勾选web、jdbc、mysql、mybatis模块,选择Spring boot2.0版本;
2、在application.properties中添加数据库连接信息;
spring.datasource.url=jdbc\:mysql\://localhost\:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.database=mysql
3、在pom.xml中添加Mybatis Generator自动生成代码插件;
<!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <dependencies> <!-- 如果插件这里不配置mysql-connector-java依赖,则需要在generatorConfig.xml中指定本地的mysql-connector-java文件 --> <!-- <classPathEntry location="E:\java\mybatis-generator-core-1.3.2\lib\mysql-connector-java-5.1.35-bin.jar"/> --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> </dependencies> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin>
在application.properties中添加配置文件信息:
# mybatis mybatis.mapper-locations=classpath*:mapping/*Mapper.xml mybatis.type-aliases-package=com.tairan.xmf.entity
注意:执行方法是Maven build...里添加指令mybatis-generator:generate;
注意:mapper文件未添加@Mapper注解,需手动添加;
在resoueces目录下添加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="test" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator> <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 --> <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true --> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="root" password="123456"></jdbcConnection> <javaTypeResolver> <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.tairan.xmf.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.tairan.xmf.dao" implementationPackage="com.tairan.xmf.dao.impl" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成哪些表 --> <table tableName="person" domainObjectName="Person" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="grade" domainObjectName="Grade" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
4、添加阿里数据源druid——官方Demo;
<!-- 阿里数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency>
5、在Maven插件下添加热部署依赖;
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <!-- spring热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <!-- Spring boot项目可以不加版本号,但我在添加了Mybatis Generator后,这里不写版本号会报错 --> <version>1.2.8.RELEASE</version> </dependency> </dependencies> </plugin>
注意:当系统通过 mvn spring-boot:run启动或者 右键application debug 启动java文件时,系统会监视classes文件,当有classes文件被改动时,系统会重新加载类文件,不用重启启动服务。注:使用application run(非debug模式下),热部署功能会失效。
6、添加通用Mapper和PageHelper分页插件——官方Demo;
<!--mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <!--分页插件pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
在application.properties中添加配置文件信息:
#mapper #mappers 多个接口时逗号隔开; # mapper.mappers=tk.mybatis.springboot.util.MyMapper # mapper.not-empty=false # mapper.identity=MYSQL #pagehelper pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql
7、在pom.xml中添加jsp解析依赖;
<!-- 用于编译JSP的jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
在application.properties中添加配置文件信息:
# jsp spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp