一、maven的拆分与聚合
一个完整的早期开发好的crm项目,现在要使用maven工程对它进行拆分,这时候就可以将dao拆解出来形成表现独立的工程,同样service,action也都这样拆分
工程拆分之后,将来还要聚合(聚合就是将拆分的工程进一步组合在一起,又形成一个完整的项目)为了达到聚合的目标,今天需要的文件有
父工程(maven project)
子模块(maven module) dao ,service, web
开发步骤:
1. 创建一个maven父工程
从它的目录结构可以看出,父工程本身不写代码,它里面有一个pom.xml文件,这个文件可以将多个子模块中通用的jar所对应的坐标,集中在父工程中配置,将来的子模块就可以不需要在pom.xml中配置通用jar的坐标了
2、创建这个父工程的一个子模块?
二、冲突的解决
1、通过添加<exclusion>标签来解决冲突
在父工程中引入了struts-core,hibernate-core,就发现jar包是有冲突的。Javassist存在版本上冲突问题
点击next
2、使用版本锁定解决(推荐)
首先父工程中pom.xml文件添加:
然后,在使用坐标时,对于同一个框架,引入多次时,它的版本信息就会多次出现,所以可以借用常量的思想,将这些版本号提取出来,在需要用到的时候,直接写版本的常量名称就可以了。
三、编写dao和service以及web三个子模块
前提 创建dao模块->service模块引用dao的jar项目->web引用service的jar项目
重点:各个子模块的内容的聚合
1、编写domain中的Customer实体类
package cn.chuantao.crm.domain;
import java.io.Serializable;
/**
* po的规范 (Persistent Object 持久化对象)
* 1.公有类
* 2.私有属性
* 3.公有的getter与setter
* 4.不能使用final修饰
* 5.提供默认无参构造
* 6.如果是基本类型,就写它对应的包装类
* 7.一般都要实现java.io.Serializable
* @author Administrator
*
*/
public class Customer implements Serializable {
private Long custId;
private String custName;
private Long custUserId;
private Long custCreateId;
private String custIndustry;
private String custLevel;
private String custLinkman;
private String custPhone;
private String custMobile;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public Long getCustUserId() {
return custUserId;
}
public void setCustUserId(Long custUserId) {
this.custUserId = custUserId;
}
public Long getCustCreateId() {
return custCreateId;
}
public void setCustCreateId(Long custCreateId) {
this.custCreateId = custCreateId;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustLinkman() {
return custLinkman;
}
public void setCustLinkman(String custLinkman) {
this.custLinkman = custLinkman;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustMobile() {
return custMobile;
}
public void setCustMobile(String custMobile) {
this.custMobile = custMobile;
}
}
同时在src目录下写dao接口和实现类CustomerDaoImp(举例为查询所有客户)
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
public List<Customer> findAll() {
return (List<Customer>)this.getHibernateTemplate().find("from Customer");
}
}
2、第二步在dao子模块的resource部分写配置文件。包括(Spring domain Hibernate三个)
Hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
//将DataSource交给Spring去管理
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="hbm2ddl.auto">none</property>
<!-- 懒加载,配合web.xml中配置的 openSessionInViewFilter -->
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<!--校验模式 JPA java persistent api-->
<property name="javax.persistence.validation.mode">none</property>
<!-- 加载映射文件-->
<mapping resource="cn/chuantao/crm/domain/Customer.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
applicationContext-dao.xml:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/crm?characterEncoding=utf8" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- CustomerDao -->
<bean id="customerDao" class="cn.chuantao.crm.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
实体类的映射暂时不写了,这里dao层的子模块完毕。下面写一下service层子模块
3、service层的配置
3.1 继承dao项目的jar包形式
<parent>
<groupId>cn.itcast.crm</groupId>
<artifactId>ssh_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ssh_service</artifactId>
<dependencies>
<dependency>
<groupId>cn.itcast.crm</groupId>
<artifactId>ssh_dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3.2 写service的接口和CustomerServiceImp实现类
3.3在resource中写配置文件
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* cn.chuantao.crm.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<!-- service -->
<bean id="customerService" class="cn.chuantao.crm.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
4、web子模块的配置
4.1 写webapp 和 web.xml文件 配置信息为
<!--spring配置文件的加载的监听 器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2.懒加载 OpenSessionInviewFilter noSession or session is closed-->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3.struts2核心控制器-->
<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>
</web-app>
4.2 Action实现类
public class CustomerAction extends ActionSupport {
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
/**
* 查询所有数据
*/
public String execute() throws Exception {
List<Customer> list = customerService.findAll();
System.out.println(list.size());
ActionContext.getContext().put("list", list);
return SUCCESS;
}
}
4.3 编写 Spring-Strutsxml文件
<bean id="customerAction" class="cn.itcast.crm.web.CustomerAction">
<property name="customerService" ref="customerService"></property>
</bean>
4.4 struts2 配置文件
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="customerAction_*" class="customerAction" method="{1}">
<result>/success.jsp</result>
</action>
</package>
</struts>
4.5 总的配置文件Spring
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath:applicationContext-action.xml"/>
<import resource="classpath:applicationContext-service.xml"/>
<import resource="classpath:applicationContext-dao.xml"/>
</beans>
5、执行顺序
Struts 在web中加入web过滤监听器->>applictionContext.xml->>加载<import resource="classpath*:三个位置(web service dao 的Spring配置文件)当dao的application-dap.xml加载时->>DataSource资源"->>Hibernate.cfg.xml文件加载->>映射文件.hbm.xml加载 懒加载 strut
四、私服的搭建和下载资源
下载 nexus的压缩包->>解压->>进入bin目录安装.bat文件->>设置电脑服务管理->>启动nexus 完成
用户名和密码 admin admin123
主要的仓库:
1. hosted,宿主仓库,部署自己的jar到这个类型的仓库,包括releases和snapshot两部分,Releases公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
2. proxy,代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件。
3. group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组。
nexus仓库默认在sonatype-work目录中:
配置:
第一步: 需要在客户端即部署dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 。
此用户名和密码用于私服校验,因为私服需要知道上传都 的账号和密码 是否和私服中的账号和密码 一致。
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
注:
releases 连接发布版本项目仓库
snapshots 连接测试版本项目仓库
第二步: 配置项目pom.xml
配置私服仓库的地址,本公司的自己的jar包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库。pom.xml这里<id> 和 settings.xml 配置 <id> 对应!
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
测试:将项目dao工程打成jar包发布到私服:
1、首先启动nexus2、对dao工程执行deploy命令
从私服下载jar包
意义:没有配置nexus之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,有了私服本地项目首先去本地仓库找jar,如果没有找到则连接私服从私服下载jar包,如果私服没有jar包私服同时作为代理服务器从中央仓库下载jar包,这样做的好处是一方面由私服对公司项目的依赖jar包统一管理,一方面提高下载速度,项目连接私服下载jar包的速度要比项目连接中央仓库的速度快的多。
管理仓库组
nexus中包括很多仓库,hosted中存放的是企业自己发布的jar包及第三方公司的jar包,proxy中存放的是中央仓库的jar,为了方便从私服下载jar包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载jar包。打开nexus配置仓库组,如下图:
在setting中配置
在客户端的setting.xml中配置私服的仓库,由于setting.xml中没有repositories的配置标签需要使用profile定义仓库。
<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
//使用profile定义仓库需要激活才可生效。
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>