客户关系管理系统框架搭建(二)

时间:2018-02-18 10:15:02
【文件属性】:

文件名称:客户关系管理系统框架搭建(二)

文件大小:20.31MB

文件格式:ZIP

更新时间:2018-02-18 10:15:02

框架搭建

crm项目的架构 * 创建web工程 * 引入jar包 * mysql的驱动包 * hibernate需要的jar包 * spring需要的jar包 * struts2需要的jar包 * jstl 需要的jar包 * junit需要的jar包 * 包的介绍 * cn.itcast.crm.container:重新封装spring容器 * cn.itcast.crm.dao:放置的是dao接口 * cn.itcast.crm.dao.impl:放置的是dao接口的实现类 * cn.itcast.crm.domain:放置的是po类、po类和数据库表关联的映射文件 * cn.itcast.crm.service:业务层的接口 * cn.itcast.crm.service.impl:业务层的接口的实现类 * cn.itcast.crm.web.action:struts2的action * cn.itcast.crm.web.form:封装页面数据的javaBean * junit:开发人员测试用的 * 搭建hibernate层 * 定义需求:部门信息的页面数据要插入到数据库中 * 定义表 CREATE TABLE `sys_user_group` ( `id` INTEGER(11) NOT NULL AUTO_INCREMENT, #编号 `remark` TEXT, #备注 `name` VARCHAR(100) DEFAULT NULL, #部门名称 `principal` VARCHAR(50) DEFAULT NULL, #部门负责人 `incumbent` VARCHAR(200) DEFAULT NULL, #部门职能 PRIMARY KEY (`id`) ) * 创建po对象,放置在cn.itcast.crm.domain包下 public class SysUserGroup implements java.io.Serializable { private Integer id; private String remark; // 备注 private String name; // 部门名称 private String principal; // 部门负责人 private String incumbent; // 部门职能 } * 创建表和po之间的映射文件SysUserGroup.hbm.xml 放置在cn.itcast.crm.domain包下 * SysUserGroup.hbm.xml文件的内容直接看该文件即可 * 创建hibernate.cfg.xml文件连接数据库,加载SysUserGroup.hbm.xml文件,放置src下 root root jdbc:mysql://localhost:3306/itcast0807crm com.mysql.jdbc.Driver true org.hibernate.dialect.MySQL5Dialect update true * 测试.放置在junit包下 public class TestHibernate { @Test public void testHibernateConf(){ Configuration config=new Configuration(); config.configure(); SessionFactory sf=config.buildSessionFactory(); Session s=sf.openSession(); Transaction tx=s.beginTransaction(); SysUserGroup sysUserGroup=new SysUserGroup(); sysUserGroup.setName("销售部"); sysUserGroup.setPrincipal("xxx"); sysUserGroup.setIncumbent("ttt"); s.save(sysUserGroup); tx.commit(); s.close(); } } * 搭建spring和hibernate * 创建beans.xml文件,放置在src下 * 引入命名空间 bean tx context aop * 配置如下: classpath:hibernate.cfg.xml * 创建dao层共同的接口,放置在cn.itcast.crm.dao下 public interface ICommonDao { public void save(T entity); } * 创建dao层共同的接口的实现类,,放置在cn.itcast.crm.dao.impl下 public class CommonDaoImpl extends HibernateDaoSupport implements ICommonDao { public void save(T entity) { this.getHibernateTemplate().save(entity); } @Resource(name="sessionFactory") //注入sessionFactory public void setSessionFactoryDI(SessionFactory sessionFactory) { System.out.println("sessionFactory "+sessionFactory); //调用父类的setSessionFactory方法,注入sessionFactory super.setSessionFactory(sessionFactory); } } * 创建部门的dao接口 SysUserGroupDao接口 放在cn.itcast.crm.dao包下 public interface ISysUserGroupDao extends ICommonDao { public final static String SERVICE_NAME="cn.itcast.crm.dao.impl.SysUserGroupDaoImpl"; } * 创建部门的dao接口的实现类 放在cn.itcast.crm.dao.impl包下 @Repository(ISysUserGroupDao.SERVICE_NAME) public class SysUserGroupDaoImpl extends CommonDaoImpl implements ISysUserGroupDao { } * 测试,放置在junit包下 public class TestSysUserGroupDao { @Test public void testSave() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ISysUserGroupDao sysUserGroupDao = (ISysUserGroupDao) ctx.getBean(ISysUserGroupDao.SERVICE_NAME); SysUserGroup sysUserGroup = new SysUserGroup(); sysUserGroup.setName("销售部"); sysUserGroup.setPrincipal("xxx"); sysUserGroup.setIncumbent("ttt"); sysUserGroupDao.save(sysUserGroup); } } * 创建部门的业务层接口 ,放置在cn.itcast.crm.service包下 public interface ISysUserGroupService { public final static String SERVICE_NAME="cn.itcast.crm.service.impl.SysUserGroupServiceImpl"; public void saveSysUserGroup(SysUserGroup sysUserGroup); } * 创建部门的业务层接口实现类,放置在cn.itcast.crm.service.impl包下 @Transactional(readOnly=true) @Service(ISysUserGroupService.SERVICE_NAME) public class SysUserGroupServiceImpl implements ISysUserGroupService { @Resource(name=ISysUserGroupDao.SERVICE_NAME) private ISysUserGroupDao sysUserGroupDao; @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false) public void saveSysUserGroup(SysUserGroup sysUserGroup) { sysUserGroupDao.save(sysUserGroup); } } * 测试 public class TestSysUserGroupService { @Test public void testSavex() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ISysUserGroupService sysUserGroupService = (ISysUserGroupService) ctx.getBean(ISysUserGroupService.SERVICE_NAME); SysUserGroup sysUserGroup = new SysUserGroup(); sysUserGroup.setName("销售部"); sysUserGroup.setPrincipal("xxx"); sysUserGroup.setIncumbent("ttt"); sysUserGroupService.saveSysUserGroup(sysUserGroup); } } * 搭建struts2(保存部门信息) * jsp页面(sys/group/add.jsp) * 请求的路径/sys/sysUserGroupAction_save.do * 根据路径创建SysUserGroupAction和在action中增加save方法,放置在cn.itcast.crm.web.action包下 public class SysUserGroupAction extends ActionSupport public String save() throws IllegalAccessException, InvocationTargetException{ System.out.println("xxxxxxxxxxxxxxxxxxxxxxxx "); return null; } } * 建立请求路径和action之间的关联 * 创建struts.xml文件,放置在src下 * 文件内容如下: /sys/group/add.jsp * 在web.xml文件配置struts2的过滤器 StrutsPrepareAndExecuteFilter org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter /* * 测试 * 定义javaBean(vo对象)接收表单数据,放置在cn.itcast.crm.web.form包下 public class SysUserGroupForm implements java.io.Serializable { private String id; private String name; private String principal; private String incumbent; private String remark; } * action要实现模型驱动接口 public class SysUserGroupAction extends ActionSupport implements ModelDriven{ private SysUserGroupForm sysUserGroupForm=new SysUserGroupForm(); public String save() throws IllegalAccessException, InvocationTargetException{ System.out.println("xxxxxxxxxxxxxxxxxxxxxxxx "); return null; } public SysUserGroupForm getModel() { return sysUserGroupForm; } } * 测试,SysUserGroupForm是否接收到值 * 如何在struts2的SysUserGroupAction中的save方法中,获取spring容器中bean节点的对象 * 创建ServiceProvinderCore,加载beans.xml文件,放置在cn.itcast.crm.container包下 public class ServiceProvinderCore { protected ApplicationContext ctx; /** * @param filename beans.xml */ public void load(String filename){ ctx=new ClassPathXmlApplicationContext(filename); } } * 创建ServiceProvinder类,获取获取spring容器中bean节点的对象,放置在cn.itcast.crm.container包下 public class ServiceProvinder { private static ServiceProvinderCore sc; static{ System.err.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); sc=new ServiceProvinderCore(); sc.load("beans.xml"); } public static Object getService(String beanName){ System.err.println("ppppppppppppppppppppppp"); if(StringUtils.isBlank(beanName)){ throw new RuntimeException("您要访问的服务名称不能为空"); } Object bean=null; //如果spring容器中包含beanName if(sc.ctx.containsBean(beanName)){ bean=sc.ctx.getBean(beanName); } //如果spring容器中不包含beanName if(bean==null){ throw new RuntimeException("您要访问的服务名称["+beanName+"]不存在"); } return bean; } } * 测试 @Test public void testSave() { ISysUserGroupService sysUserGroupService=(ISysUserGroupService)ServiceProvinder.getService(ISysUserGroupService.SERVICE_NAME); SysUserGroup sysUserGroup = new SysUserGroup(); sysUserGroup.setName("销售部"); sysUserGroup.setPrincipal("xxx"); sysUserGroup.setIncumbent("ttt"); sysUserGroupService.saveSysUserGroup(sysUserGroup); } * 在 SysUserGroupAction中的save方法中增加如下代码 public String save() throws IllegalAccessException, InvocationTargetException{ System.out.println("sysUserGroupForm.getName() "+sysUserGroupForm.getName()); //实例化po对象 SysUserGroup sysUserGroup=new SysUserGroup(); //赋值vo对象的值到po中 BeanUtils.copyProperties(sysUserGroup, sysUserGroupForm); //获取业务层的对象(本项目struts2和spring是分离的) ISysUserGroupService sysUserGroupService= (ISysUserGroupService)ServiceProvinder.getService(ISysUserGroupService.SERVICE_NAME); //调用业务层保存po对象 sysUserGroupService.saveSysUserGroup(sysUserGroup); return null; } * 测试:


网友评论