使用eclipse创建maven项目需要的一些基础步骤

本文详细介绍了如何在eclipse环境中配置maven,设置maven仓库,集成nexus-aliyun中央仓库,并创建一个包含spring boot、mybatis的maven项目。内容包括环境变量配置、settings.xml设置、eclipse的maven配置,以及项目结构、配置文件如application.yml、generatorConfig.xml、pom.xml的详解。同时,还展示了App.java启动类和MyBatisGeneratorUtil.java工具类的代码示例。
摘要由CSDN通过智能技术生成

使用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();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值