




已阅读5页,还剩34页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Mybatis、SpringMVC练习CRM系统1 CRM项目外观 北京市昌平区建材城西路金燕龙办公楼一层 电话:400-618-90901. 开发环境IDE:Eclipse Mars2 Jdk: 1.7数据库:MySQL 2. 创建数据库数据库sql文件位置如下图:创建crm数据库,执行sql效果如下图:3. 工程搭建使用的Bootstrap前端框架,官方网站/工程使用Springmvc、spring、mybatis框架整合完成。3.1. 需要的jar包1. spring(包括springmvc)2. mybatis3. mybatis-spring整合包4. 数据库驱动5. 第三方连接池。6. Json依赖包Jacksonjar包位置如下图:3.2. 整合思路Dao层:1、 SqlMapConfig.xml,空文件即可,但是需要文件头。2、 applicationContext-dao.xmla) 数据库连接Druidb) SqlSessionFactory对象,需要spring和mybatis整合包下的。c) 配置mapper文件扫描器。Mapper动态代理开发 增强版Service层:1、 applicationContext-service.xml包扫描器,扫描service注解的类。2、 applicationContext-trans.xml配置事务。Controller层:1、 Springmvc.xmla) 包扫描器,扫描Controller注解的类。b) 配置注解驱动c) 配置视图解析器Web.xml文件:1、 配置spring监听器2、 配置前端控制器。3.3. 创建工程创建动态web工程,步骤如下图:创建boot-crm,如下图3.4. 加入jar包加入课前资料中的jar包3.5. 加入配置文件创建config资源文件夹,在里面创建mybatis和spring文件夹3.5.1. SqlMapConfig.xml空文件即可3.5.2. applicationContext-dao.xml需要配置:加载properties文件,数据源,SqlSessionFactory,Mapper扫描3.5.3. perties配置数据库信息jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:/localhost:3306/crm?characterEncoding=utf-8jdbc.username=rootjdbc.password=root3.5.4. perties配置日志信息# Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output.log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p %t - %m%n3.5.5. applicationContext-service.xml配置service扫描3.5.6. applicationContext-trans.xml配置事务管理:事务管理器、通知、切面3.5.7. Springmvc.xml配置SpringMVC表现层:Controller扫描、注解驱动、视图解析器3.5.8. Web.xml配置Spring、SpringMVC、解决post乱码问题boot-crmindex.jspcontextConfigLocationclasspath:spring/applicationContext-*.xmlorg.springframework.web.context.ContextLoaderListenerencodingorg.springframework.web.filter.CharacterEncodingFilterencoding/*boot-crmorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring/springmvc.xml1boot-crm/3.6. 加入静态资源最终效果如下图:4. 实现页面展示4.1. 代码实现编写CustomerController 显示用户列表ControllerRequestMapping(customer)public class CustomerController /* * 显示用户列表 * * return */RequestMapping(list)public String queryCustomerList() return customer;4.2. 页面显示问题访问页面,发现不能正常显示打开开发者工具,选择Network,发现css、js等资源文件无法加载原因:web.xml配置时,是设置所有的请求都进入SpringMVC。但是SpringMVC 无法处理css、js等静态资源,所以无法正常显示解决方案:1. 在springmvc.xml中配置2. 修改web.xml,让所有以action结尾的请求都进入SpringMVCboot-crm*.action解决后的效果如下图,可以正常显示页面样式:我们使用第二种方式解决,因为此项目中的页面的请求都是以action结尾的,所以使用第二种方式,在web.xml里面进行相应的配置boot-crm*.action5. 实现查询条件初始化5.1. 需求分析页面效果如上图,在查询客户的时候,可以选择客户来源,所属行业,客户级别信息,页面加载时需要初始化查询条件下拉列表。前端jsp逻辑客户名称 客户来源 -请选择-option value=$item.dict_id selected$item.dict_item_name 所属行业 -请选择-option value=$item.dict_id selected$item.dict_item_name 客户级别-请选择-option value=$item.dict_id selected$item.dict_item_name 查询按照jsp的要求,把对应的数据查询出来,放到模型中。数据存放在base_dict表,可以使用dict_type_code类别代码进行查询使用需要获取的数据如下图:使用的sql:SELECT * FROM base_dict WHERE dict_type_code = 0015.2. 实现DAO开发5.2.1. pojo因为页面显示的名字是下划线方式,和数据库表列名一样,根据页面的样式,编写pojopublic class BaseDict private String dict_id;private String dict_type_code;private String dict_type_name;private String dict_item_name;private String dict_item_code;private Integer dict_sort;private String dict_enable;private String dict_memo;get/set。5.2.2. Mapper编写BaseDictMapperpublic interface BaseDictMapper /* * 根据类别代码查询数据 * * param dictTypeCode * return */List queryBaseDictByDictTypeCode(String dictTypeCode);5.2.3. Mapper.xml编写BaseDictMapper.xmlSELECT * FROM base_dict WHERE dict_type_code =#dict_type_code5.3. 实现Service开发5.3.1. BaseDictService 接口public interface BaseDictService /* * 根据类别代码查询 * * param dictTypeCode * return */List queryBaseDictByDictTypeCode(String dictTypeCode);5.3.2. BaseDictServiceImpl 实现类Servicepublic class BaseDictServiceImpl implements BaseDictService Autowiredprivate BaseDictMapper baseDictMapper;Overridepublic List queryBaseDictByDictTypeCode(String dictTypeCode) List list = this.baseDictMapper.queryBaseDictByDictTypeCode(dictTypeCode);return list;5.4. 实现Controller开发5.4.1. 修改之前编写的controllerControllerRequestMapping(customer)public class CustomerController Autowiredprivate BaseDictService baseDictService;/* * 显示客户列表 * * return */RequestMapping(list)public String queryCustomerList(Model model) / 客户来源List fromType = this.baseDictService.queryBaseDictByDictTypeCode(002);/ 所属行业List industryType = this.baseDictService.queryBaseDictByDictTypeCode(001);/ 客户级别List levelType = this.baseDictService.queryBaseDictByDictTypeCode(006);/ 把前端页面需要显示的数据放到模型中model.addAttribute(fromType, fromType);model.addAttribute(industryType, industryType);model.addAttribute(levelType, levelType);return customer;5.4.2. 效果实现效果如下图:5.4.3. 硬编码问题这里是根据dict_type_code类别代码查询数据,这里的查询条件是写死的,有硬编码问题。可以把类别代码提取到配置文件中,再使用value注解进行加载。. 添加perties添加perties配置文件#客户来源CUSTOMER_FROM_TYPE=002#客户行业CUSTOMER_INDUSTRY_TYPE=001#客户级别CUSTOMER_LEVEL_TYPE=00. 修改springmvc.xml配置文件在springmvc.xml中加载perties注意:Controller需要的配置文件信息必须添加到springmvc的配置文件中. 修改Controller方法ControllerRequestMapping(customer)public class CustomerController / 客户来源Value($CUSTOMER_FROM_TYPE)private String CUSTOMER_FROM_TYPE;/ 客户行业Value($CUSTOMER_INDUSTRY_TYPE)private String CUSTOMER_INDUSTRY_TYPE;/ 客户级别Value($CUSTOMER_LEVEL_TYPE)private String CUSTOMER_LEVEL_TYPE;Autowiredprivate BaseDictService baseDictService;/* * 显示客户列表 * * return */RequestMapping(list)public String queryCustomerList(Model model) / 客户来源List fromType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_FROM_TYPE);/ 所属行业List industryType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_INDUSTRY_TYPE);/ 客户级别List levelType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_LEVEL_TYPE);/ 把前端页面需要显示的数据放到模型中model.addAttribute(fromType, fromType);model.addAttribute(industryType, industryType);model.addAttribute(levelType, levelType);return customer;6. 客户列表展示6.1. 需求展示客户列表,并且可以根据查询条件过滤查询结果,并且实现分页。效果如下图:页面代码:客户信息列表ID客户名称客户来源客户所属行业客户级别固定电话手机操作$row.cust_id$row.cust_name$row.cust_source$row.cust_industry$row.cust_level$row.cust_phone$row.cust_mobile修改删除分析我们需要根据四个条件进行查询,返回数据是分页对象PageSql语句:SELECTa.cust_id,a.cust_name,a.cust_user_id,a.cust_create_id,b.dict_item_name cust_source,c.dict_item_name cust_industry,d.dict_item_name cust_level,a.cust_linkman,a.cust_phone,a.cust_mobile,a.cust_zipcode,a.cust_address,a.cust_createtimeFROMcustomer aLEFT JOIN base_dict b ON a.cust_source = b.dict_idLEFT JOIN base_dict c ON a.cust_industry = c.dict_idLEFT JOIN base_dict d ON a.cust_level = d.dict_idWHEREa.cust_name LIKE %马%AND a.cust_source = 6AND a.cust_industry = 2AND a.cust_level = 22LIMIT 0, 106.2. 创建pojo开发public class Customer private Long cust_id;private String cust_name;private Long cust_user_id;private Long cust_create_id;private String cust_source;private String cust_industry;private String cust_level;private String cust_linkman;private String cust_phone;private String cust_mobile;private String cust_zipcode;private String cust_address;private Date cust_createtime;get/set。6.3. 实现DAO分析:1. 前台发起请求,需要接收请求过来的查询条件数据,可以使用pojo接
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025版餐饮配送行业食品安全溯源服务合同
- 高三试卷:四川省雅安市2024-2025学年高三上学期11月零诊试题数学含答案
- 二零二五年度电工设备调试与验收合同
- 2025版工业地产合作开发合同模板
- 2025版观叶盆栽种苗园艺市场直销连锁合作合同
- 二零二五年度房地产投资居间合同范本
- 2025版工程玻璃节能减排项目合作合同范本
- 2025版仓储房屋租赁及仓储配套设施租赁与维护服务合同
- 2025版校园食堂承包经营合同示范文本
- 2025版餐饮业二人合伙开店管理服务合同
- 2025年河北单招七类考试题库
- 2025年健身教练专业知识测评考核试卷及答案
- 2025年黑龙江省事业单位招聘考试教师化学学科专业试卷
- 2022版《义务教育数学课程标准》测试卷(完整版含答案)
- 2025行政执法人员考试题库含答案
- 联通校招测评题库及答案
- 【好题汇编】2023-2025年中考物理真题分类汇编 专题:内能及内能和利用(有解析)
- 科创板块测试题及答案
- 履带吊安装拆除作业安全管理与实施方案
- 儿科护理进修
- 人员资质认定管理办法
评论
0/150
提交评论