频道栏目
首页 > 程序开发 > 软件开发 > Java > 正文
Eclipse + Maven 搭建 SSH 环境教程
2017-11-06 20:57:50         来源:fengwusan的专栏  
收藏   我要投稿

Eclipse + Maven 搭建 SSH 环境教程。

1、安装Maven,下载地址:https://mirrors.cnnic.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip,下载后,解压缩到D:盘,系统变量中添加M2_HOME=D:\apache-maven-3.3.9,编辑PATH变量,在末尾添加%M2_HOME%\bin。

在%M2_HOME%下创建repo文件夹,编辑%M2_HOME%\conf\settings.xml,添加配置

D:/apache-maven-3.3.9/repo

2、打开Eclipse,选择Help - Install New Software,在Work with输入框中输入:https://mirror.bit.edu.cn/eclipse/technology/m2e/releases/,这是国内的mirror,速度稍微快点,Pending后,选择安装Maven Integration for Eclipse,一路Next,漫长的安装结束后,重启Eclipse。

3、选择Window - Preferences - Maven - Installations - add,选择Maven安装目录,保存结果。

4、File - New - Project,选择Maven Project,Next,Next,漫长的Retrieving archetypes后,Catalog选择Maven Central,Filter输入:maven-archetype-webapp,Next,Group Id输入:com.moapp,Artifact Id输入工程名:mavenSSH,点击Finish。

5、编辑pom.xml

4.0.0

com.moapp

mavenSSH

1.0

mavenSSH

https://maven.apache.org

war

UTF-8

4.3.0.RELEASE

5.2.0.Final

2.5.1

2.7.5

2.5

junit

junit

4.12

test

org.apache.struts

struts2-core

2.5.1

javax.servlet

servlet-api

org.springframework

spring-core

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-test

${spring.version}

org.springframework

spring-orm

${spring.version}

org.hibernate

hibernate-core

${hibernate.version}

org.hibernate.javax.persistence

hibernate-jpa-2.0-api

1.0.1.Final

org.hibernate

hibernate-c3p0

${hibernate.version}

org.apache.struts

struts2-spring-plugin

${struts2.version}

org.apache.struts

struts2-convention-plugin

${struts2.version}

org.mortbay.jetty

servlet-api

3.0.20100224

log4j

log4j

1.2.17

org.apache.logging.log4j

log4j-core

${log4j.version}

org.apache.logging.log4j

log4j-api

${log4j.version}

org.apache.logging.log4j

log4j-web

${log4j.version}

mysql

mysql-connector-java

6.0.3

com.alibaba

fastjson

1.2.13

com.alibaba

druid

1.0.21

com.fasterxml.jackson.core

jackson-annotations

${jackson.version}

com.fasterxml.jackson.core

jackson-core

${jackson.version}

com.fasterxml.jackson.core

jackson-databind

${jackson.version}

org.aspectj

aspectjweaver

1.8.9

javax.servlet

jstl

1.2

mavenSSH

org.apache.maven.plugins

maven-surefire-plugin

2.19.1

true

org.apache.maven.plugins

maven-war-plugin

2.6

src/main/webapp/WEB-INF/web.xml

org.apache.maven.plugins

maven-javadoc-plugin

2.10.4

target/javadoc

target/javadoc

UTF-8

UTF-8

UTF-8

private

org.codehaus.cargo

cargo-maven2-plugin

1.5.0

tomcat7x

D:\tomcat

existing

D:\tomcat

保存pom.xml,系统自动下载jar及其依赖文件

6、编辑src/main/webapp/WEB-INF/web.xml

mavenSSH

contextConfigLocation

classpath:spring.xml

classpath:spring-hibernate.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

/*

openSessionInViewFilter

org.springframework.orm.hibernate5.support.OpenSessionInViewFilter

singleSession

true

org.springframework.web.context.ContextLoaderListener

org.springframework.web.util.IntrospectorCleanupListener

struts2

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

struts2

*.action

openSessionInViewFilter

*.action

30

/index.jsp

7、新建src/main/java/spring.xml

8、新建src/main/java/spring-hibernate.xml

${hibernate.dialect}

${hibernate.show_sql}

${hibernate.format_sql}

${hibernate.hbm2ddl.auto}

9、新建src/main/java/config.properties

#application configs

#jdbc druid config

validationQuery = SELECT 1

#mysql connect 6.x 使用com.mysql.cj.jdbc.Driver

jdbc.driverClassName = com.mysql.cj.jdbc.Driver

jdbc.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true

jdbc.username = devel

jdbc.password = 123456

#hibernate config

hibernate.dialect = org.hibernate.dialect.MySQLDialect

hibernate.show_sql = true

hibernate.format_sql = false

hibernate.hbm2ddl.auto = update

10、新建src/main/java/struts.xml

AUTOMATIC

11、新建src/main/java/log4j.properties

### set log levels ###

log4j.rootLogger = INFO , C , D , E

### console ###

log4j.appender.C = org.apache.log4j.ConsoleAppender

log4j.appender.C.Target = System.out

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

log4j.appender.C.layout.ConversionPattern = [mavenSSH][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n

### log file ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File = ../logs/ssh_mavenSSH.log

log4j.appender.D.Append = true

log4j.appender.D.Threshold = INFO

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

log4j.appender.D.layout.ConversionPattern = [mavenSSH][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n

### exception ###

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender

log4j.appender.E.File = ../logs/ssh_mavenSSH_error.log

log4j.appender.E.Append = true

log4j.appender.E.Threshold = ERROR

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

log4j.appender.E.layout.ConversionPattern = [mavenSSH][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n

12、新建src/main/java/log4j2.xml

13、建立mysql数据库test,新建表user,新建用户devel,密码123456,赋予devel对test数据库的所有权限

create database test default charset utf8 collate utf8_general_ci;

use test;

create table user (

id int auto_increment not null,

userName varchar(50) not null,

password char(32) not null,

gender tinyint not null,

primary key(id)

)ENGINE=MyISAM;

grant all privileges on test.* to 'devel'@'localhost' identified by '123456';

flush privileges;

14、bean/dao/service/action文件

com.moapp.mavenSSH.beans.User.java

package com.moapp.mavenSSH.beans;

import java.io.Serializable;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

@Entity(name = "user")

public class User implements Serializable {

private static final long serialVersionUID = 3834833794454172304L;

private int id;

private String userName;

private String password;

private int gender;

@Id

@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Column(name = "userName", nullable = false)

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

@Column(name = "password", nullable = false)

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Column(name = "gender", nullable = false)

public int getGender() {

return gender;

}

public void setGender(int gender) {

this.gender = gender;

}

}

com.moapp.mavenSSH.dao.BaseDao.java

package com.moapp.mavenSSH.dao;

import java.io.Serializable;

import java.util.List;

public interface BaseDao {

T load(PK id);

T get(PK id);

List findAll();

void persist(T entity);

PK save(T entity);

void saveOrUpdate(T entity);

void delete(PK id);

void flush();

}

com.moapp.mavenSSH.dao.UserDao.java

package com.moapp.mavenSSH.dao;

import com.moapp.mavenSSH.beans.User;

import com.moapp.mavenSSH.dao.BaseDao;

public interface UserDao extends BaseDao {

}

com.moapp.mavenSSH.daoImpl.UserDaoImpl.java

package com.moapp.mavenSSH.daoImpl;

import java.util.List;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

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

import org.springframework.stereotype.Repository;

import com.moapp.mavenSSH.beans.User;

import com.moapp.mavenSSH.dao.UserDao;

@Repository("UserDao")

public class UserDaoImpl implements UserDao{

@Autowired

private SessionFactory sessionFactory;

private Session getCurrentSession() {

return sessionFactory.getCurrentSession();

}

@Override

public User load(Integer id) {

return getCurrentSession().load(User.class, id);

}

@Override

public User get(Integer id) {

return getCurrentSession().get(User.class, id);

}

@SuppressWarnings("unchecked")

@Override

public List findAll() {

return getCurrentSession().createQuery("from user").getResultList();

}

@Override

public void persist(User entity) {

getCurrentSession().persist(entity);

}

@Override

public Integer save(User entity) {

return (Integer)getCurrentSession().save(entity);

}

点击复制链接 与好友分享!回本站首页
上一篇:观察者设计模式 Vs 事件委托
下一篇:Java8 Stream流操作在用户系统中的妙用
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站