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. webpack构建原理和实现简单webpack

    webpack打包原理分析 基础配置,webpack会读取配置 (找到入口模块) 如:读取webpack.config.js配置文件: const path = require("path& ...

  2. 控制DIV移动

    键盘控制DIV移动 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  3. Java工作流引擎jflow对流程的结束大总结

    关键字: 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 表单引擎 工作流功能说明  工作流设计 工作流快速开发平台   业务流程管理   bpm工作流系统  java工 ...

  4. CentOS 上配置 lua 的服务器环境(enet)

    安装 lua & luarocket 安装依赖 $ yum install gcc gcc-c++ kernel-devel $ yum install readline-dev $ yum ...

  5. 【机器学习实战】计算两个矩阵的成对距离(pair-wise distances)

    矩阵中每一行是一个样本,计算两个矩阵样本之间的距离,即成对距离(pair-wise distances),可以采用 sklearn 或 scipy 中的函数,方便计算. sklearn: sklear ...

  6. 利用Fiddler对Jmeter的请求进行抓包

    前言 有时候,为了得到更详细的请求结果,我们可能需要使用Fiddler结合Jmeter来抓包分析,从而更好的辅助测试. 遇到的问题 这里以一个获取学生信息的接口为例进行说明. 当我在Jmeter里按接 ...

  7. 【HTML】--- 列表、表格、form表单标签

    Html常用标签(2) 上篇博客讲了些常用的html标签 :[HTML]---常用标签(1) 这里主要讲 列表.表格标签和 form表单标签. 一.列表.表格标签 1.列表标签 概念 把内容以列表的形 ...

  8. sklearn集成支持向量机svm.SVC参数说明

    经常用到sklearn中的SVC函数,这里把文档中的参数翻译了一些,以备不时之需. 本身这个函数也是基于libsvm实现的,所以在参数设置上有很多相似的地方.(PS: libsvm中的二次规划问题的解 ...

  9. Navicat远程连接MySQL8,必知防坑策略

    项目上线是每一个开发工程师面临收获前面抓紧时间开发的成果,但有时我们上线项目首先需要做一些相关的业务测试.通过Xshell远程连接后使用命令行的方式连接操作Mysql这个没什么太大的你问题.但每次通过 ...

  10. Spring 框架下的 JDBC

    Spring JDBC Spring 对JDBC技术规范做了进一步封装,它又叫Spring JDBCTemplate(jdbc模板技术) 纯JDBC:代码清晰的.效率最高.代码是最烦的.    Spr ...