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. Android 线性布局 计算器

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. Bash脚本编程之数组

    数组简介 在bash脚本编程当中,变量是存储单个元素的内存空间:而数组是存储多个元素的一段连续的内存空间. 数组由数组名和下标构成,如下. ARRAY_NAME[SUBSCRIPT] 数组按照下标的类 ...

  3. python基础之字符串讲解(下)

    7.swapspace 这个命令是让大小写翻转 s = 'qwerQ' s3 = s.swapcase() print(s3) 8.title 每个隔开(特殊字符或者数字)的单词首字母大写 s = ' ...

  4. c++11多线程笔记

    1 thread类thread f;线程等待join()线程分离detach() thread类不可拷贝复制 std::this_thread::yield(); 2 bind 与lambda表达式 ...

  5. Java基础语法05-面向对象-封装-包-构造器-初始化

    封装 面向对象三大特性:封装.继承.多态 封装的好处 1.调用者:方便使用/简化使用 2.设计者:安全,可控 隐藏对象内部的复杂性,只对外公开简单的接口.便于外界调用,从而提高系统的可扩展性.可维护性 ...

  6. 前端最佳实践——DOM操作

    1.浏览器渲染原理 在讲DOM操作的最佳性能实践之前,先介绍下浏览器的基本渲染原理. 分为以下四个步骤: 解析HTML(HTML Parser) 构建DOM树(DOM Tree) 渲染树构建(Rend ...

  7. C lang:Pointer and Array

    Xx_Introduction Point and Array germane. Xx_Code #include<stdio.h> #define SIZE 4 int main(voi ...

  8. Abusing SUDO Advance for Linux Privilege Escalation

    Index What is SUDO? Scenario. Sudoer FIle Syntax. Exploiting SUDO zip tar strace tcpdump nmap scp ex ...

  9. diango url的命名和反向解析

    url的命名和反向解析 静态路由 url(r'^login/', views.login,name='login'), 反向解析ht 模板 {% url 'login' %} --> '/app ...

  10. Linux系统学习 十九、VSFTP服务—虚拟用户访问—为每个虚拟用户建立自己的配置文件,单独定义权限

    为每个虚拟用户建立自己的配置文件,单独定义权限 可以给每个虚拟用户单独建立目录,并建立自己的配置文件.这样方便单独配置权限,并可以单独指定上传目录 1.修改配置文件 vi /etc/vsftpd/vs ...