Maven搭建SSH环境

一、新建maven项目

Maven搭建SSH环境

选中maven-aechetype-webapp

Maven搭建SSH环境

group Id一般为公司域 ;Artifact Id相当于项目名称 ;version不需要更改;Package不使用默认,手动清空

Finish

项目结构如图 报错是因为没有加入servlet API 暂时不用管。

Maven搭建SSH环境

二、配置pom.xml

  1    <properties>
<!-- 文件拷贝编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 输出编码 -->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
8    </properties>
<dependencies>
<!-- log4j 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1-b09</version>
<scope>provided</scope>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
<!-- mchange C3P0 数据源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5-pre2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.29</version>
</dependency>
<!-- sf.json -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier><!--指定jdk版本 -->
</dependency>
<!-- struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.14</version>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.14</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.14</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.0.Final</version>
</dependency>
</dependencies>

保存,Maven会自动联网去下载所有需要的jar包,只写jar的名字,不用记住,只要去Maven*仓库去搜索,Maven*仓库地址:http://search.maven.org/完成之后,多了很多jar,并且都放到了配置的本地仓库中,以后要再使用相同版本的jar时候,就不需要联网下载了。

三、修改项目结构

因为构建的MSSH项目的结构并不是一个标准的Maven结构,我们需要手动新建几个Source Folder

Maven搭建SSH环境

这是标准的maven项目结构

src/main/java:存放java源文件

src/main/resources:存放项目配置文件,如spring.xml,hibernate.cfg.xml。。。

src/test/java:存放test的java文件

src/test/resources:存放test时候所需的配置文件

配置Source Folder的output folder

Maven搭建SSH环境

同步项目的jdk版本

Maven搭建SSH环境

Maven搭建SSH环境Maven搭建SSH环境

设置web.xml的路径

Maven搭建SSH环境

further configuration available若没有出现 可以先去掉dynamic web module的勾 apply之后再打上勾 就会出现此选项 设置路径为src/main/webapp

四、编写配置文件

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name></display-name> <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context
Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
</context-param> <!-- spring 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Filter 定义 -->
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Hibernate的session丢失解决方法 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- struts2 filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

database.properties

hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
url=jdbc:mysql://localhost:3306/mssh?useUnicode=true&characterEncoding=UTF-8
username=root
password=root hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false

spring.xml

扫描com.shi包下的文件

<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"> <!-- 引入属性文件 -->
<context:property-placeholder location="classpath:database.properties" /> <!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.shi" /> </beans>

spring-hibernate.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- 使用C3P0数据源,MySQL数据库 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- MySQL5 -->
<property name="driverClass" value="${driverClassName}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
</bean> <!-- session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.shi.*.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean> <!-- 配置事务 -->
<bean name="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" /> </beans>

struts.xml

使用零配置,扫描com.shi包下的文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 将Action交给spring容器管理 -->
<constant name="struts.objectFactory" value="spring" /> <!-- 零配置 -->
<constant name="struts.convention.package.locators" value="shi" />
<constant name="struts.convention.package.locators.basePackage" value="com" /> <!-- 模式为开发模式,修改xml配置文件会自动加载,项目交付的时候设置为false,以免影响性能 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 字符集编码 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<package name="defaultPackage" namespace="/" extends="struts-default"> </package>
</struts>

五、使用

@Entity
@Table(name = "DAE_USER")
public class User extends ObjectEntity{ private static final long serialVersionUID = 1L; private String name;
private String password;
private List<Role> roleList = new ArrayList<Role>(); public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} // 多对多定义
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "DAE_USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
public List<Role> getRoleList() {
return roleList;
} public void setRoleList(List<Role> roleList) {
this.roleList = roleList;
} public void addRole(Role role){
this.roleList.add(role);
} }

User

@Component
public class UserDao{ @Autowired
private SessionFactory sessionFactory; public Session getCurrentSession(){
return sessionFactory.getCurrentSession();
} public void save(User entity) {
getCurrentSession().save(entity);
} public void update(User entity) {
getCurrentSession().update(entity);
} public void delete(String id) {
getCurrentSession().delete(this.get(id));;
} public User get(String id) {
String hql = "from User where id = '"+id+"'";
return (User) getCurrentSession().createQuery(hql).uniqueResult();
} @SuppressWarnings("unchecked")
public List<User> getAll() {
String hql = "from User";
return getCurrentSession().createQuery(hql).list();
} }

UserDao

@Service
@Transactional
public class UserManager extends ServiceManager<User> { @Autowired
private UserDao userDao; @Override
public void save(User entity) throws Exception {
if (entity != null) {
entity.initDate();
userDao.save(entity);
} else {
throw new Exception("保存出错");
}
} @Override
public void update(User entity) throws Exception {
if (entity != null) {
entity.initDate();
userDao.update(entity);
} else {
throw new Exception("修改出错");
}
} @Override
public void delete(String id) throws Exception {
userDao.delete(id);
} @Override
public User get(String id) throws Exception {
return userDao.get(id);
} @Override
public List<User> getAll() throws Exception {
return userDao.getAll();
}
}

UserManager

public class UserAction extends CrudActionSupport<User> {

    private static final long serialVersionUID = 1L;

    @Autowired
private UserManager userManager;
@Autowired
private RoleManager roleManager; private String id;
private User entity; @Override
public User getModel() {
return entity;
} @Override
protected void prepareModel() throws Exception {
if (!EmptyUtil.isEmpty(id)) {
entity = userManager.get(id);
} else {
entity = new User();
}
} @Override
public String list() throws Exception {
List<User> userList = userManager.getAll();
for(User user :userList){
System.out.println(user.getName());
}
return SUCCESS;
} @Override
public String input() throws Exception {
return "input";
} @Override
public String save() throws Exception {
this.userManager.save(entity);
return null;
} @Override
public String delete() throws Exception {
return null;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public User getEntity() {
return entity;
} public void setEntity(User entity) {
this.entity = entity;
} }

UserAction

上一篇:基于 Webpack 4 搭建 Vue 开发环境


下一篇:CentOS 7 安装.NET Core 2.0