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/写自己的项目名称

CRM第一篇的更多相关文章

  1. CRM第一篇:权限组件之权限控制

    一.权限组件(1):一级菜单 二.权限组件(2):二级菜单 三.权限组件(3):默认选中非菜单(二级菜单) 四.权限组件(4):给动态菜单增加面包屑导航 五.权限组件(5):权限粒度控制到按钮 六.权 ...

  2. 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)

    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...

  3. Python爬虫小白入门(四)PhatomJS+Selenium第一篇

    一.前言 在上一篇博文中,我们的爬虫面临着一个问题,在爬取Unsplash网站的时候,由于网站是下拉刷新,并没有分页.所以不能够通过页码获取页面的url来分别发送网络请求.我也尝试了其他方式,比如下拉 ...

  4. Three.js 第一篇:绘制一个静态的3D球体

    第一篇就画一个球体吧 首先我们知道Three.js其实是一个3D的JS引擎,其中的强大之处就在于这个JS框架并不是依托于JQUERY来写的.那么,我们在写这一篇绘制3D球体的文章的时候,应该注意哪些地 ...

  5. 深入学习jQuery选择器系列第一篇——基础选择器和层级选择器

    × 目录 [1]id选择器 [2]元素选择器 [3]类选择器[4]通配选择器[5]群组选择器[6]后代选择器[7]兄弟选择器 前面的话 选择器是jQuery的根基,在jQuery中,对事件处理.遍历D ...

  6. 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  7. Android基础学习第一篇—Project目录结构

    写在前面的话: 1. 最近在自学Android,也是边看书边写一些Demo,由于知识点越来越多,脑子越来越记不清楚,所以打算写成读书笔记,供以后查看,也算是把自己学到所理解的东西写出来,献丑,如有不对 ...

  8. 深入理解ajax系列第一篇——XHR对象

    × 目录 [1]创建对象 [2]发送请求 [3]接收响应[4]异步处理[5]实例演示 前面的话 ajax是asynchronous javascript and XML的简写,中文翻译是异步的java ...

  9. 深入理解javascript对象系列第一篇——初识对象

    × 目录 [1]定义 [2]创建 [3]组成[4]引用[5]方法 前面的话 javascript中的难点是函数.对象和继承,前面已经介绍过函数系列.从本系列开始介绍对象部分,本文是该系列的第一篇——初 ...

随机推荐

  1. Caffe源码-Net类(上)

    Net类简介 Net类主要处理各个Layer之间的输入输出数据和参数数据共享等的关系.由于Net类的代码较多,本次主要介绍网络初始化部分的代码.Net类在初始化的时候将各个Layer的输出blob都统 ...

  2. 初识PMP PMBOK初解(指南第一章引论)

    引论 1.1指南概述和目的 普遍认可:大多数时候是适用于大多数项目,价值和有效性已获得一致认可. 良好实践:知识.技能.工具和技术能够达到预期的商业价值和成果,提高很多项目成功的可能性. 确定过程.输 ...

  3. java基础 - 锁

    ------------------------ 参考: https://www.cnblogs.com/hustzzl/p/9343797.html https://blog.csdn.net/qq ...

  4. OS之进程和线程

    1. 线程是程序执行流的最小单元. 一个标准的线程由线程ID,当前指令指针PC,寄存器集合和堆栈组成. 2. 通常意义上,一个进程由一个到多个线程组成,各个线程之间共享程序的的内存空间,包括代码段.数 ...

  5. 数据安全管理:RSA加密算法,签名验签流程详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.RSA算法简介 1.加密解密 RSA加密是一种非对称加密,在公开密钥加密和电子商业中RSA被广泛使用.可以在不直接传递密钥的情况下,完成加 ...

  6. bootstrap-table 常用总结-树形结构(展开和折叠)

    今天在工作的时候,遇到了一个需求,就是需要一键展开或者关闭树形结构.关于树形结构的不是很熟悉,然后去百度,结果也不是很准确.最后经过Google才找到.下面分享给大家 直接看代码: var flag ...

  7. angular cli + primeNG

    目录: 1.安装  angular cli 2.创建项目 3.构建路由 4.新建组件 5.组件之间的通信 6.引入primeNG 7.修改primeNG组件样式 8.问题 -------------- ...

  8. Java学习笔记--线程day01

    线程的概念:一个线程是进程的顺序执行流: 同类的多个线程共享一块内存空间和一组系统资源,线程本身有一个供程序执行时的堆栈.线程在切换时负荷小,因此,线程也被称为轻负荷进程.一个进程中可以有多个线程. ...

  9. swift为什么不是do while?

    swift为什么不是do while do while已经深入人心之后,对repeat while的语义逻辑该如何接受呢? do while的语义逻辑是:这个whilte循环先执行一次.逻辑目标针对w ...

  10. Visual Studio安装工具和安装组件下载速度慢的问题

    下载安装Visual Studio时在下面这个界面下载时速度只有几十KB每秒 解决办法:修改Host文件,在Host文件中添加下面代码,然后保存即可 110.53.72.104 download.vi ...