使用eclipse创建maven项目需要的一些基础步骤
首先是环境搭建
1.环境变量
a.maven_home: D:\Program Files\apache-maven-3.6.3
b.path:%maven_home%\bin
2.config
settings
a.仓库位置
E:\m2\repository
b.中央仓库
nexus-aliyun
central
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public
3.集成到eclipse中
a.安装路径(window/preferences/maven/)
b.settings文件引入
application.yml
generatorConfig.xml
pom.xml
App.java
MyBatisGeneratorUtil.java
application.yml 内容如下(resources目录下)
server:
port: 8080
spring:
datasource:
name: test
url: jdbc:mysql://localhost/power?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: root
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select ‘x’
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mvc:
view:
prefix: /WEB-INF/page/ # 页面默认前缀目录
suffix: .jsp # 响应页面默认后缀
resources:
static-locations:/static/
# 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.tf.pojo # 注意:对应实体类的路径
#pagehelper分页插件
pagehelper:
offsetAsPageNum: true
rowBoundsWithCount: true
dialect: mysql
reasonable: true
generatorConfig.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 -->
<!-- false:默认, scale>0;length>18:使用BigDecimal; scale=0;length[10,18]:使用Long;
scale=0;length[5,9]:使用Integer; scale=0;length<5:使用Short; -->
<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成Model类存放位置 -->
<javaModelGenerator targetPackage="com.tf.pojo" targetProject="src/main/java">
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
<property name="enableSubPackages" value="true" />
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="true" />
<!-- for MyBatis3/MyBatis3Simple 自动为每一个生成的类创建一个构造方法,构造方法包含了所有的field;而不是使用setter; -->
<property name="constructorBased" value="false" />
<!-- for MyBatis3/MyBatis3Simple 是否创建一个不可变的类,如果为true, 那么MBG会创建一个没有setter方法的类,取而代之的是类似constructorBased的类 -->
<property name="immutable" value="false" />
</javaModelGenerator>
<!--生成映射文件存放位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成Dao类存放位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.tf.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--生成对应表及类名(代码生成器) -->
<table tableName="" domainObjectName="" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false" />
<!-- 需要插入时返回主键值,请将此属性打开,column值为主键的列明 -->
<generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
</context>
pom.xml的内容:
4.0.0
com.tf
power
0.0.1-SNAPSHOT
war
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring-boot-starter-web包含了Spring Boot预定义的一些Web开发的常用依赖包 如: spring-webmvc,Tomcat.... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot -->
<dependency><!-- 热部署 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional><!-- true:依赖不会传递 -->
</dependency>
<!--mybaitds 代码生成器 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
org.mybatis.generator mybatis-generator-maven-plugin 1.3.5 Generate MyBatis Artifacts package generate
src/main/resources/generatorConfig.xml
true
true org.springframework.boot spring-boot-maven-plugin true
App.java
package com.tf;
import java.util.Properties;
/**
- Hello world!
*/
//此注解指定这是一个SpringBoot的应用程序,
//不加就会报异常 Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import com.github.pagehelper.PageHelper;
@SpringBootApplication
@MapperScan(“com.tf.dao”)
public class App extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
//配置mybatis的分页插件pageHelper
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty(“offsetAsPageNum”,“true”);
properties.setProperty(“rowBoundsWithCount”,“true”);
properties.setProperty(“reasonable”,“true”);
properties.setProperty(“dialect”,“mysql”); //配置mysql数据库的方言
pageHelper.setProperties(properties);
return pageHelper;
}
}
MyBatisGeneratorUtil.java
package com.tf;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MyBatisGeneratorUtil {
public static void main(String[] args) {
List warnings = new ArrayList();
boolean overwrite = true;
String genCfg = “/generatorConfig.xml”;
File configFile = new File(MyBatisGeneratorUtil.class.getResource(genCfg).getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}