Eclipse+Maven 项目创建

Posted w53064

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eclipse+Maven 项目创建相关的知识,希望对你有一定的参考价值。

★:jar包下载不了的话可能是镜像里没有这个版本,换个低版本的就行

★:eclipse工程validating很慢,可以先关掉验证(一般对项目没什么影响)

★:eclipse工程pom.xml文件报错 到仓库 删除所有以.lastupdate结尾的文件

★:maven下载jar包慢

解决办法就是到setting文件中,配上<mirror>标签的内容 -->

加在<mirrors>里面

<mirror>

<id>alimaven</id>

<name>aliyun maven</name>

<url>http://maven.aliyun.com/nexus/content/groups/public/</url>

<mirrorOf>central</mirrorOf>

</mirror>

0:maven .setting文件配置jdk版本

在你的maven/config/setting.xml文件的<properties>属性中加上这段代码

技术分享图片

<profile>

 

<id>jdk-1.8</id>

 

<activation>

 

<activeByDefault>true</activeByDefault>

 

<jdk>1.8</jdk>

 

</activation>

 

<properties>

 

<maven.compiler.source>1.8</maven.compiler.source>

 

<maven.compiler.target>1.8</maven.compiler.target>

 

<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

 

</properties>

1:项目创建

在 Package Explorer 空白处右键创建Maven Project

技术分享图片

 

技术分享图片

技术分享图片

  1. 为你的web项目配置tomcat环境

    在你的项目右键→Bulid Path→Add Libraries→server runtime→添加tomcat

    技术分享图片技术分享图片技术分享图片

    完成后如图所示:

    技术分享图片

    3.更改Deployment Assembly设置

    将图中用不到的三个配置Remove掉

    技术分享图片

    4.修改Project Fact

    技术分享图片

     

    在pom.xml文件中的bulid节点加上

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    <configuration>

    <source>1.8</source>

    <target>1.8</target>

    </configuration>

    </plugin>

    </plugins>

    技术分享图片

     

    然后对项目alt+F5 更新项目

     

     

     

    技术分享图片

     

  2. 创建web.xml文件

    技术分享图片

    6.在pom文件中添加依赖

    ①:配置<dependencies>标签

    <properties>

    <!-- Spring版本号 -->

    <spring.version>4.3.8.RELEASE</spring.version>

    <!-- log4j日志文件管理包版本 -->

    <slf4j.version>1.6.6</slf4j.version>

    <log4j.version>1.2.12</log4j.version>

    <!-- junit版本号 -->

    <junit.version>4.10</junit.version>

    <!-- mybatis版本号 -->

    <mybatis.version>3.2.1</mybatis.version>

    </properties>

     

    <dependencies>

     

    <dependency>

    <groupId>javax</groupId>

    <artifactId>javaee-api</artifactId>

    <version>8.0</version>

    <scope>provided</scope>

    </dependency>

     

    <!-- javaee-web-api包 注意和项目使用的JDK版本对应 -->

    <dependency>

    <groupId>javax</groupId>

    <artifactId>javaee-web-api</artifactId>

    <version>8.0</version>

    <scope>provided</scope>

    </dependency>

    <!-- 添加Spring依赖 -->

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-core</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context-support</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aop</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aspects</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-tx</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jdbc</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-web</artifactId>

    <version>${spring.version}</version>

    </dependency>

     

    <!--单元测试依赖 -->

    <dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>${junit.version}</version>

    <scope>test</scope>

    </dependency>

     

    <!-- 日志文件管理包 -->

    <!-- log start -->

    <dependency>

    <groupId>log4j</groupId>

    <artifactId>log4j</artifactId>

    <version>${log4j.version}</version>

    </dependency>

    <dependency>

    <groupId>org.slf4j</groupId>

    <artifactId>slf4j-api</artifactId>

    <version>${slf4j.version}</version>

    </dependency>

    <dependency>

    <groupId>org.slf4j</groupId>

    <artifactId>slf4j-log4j12</artifactId>

    <version>${slf4j.version}</version>

    </dependency>

    <!-- log end -->

     

    <!--spring单元测试依赖 -->

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-test</artifactId>

    <version>${spring.version}</version>

    <scope>test</scope>

    </dependency>

     

    <!--mybatis依赖 -->

    <dependency>

    <groupId>org.mybatis</groupId>

    <artifactId>mybatis</artifactId>

    <version>${mybatis.version}</version>

    </dependency>

     

    <!-- mysql相关包 -->

    <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <version>5.1.26</version>

    </dependency>

    <!-- 数据库连接池 -->

    <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>druid</artifactId>

    <version>1.0.20</version>

    </dependency>

     

    <!-- Spring集成MyBatis -->

    <dependency>

    <groupId>org.mybatis</groupId>

    <artifactId>mybatis-spring</artifactId>

    <version>1.2.3</version>

    </dependency>

     

    <!-- JSP标准标签库 -->

    <dependency>

    <groupId>javax.servlet</groupId>

    <artifactId>jstl</artifactId>

    <version>1.2</version>

    </dependency>

     

    <!-- Json -->

    <!-- 格式化对象,方便输出日志 -->

    <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>fastjson</artifactId>

    <version>1.2.6</version>

    </dependency>

    <dependency>

    <groupId>org.codehaus.jackson</groupId>

    <artifactId>jackson-mapper-asl</artifactId>

    <version>1.9.13</version>

    </dependency>

    <!-- 上传组件包 -->

    <dependency>

    <groupId>commons-fileupload</groupId>

    <artifactId>commons-fileupload</artifactId>

    <version>1.3.1</version>

    </dependency>

    <dependency>

    <groupId>commons-io</groupId>

    <artifactId>commons-io</artifactId>

    <version>2.4</version>

    </dependency>

    <dependency>

    <groupId>commons-codec</groupId>

    <artifactId>commons-codec</artifactId>

    <version>1.9</version>

    </dependency>

    </dependencies>

    7:创建ssm框架用到的配置文件

    7.1:创建log4j文件。内容如下:

    #USE THIS SETTING FOR OUTPUT MYBATIS`s SQL ON THE CONSOLE
    

    log4j.rootLogger=DEBUG, Console
    

    #Console
    

    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    

    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    

    log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
    

    log4j.logger.java.sql.ResultSet=INFO
    

    log4j.logger.org.apache=INFO
    

    log4j.logger.java.sql.Connection=DEBUG
    

    log4j.logger.java.sql.Statement=DEBUG
    

    log4j.logger.java.sql.PreparedStatement=DEBUG
    

    7.2:在mybatis文件夹下创建mybatis-config.xml 文件,内容如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE configuration

    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-config.dtd">

    <configuration>

    <!-- 设置别名 -->

    <typeAliases>

    <package name="com.ssm.model" />

    </typeAliases>

    </configuration>

    7.3:在spring文件夹下创建spring-mvc.xml文件,内容如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

     

    <!-- 扫描controller(controller层注入) -->

    <context:component-scan base-package="com.ssm.controller"/>

     

    <!-- 对模型视图添加前后缀 -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"

    p:prefix="/WEB-INF/view/" p:suffix=".jsp"/>

    </beans>

    7.4:在spring文件夹中简历spring-mybatis.xml文件

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 定义数据源Bean -->

    <!-- Druid -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

    <property name="url" value="jdbc:mysql://localhost:3306/ssm" />

    <property name="username" value="root" />

    <property name="password" value="123456" />

    </bean>

    <!-- 注册SqlSessionFactoryBean -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />

    <!-- 自动扫描mappers.xml文件 -->

    <property name="mapperLocations" value="classpath:com/ssm/dao/*.xml" />

    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>

    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <property name="basePackage" value="com.ssm.dao" />

    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

    </bean>

    </beans>

    7.5:在spring中创建applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd">

     

    <!-- 引入jdbc配置文件 -->

    <bean id="propertyConfigurer"

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">

    <list>

    <value>classpath:properties/*.properties</value>

    <!--要是有多个配置文件,只需在这里继续添加即可 -->

    </list>

    </property>

    </bean>

     

     

     

    <!-- 配置数据源 -->

    <bean id="dataSource"

    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <!-- 不使用properties来配置 -->

    <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />

    <property name="url" value="jdbc:mysql://localhost:3306/learning" /> <property

    name="username" value="root" /> <property name="password" value="[email protected]"

    /> -->

    <!-- 使用properties来配置 -->

    <property name="driverClassName">

    <value>${jdbc.driverClassName}</value>

    </property>

    <property name="url">

    <value>${jdbc.url}</value>

    </property>

    <property name="username">

    <value>${jdbc.user}</value>

    </property>

    <property name="password">

    <value>${jdbc.password}</value>

    </property>

    </bean>

     

    <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <property name="basePackage" value="com.ssm.dao" />

    </bean>

     

    <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置 -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />

    <property name="mapperLocations" value="classpath*:com/ssm/mapper/*.xml" />

    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />

    <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"

    /> -->

    </bean>

     

    <!-- 自动扫描注解的bean -->

    <context:component-scan base-package="com.ssm.service" />

     

    </beans>

    7.6:在properties下创建jdbc.properties

    jdbc.driverClassName=com.mysql.jdbc.Driver

    jdbc.url=jdbc:mysql://localhost:3306/ssm

    jdbc.user=root

    jdbc.password=123456

  3. 创建java文件

    8.1 目录结构

    技术分享图片

     

    8.2 UserController

    package com.ssm.controller;

     

    import javax.annotation.Resource;

     

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.servlet.ModelAndView;

     

    import com.ssm.model.User;

    import com.ssm.service.UserService;

     

    @Controller

    public class UserController {

    @Resource

    private UserService userService;

     

    @RequestMapping("/")

    public ModelAndView getIndex(){

    ModelAndView mav = new ModelAndView("index");

    User user = userService.selectUserById(1);

    mav.addObject("user", user);

    return mav;

    }

    }

     

    8.3 UserDao

    package com.ssm.dao;

     

    import com.ssm.model.User;

     

    public interface UserDao {

     

    public User selectUserById(Integer userId);

     

    }

     

    8.4 usermapper.xml

    <?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.ssm.dao.UserDao">

    <!--设置domain类和数据库中表的字段一一对应,注意数据库字段和domain类中的字段名称不致,此处一定要!-->

    <resultMap id="BaseResultMap" type="com.ssm.model.User">

    <id column="USER_ID" property="userId" jdbcType="INTEGER" />

    <result column="USER_NAME" property="userName" jdbcType="CHAR" />

    <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" />

    <result column="USER_EMAIL" property="userEmail" jdbcType="CHAR" />

    </resultMap>

    <!-- 查询单条记录 -->

    <select id="selectUserById" parameterType="int" resultMap="BaseResultMap">

    SELECT * FROM t_user WHERE USER_ID = #{userId}

    </select>

    </mapper>

     

    8.5 User.java

    package com.ssm.model;

     

    public class User {

    private Integer userId;

    private String userName;

    private String userPassword;

    private String userEmail;

     

    public Integer getUserId() {

    return userId;

    }

     

    public void setUserId(Integer userId) {

    this.userId = userId;

    }

     

    public String getUserName() {

    return userName;

    }

     

    public void setUserName(String userName) {

    this.userName = userName;

    }

     

    public String getUserPassword() {

    return userPassword;

    }

     

    public void setUserPassword(String userPassword) {

    this.userPassword = userPassword;

    }

     

    public String getUserEmail() {

    return userEmail;

    }

     

    public void setUserEmail(String userEmail) {

    this.userEmail = userEmail;

    }

     

    @Override

    public String toString() {

    return "User [userId=" + userId + ", userName=" + userName

    + ", userPassword=" + userPassword + ", userEmail=" + userEmail

    + "]";

    }

     

    }

     

    8.6 UserService

    package com.ssm.service;

     

    import com.ssm.model.User;

    import org.springframework.stereotype.Service;

     

     

    public interface UserService {

    User selectUserById(Integer userId);

     

    }

     

    8.7 UserServiceImpl

     

    package com.ssm.service;

     

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.stereotype.Service;

     

    import com.ssm.dao.UserDao;

    import com.ssm.model.User;

     

    @Service

    public class UserServiceImpl implements UserService{

    @Autowired

    private UserDao userDao;

     

    public User selectUserById(Integer userId) {

    return userDao.selectUserById(userId);

     

    }

     

    }

     

    9:数据库搭建

    CREATE TABLE `t_user` (

    `USER_ID` int(11) NOT NULL AUTO_INCREMENT,

    `USER_NAME` char(30) NOT NULL,

    `USER_PASSWORD` char(10) NOT NULL,

    `USER_EMAIL` char(30) NOT NULL,

    PRIMARY KEY (`USER_ID`),

    KEY `IDX_NAME` (`USER_NAME`)

    ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8

     

     

     

    INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (1, ‘谢彦杰‘, ‘12345678‘, ‘[email protected]‘);

    INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (2, ‘eangie‘, ‘123‘, ‘[email protected]‘);

    INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (3, ‘eangie‘, ‘123‘, ‘[email protected]‘);

    INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (4, ‘eangie‘, ‘123‘, ‘[email protected]‘);

     

     

    10.在src/main/webapp/WEB-INF/view下创建index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

    </head>

    <body>

    <h2>Hello World!</h2>

    ${user.userId}<br>

    ${user.userName}<br>

    ${user.userPassword}

    </body>

    </html>

     

  4. web.xml更新

    <!-- 读取spring配置文件 -->

    <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:spring/applicationContext.xml</param-value>

    </context-param>

    <!-- 设计路径变量值 -->

    <context-param>

    <param-name>webAppRootKey</param-name>

    <param-value>springmvc.root</param-value>

    </context-param>

     

     

    <!-- Spring字符集过滤器 -->

    <filter>

    <filter-name>SpringEncodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

    <param-name>encoding</param-name>

    <param-value>UTF-8</param-value>

    </init-param>

    <init-param>

    <param-name>forceEncoding</param-name>

    <param-value>true</param-value>

    </init-param>

    </filter>

    <filter-mapping>

    <filter-name>SpringEncodingFilter</filter-name>

    <url-pattern>/*</url-pattern>

    </filter-mapping>

     

    <!-- 日志记录 -->

    <context-param>

    <!-- 日志配置文件路径 -->

    <param-name>log4jConfigLocation</param-name>

    <param-value>classpath:log4j.properties</param-value>

    </context-param>

    <context-param>

    <!-- 日志页面的刷新间隔 -->

    <param-name>log4jRefreshInterval</param-name>

    <param-value>6000</param-value>

    </context-param>

    <listener>

    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

    </listener>

     

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

     

    <!-- springMVC核心配置 -->

    <servlet>

    <servlet-name>dispatcherServlet</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

    <param-name>contextConfigLocation</param-name>

    <!--spingMVC的配置路径 -->

    <param-value>classpath:spring/spring-mvc.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

    </servlet>

    <!-- 拦截设置 -->

    <servlet-mapping>

    <servlet-name>dispatcherServlet</servlet-name>

    <url-pattern>/</url-pattern>

    </servlet-mapping>

     

    <!-- 错误跳转页面 -->

    <error-page>

    <!-- 路径不正确 -->

    <error-code>404</error-code>

    <location>/WEB-INF/errorpage/404.jsp</location>

    </error-page>

    <error-page>

    <!-- 没有访问权限,访问被禁止 -->

    <error-code>405</error-code>

    <location>/WEB-INF/errorpage/405.jsp</location>

    </error-page>

    <error-page>

    <!-- 内部错误 -->

    <error-code>500</error-code>

    <location>/WEB-INF/errorpage/500.jsp</location>

    </error-page>

    12.项目右键 runAs/run on Server

    技术分享图片

    然后一直点下去就行

以上是关于Eclipse+Maven 项目创建的主要内容,如果未能解决你的问题,请参考以下文章

如何使用eclipse创建Maven工程及其子模块

myeclipse 怎么配置maven web

eclipse创建maven项目报错!帮忙解决!

配合m2eclipse创建一个标准的maven web项目

eclipse 怎么创建maven jar包

用eclipse创建一个maven管理的项目,创建完成后pom.xml报错!!急求怎么解决!!