一、依赖传递
只添加了一个struts2-core依赖,发现项目中出现了很多jar,这种情况叫依赖传递
添加struts2-spring-plugin 2.3.24
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
</dependency>
添加spring-context 4.2.4
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
二、依赖版本冲突的解决
1、路径近者优先原则
自己添加jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
2、第一声明优先原则
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
</dependency>
3、排除原则
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
4、版本锁定原则
还可以把版本抽出来
如果,我们自己要写pom.xml的话,直接网站找一个。搜索ssh pom.xml或者ssm pom.xml。
把pom.xml直接贴到我们项目中。
创建数据库
编写实体类
public class Customer {
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;
}
编写映射文件Customer.hbm.xml。映射文件的位置,以前我们是和实体类放在一起。现在maven项目,要放到resources目录下。但是,在resources目录下要建立和实体类包名一样的路径,这样,编译之后,实体类和映射文件放到同一个地方了。
添加配置文件
添加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>
<!-- 会话工厂 -->
<session-factory>
<!-- 数据库方言,根据数据库选择 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--为了方便调试是否在运行hibernate时在日志中输出sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否对日志中输出的sql语句进行格式化 -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<!-- 加载映射文件 -->
<mapping resource="com/ken/crm/entity/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
添加spring配置文件
<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" xmlns:tx="http://www.springframework.org/schema/tx"
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/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">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/maven" />
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 依赖dataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- 创建工厂需要加载hibernate映射文件 -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
</beans>
创建dao
public interface CustomerDao {
/**
* 根据id获取客户
* @param id
* @return
*/
Customer findById(Long id);
}
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
@Override
public Customer findById(Long id) {
return this.getHibernateTemplate().get(Customer.class, id);
}
}
在dao的实现类中注入sessionFactory
<bean id="customerDao" class="com.ken.crm.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
dao junit测试
右击CustomerDaoImpl-new-junit test case在pom.xml中加入spring整合junit的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>provided</scope>
</dependency>
测试类
@RunWith(SpringJUnit4ClassRunner.class) // 使用junit4进行测试
@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) // 加载配置文件
public class CustomerDaoImplTest {
@Autowired
private CustomerDao customerDao;
@Test
public void test() {
Customer customer = customerDao.findById(1L);
System.out.println(customer);
}
}
测试成功
编写service
public interface CustomerService {
/**
* 根据id获取客户
* @param id
* @return
*/
Customer findById(Long id);
}
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
@Override
public Customer findById(Long id) {
return customerDao.findById(id);
}
}
<bean id="customerService" class="com.ken.crm.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao" />
</bean>
编写Action
public class CustomerAction extends ActionSupport {
private Long custId;
private Customer customer;
private CustomerService customerService;
public String findCustomerById() {
customer = customerService.findById(custId);
System.out.println("前端传过来的客户id====" + custId);
return SUCCESS;
}
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
}
配置spring
<bean id="customerAction" class="com.ken.crm.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService" />
</bean>
修改struts.xml
配置web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
启动项目,测试