版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協定,轉載請附上原文出處連結和本聲明。
本文連結:https://blog.csdn.net/zhao1299002788/article/details/101757461
2.1搭建前提
我們在搭建CRM開發環境之前,需要明确2件事情:
a、我們搭建環境采用基于注解的配置。
b、搭建環境需要測試,我們以客戶的儲存和清單查詢作為測試功能。
2.2搭建步驟
2.2.1導入Spring和Hibernate的jar包
Hibernate基本jar包(包括了資料庫驅動)
C3P0的jar包
Spring的IoC,AOP和事務控制必備jar包
2.2.2建立實體類并使用注解映射
/**
* 客戶的實體類
*
* 明确使用的注解都是JPA規範的
* 是以導包都要導入javax.persistence包下的
*
*/
@Entity//表示目前類是一個實體類
@Table(name="cst_customer")//建立目前實體類和表之間的對應關系
public class Customer implements Serializable {
@Id//表明目前私有屬性是主鍵
@GeneratedValue(strategy=GenerationType.IDENTITY)//指定主鍵的生成政策
@Column(name="cust_id")//指定和資料庫表中的cust_id列對應
private Long custId;
@Column(name="cust_name")//指定和資料庫表中的cust_name列對應
private String custName;
@Column(name="cust_industry")//指定和資料庫表中的cust_industry列對應
private String custIndustry;
@Column(name="cust_source")
private String custSource;
@Column(name="cust_level")
private String custLevel;
@Column(name="cust_address")//指定和資料庫表中的cust_address列對應
private String custAddress;
@Column(name="cust_phone")//指定和資料庫表中的cust_phone列對應
private String custPhone;
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 String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
@Override
public String toString() {
return "Customer [custId=" + custId + ", custName=" + custName
+ ", custIndustry=" + custIndustry + ", custAddress="
+ custAddress + ", custPhone=" + custPhone + "]";
}
}
2.2.3編寫業務層接口及實作類
/**
* 客戶的業務層接口
* 一個功能子產品一個service
*/
public interface ICustomerService {
/**
* 儲存客戶
* @param customer
*/
void saveCustomer(Customer customer);
/**
* 查詢所有客戶資訊
* @return
*/
List<Customer> findAllCustomer();
}
/**
* 客戶的業務層實作類
*/
@Service("customerService")
@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
public class CustomerServiceImpl implements ICustomerService {
@Autowired
private ICustomerDao customerDao;
@Override
public void saveCustomer(Customer customer) {
customerDao.save(customer);
}
@Override
@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public List<Customer> findAllCustomer() {
return customerDao.findAllCustomer();
}
}
2.2.4編寫持久層接口及實作類
/**
* 客戶的持久層接口
*/
public interface ICustomerDao extends IBaseDao<Customer>{
/**
* 查詢所有客戶
* @return
*/
List<Customer> findAllCustomer();
/**
* 儲存客戶
* @param customer
* @return
*/
void saveCustomer(Customer customer);
}
/**
* 客戶的持久層實作類
*/
@Repository("customerDao")
public class CustomerDaoImpl implements ICustomerDao {
@Autowired
private HibernateTemplate hibernateTemplate;
@Override
public void saveCustomer(Customer customer) {
hibernateTemplate.save(cusotmer);
}
@Override
public List<Customer> findAllCustomer() {
return (List<Customer>) hibernateTemplate.find("from Customer ");
}
}
2.2.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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">
<!-- 配置spring運作要掃描的包 -->
<context:component-scan base-package="com.baidu"></context:component-scan>
<!-- 開啟spring對注解事務的支援 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate5.HibernateTemplate">
<!-- 注入SessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 注入SessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 1、連接配接資料庫的 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 2、hibernate基本配置的 -->
<property name="hibernateProperties">
<props>
<!-- 資料庫的方言-->
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 是否顯示sql語句-->
<prop key="hibernate.show_sql">true</prop>
<!-- 是否格式化sql語句-->
<prop key="hibernate.format_sql">false</prop>
<!-- 采用何種方式生成資料庫表結構 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 是spring把sesion綁定到目前線程上的配置 -->
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate5.SpringSessionContext
</prop>
</props>
</property>
<!-- 3、指定掃描映射注解的包-->
<property name="packagesToScan">
<array>
<value>com.baidu.domain</value>
</array>
</property>
</bean>
<!-- 配置資料源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///crm"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>
2.2.6導入junit的jar包
2.2.7配置spring整合junit的測試類并測試
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:config/spring/applicationContext.xml"})
public class CustomerServiceTest {
@Autowired
private ICustomerService customerService;
@Test
public void testFindAll(){
customerService.findAllCustomer();
}
@Test
public void testSave(){
Customer c = new Customer();
c.setCustName("傳智學院 ");
customerService.saveCustomer(c);
}
}
2.2.8導入jsp頁面
2.2.9導入struts2的jar包
2.2.10編寫Action并配置
/**
* 客戶的動作類
*
*/
@Controller("customerAction")
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/customer")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer();
@Autowired
private ICustomerService customerService ;
@Override
public Customer getModel() {
return customer;
}
/**
* 添加客戶
* @return
*/
@Action(value="addCustomer",results={
@Result(name="addCustomer",type="redirect",location="/jsp/success.jsp")
})
public String addCustomer(){
customerService.saveCustomer(customer);
return "addCustomer";
}
/**
* 擷取添加客戶頁面
* @return
*/
@Action(value="addUICustomer",results={ @Result(name="addUICustomer",type="dispatcher",location="/jsp/customer/add.jsp")
})
public String addUICustomer(){
return "addUICustomer";
}
private List<Customer> customers;
@Action(value="findAllCustomer",results={ @Result(name="findAllCustomer",type="dispatcher",location="/jsp/customer/list.jsp")
})
public String findAllCustomer(){
page = customerService.findAllCustomer(dCriteria,num);
return "findAllCustomer";
}
public List<Customer> getCustomers() {
return customers;
}
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
}
2.2.11改造jsp頁面
略。設計的jsp有:menu.jsp add.jsp list.jsp
2.2.12編寫web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- 配置監聽器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 手動指定spring配置檔案的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring/applicationContext.xml
</param-value>
</context-param>
<!-- 配置解決Nosession問題的過濾器 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置struts2的核心控制,同時需要手動指定struts2的配置檔案位置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<!-- 手動指定struts2配置檔案的位置 -->
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,struts-plugin.xml,config/struts/struts.xml
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.2.13測試環境搭建是否成功
我們可以通過浏覽器位址欄輸入網址來檢視是否搭建成功:
http://localhost:8080/寫自己的項目名稱
複制