BOOT客户管理系统(详解)
Github:https://github.com/wz20
Gitee:https://gitee.com/Wang-ZeLXM/boot-CRM
本文中的代码不可以直接粘贴,因为有一些没有写在注释中的文字分析!请前往github,或网站资源
演示图:
本系统SSM整合思路:
springMVC系统流程图:
一. 环境搭建
1.pom.xml添加依赖
- <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- spring--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.12.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.2.12.RELEASE</version></dependency><!--springmvc所用依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.12.RELEASE</version></dependency><!--Servlet - JSP--><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- mybatis所用依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!-- 数据库所用依赖--><!--数据库驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.24</version></dependency><!--数据库连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.1</version></dependency><!--jdbc--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.12.RELEASE</version></dependency><!--日志 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.14</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.25</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.logback-extensions</groupId><artifactId>logback-ext-spring</artifactId><version>0.1.4</version></dependency><!-- 工具包 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.11</version></dependency><!--jackson--><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.2</version></dependency><!-- 文件上传下载 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.2</version></dependency><!--事务--><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.2.12.RELEASE</version></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></dependency>
复制代码 2. 创建目录构造
3. 配置文件的编写
spring配置文件—applicationContext的编写
- <?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--扫描包除了带有controller注解的--><context:component-scanbase-package="com.wangze"use-default-filters="true"><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 整合mybatis--><context:property-placeholderlocation="classpath:db.properties"/><!--连接池--><beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="driverClassName"value="${jdbc.driver}"/><propertyname="url"value="${jdbc.url}"/><propertyname="username"value="${jdbc.username}"/><propertyname="password"value="${jdbc.password}"/><!--最大连接数10,最小空闲数5--><propertyname="maxActive"value="10"/><propertyname="minIdle"value="5"/></bean><!--配置sqlSessionFactoryBean--><beanid="sqlSessionFactoryBean"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="typeAliasesPackage"value="com.wangze.core.entity"/>有了这个别名,在mapper中就可以不用写全类名
- <propertyname="mapperLocations"><list><value>classpath:com.wangze.mapper/*Mapper.xml</value>扫描mapper下所有以Mapper结尾的文件
- </list></property></bean><!--创建DAO对象 MapperScannerConfigure--><beanid="scanner"class="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactoryBean"/><propertyname="basePackage"value="com.wangze.core.dao"> 指定mapper对应的dao接口包
- </property></bean><!-- 整合mybatis完毕--><!--注解开发 事务 事务的开发就是把AOP开发中的额外方法换成事务--><beanid="dataSourceTransactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><tx:annotation-driventransaction-manager="dataSourceTransactionManager"/><!--只需要在原始对象上加注解:@Tronsaction 增删改操作 @Transactional 查询操作 @Transactional(propagation=Propagation.SUPPORTS,readOnly=true)--></beans>
复制代码 mvc配置文件
- <?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--配置包扫描--><context:component-scanbase-package="com.wangze"use-default-filters="false"><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan><!--加载属性文件--><context:property-placeholderlocation="classpath:resource.properties"/><!--处置器映射器 适配器--><mvc:annotation-driven/><!--配置静态资源访问映射,此配置中的文件将不会呗前端控制器拦截 --><mvc:resourceslocation="/js/"mapping="/js/**"/><mvc:resourceslocation="/css/"mapping="/css/**"/><mvc:resourceslocation="/fonts/"mapping="/fonts/**"/><mvc:resourceslocation="/images/"mapping="/images/**"/><!-- 配置视图解释器ViewResolver --><beanid="jspViewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="prefix"value="/WEB-INF/jsp/"/><propertyname="suffix"value=".jsp"/></bean></beans>
复制代码 db.properties(数据库连接池)
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/boot_crm
- jdbc.username=root
- jdbc.password=123456
- jdbc.initialSize=5
复制代码 resource.properties(用于查询数据字典中的数据)
- customer.from.type=002
- customer.industry.type=001
- customer.level.type=006
复制代码 log4j.properties(log4j配置文件)
- # resources文件夹根目录下
- log4j.rootLogger=debug,console
- log4j.appender.console=org.apache.log4j.ConsoleAppender
- log4j.appender.console.Target=System.out
- log4j.appender.console.layout=org.apache.log4j.PatternLayout
- log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
复制代码 logback.xml(logback配置文件)
当取消spring文件用spring3.0+提供的@Bean所取代时,log4j不能用了,我们使用logback日志- <?xml version="1.0" encoding="UTF-8"?><configuration><!-- 控制台输出 --><appendername="STDOUT"class="ch.qos.logback.core.ConsoleAppender"><encoder><!--格式化输出:%d表示⽇期,%thread表示线程名,
- %-5level:级别从左显示5个字符宽度%msg:⽇志消息,%n是换⾏符--><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}
- [%thread] %-5level %logger{50} - %msg%n</pattern></encoder></appender><rootlevel="DEBUG"><appender-refref="STDOUT"/></root></configuration>
复制代码 web.xml
- <?xml version="1.0" encoding="UTF-8"?><web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- spring--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!--监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--springmvc--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> //中央控制器
- <init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc-config.xml</param-value></init-param><load-on-startup>1</load-on-startup> //启动时机
- </servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern> //默认所有恳求都被springmvc处置,需要配置静态资源不被拦截;
- </servlet-mapping><!-- 过滤器 防止恳求和相应的数据乱码 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--默认首页--><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
复制代码 4.导入页面资源
- 请访问我的github获取:https://github.com/wz20
复制代码 5.导入数据库
二、实现登录功能(User相关)
- 用户发出登录恳求后,验证登录账号和密码,假设正确,跳转视图层;假设错误,给出提示;
复制代码 1.编写user实体类
在entity包下,由于我使用了lombok,所以@Data 自动生成set,get方法- package com.wangze.core.entity;import lombok.Data;import org.springframework.stereotype.Component;import java.io.Serial;import java.io.Serializable;/**
- * @author: 王泽20
- * 一个对象序列化的接口,一个类只要实现了Serializable接口,它的对象才干被序列化。
- * 从说明中我们可以看到,假设我们没有自己声明一个serialVersionUID变量,接口会默认生成一个serialVersionUID
- * 但是强烈建议用户自定义一个serialVersionUID,因为默认的serialVersionUID对于class的细节非常敏感,反序列化时可能会导致InvalidClassException这个异常。
- * 在前面我们已经新建了一个实体类User实现Serializable接口,并且定义了serialVersionUID变量。
- */@Component@DatapublicclassUserimplementsSerializable{@Serialprivatestaticfinallong serialVersionUID =1L;private Integer user_Id;//用户idprivate String user_code;//用户账号private String user_name;//用户名称private String user_password;//用户密码private Integer user_state;//用户状态}
复制代码 2.编写UserDao接口
- package com.wangze.core.dao;import com.wangze.core.entity.User;import org.apache.ibatis.annotations.Param;import org.springframework.stereotype.Repository;/**
- * @author: 王泽20
- * 通过账号密码查询用户
- */@Repository//这个注解相当于<bean>标签,是@Component的衍生注解,用于dao层publicinterfaceUserDao{public User findUser(@Param("usercode") String usercode,@Param("password") String password);//当你使用了使用@Param注解来声明参数时,假设使用 #{} 或 ${} 的方式都可以。//在旧版本中假设想在mapper.xml 中引用变量使用${} 必需用@param为变量起别名;新版只要是只要一个参数都可以不用起别名//# 和 $ 到底有什么区别??? #采用占位符的方式 $采用参数拼接的方式 参数拼接有SQL注入的风险}
复制代码 对应mapper.xml
- <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.wangze.core.dao.UserDao"><!--查询用户--><selectid="findUser"parameterType="String"resultType="com.wangze.core.entity.User">
- select * from sys_user
- where user_code = #{usercode}
- and user_password = #{password}
- and user_state = '1'
- </select></mapper>
复制代码 3.编写UserService接口
- package com.wangze.core.service;import com.wangze.core.entity.User;publicinterfaceUserService{public User findUser(String usercode,String password);}
复制代码 4.编写UserServiceImpl
- package com.wangze.core.service;import com.wangze.core.dao.UserDao;import com.wangze.core.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;/**
- * @author: 王泽20
- */@Service//这个注解相当于<bean>标签,是@Component的衍生注解,用于service层@TransactionalpublicclassUserServiceImplimplementsUserService{@Autowired//注入成员变量userDaoprivate UserDao userDao;@Override//在数据库中找这个userpublic User findUser(String usercode, String password){
- User user =this.userDao.findUser(usercode,password);return user;}}
复制代码 5.编写UserController类
- package com.wangze.core.controller;import com.wangze.core.entity.User;import com.wangze.core.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import javax.servlet.http.HttpSession;/**
- * @author: 王泽20
- */@Controller//这个注解相当于<bean>标签,是@Component的衍生注解,用于Controller层publicclassUserController{@Autowired//依赖注入private UserService userService;@RequestMapping(value ="/login",method = RequestMethod.POST)//必需是post方法 public String login(String usercode, String password, Model model, HttpSession session){
- User user = userService.findUser(usercode,password);if(user != null){
- session.setAttribute("USER_SESSION",user);return"customer";}
- model.addAttribute("msg","账号或密码错误,请重新输入");return"login";}}
复制代码 6.测试
登录验证(拦截器)
1.创建LoginInterceptor类
- package com.wangze.core.interceptor;import com.wangze.core.entity.User;import org.springframework.stereotype.Component;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;/**
- * @author: 王泽20 springmvc中的拦截器,只要返回值为true,后面的方法才会继续执行
- */@ComponentpublicclassLoginInterceptorimplementsHandlerInterceptor{@OverridepublicbooleanpreHandle(HttpServletRequest request, HttpServletResponse response,Object handler)throws ServletException, IOException {//获取恳求的url
- String url =request.getRequestURI();//拦截不是登录的恳求if(url.contains("/login")){returntrue;}//获取session
- HttpSession session =request.getSession();
- User user=(User) session.getAttribute("USER_SESSION");//判断session中是否有用户数据,假设有,则返回true,继续向下执行if(user != null){returntrue;}//不符合条件的给出提示信息,并转发到登录界面
- request.setAttribute("msg","您还没有登录,请先登录");
- request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);//抛出异常returnfalse;}@OverridepublicvoidpostHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception {}@OverridepublicvoidafterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {}}
复制代码 2.在springmvc配置文件中配置拦截器
- <!-- 拦截器--><mvc:interceptors><mvc:interceptor><mvc:mappingpath="/**"/><refbean="loginInterceptor"/></mvc:interceptor></mvc:interceptors>
复制代码 3.验证
- @RequestMapping("/tocon")public String tocon(){return"customer";}
- 阅读器输入...../tocon
复制代码 退出登录
在userController中- /* 退出登录 */@RequestMapping("logout")public String logout(HttpSession session){
- session.invalidate();//肃清sessionreturn"redirect:login";}//向用户登录界面跳转@RequestMapping(value ="login",method = RequestMethod.GET)public String toLogin(){return"login";}
复制代码 三、客户管理模块
- 对客户表,停止增删改查 (重点:分页查询 详细两个工具类此处不做介绍,在github中可以获取源码)
复制代码 一、查询操作
1.实体类
- Customer
- package com.wangze.core.entity;import lombok.Data;import org.springframework.stereotype.Component;import java.io.Serial;import java.io.Serializable;import java.util.Date;/**
- * @author: 王泽20
- */@Data@ComponentpublicclassCustomerimplementsSerializable{@Serialprivatestaticfinallong serialVersionUID =1L;private Integer cust_id;//客户编号private String cust_name;//客户名称private Integer cust_user_id;//负责人idprivate Integer cust_create_id;//创建人idprivate 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;//创建时间private Integer start;//起始行private Integer rows;//所取行数}
复制代码 - 数据字典 BaseDict
- package com.wangze.core.entity;import lombok.Data;import org.springframework.stereotype.Component;import java.io.Serial;import java.io.Serializable;/**
- * @author: 王泽20
- */@Data@ComponentpublicclassBaseDictimplementsSerializable{@Serialprivatestaticfinallong serialVersionUID =1L;private String dict_id;//数据字典idprivate 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;//备注}
复制代码 2.Dao
CustomerDao- package com.wangze.core.dao;import com.wangze.core.entity.Customer;import org.springframework.stereotype.Repository;import java.util.List;@RepositorypublicinterfaceCustomerDao{//客户列表public List<Customer>selectCustomerList(Customer customer);//客户数public Integer selectCustomerListCount(Customer customer);}
复制代码 BaseDictDao- package com.wangze.core.dao;import com.wangze.core.entity.BaseDict;import java.util.List;publicinterfaceBaseDictDao{public List<BaseDict>selectBaseDictByTypeCode(String typecode);}
复制代码 3.Mapper
- CustomerMapper
- <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.wangze.core.dao.CustomerDao"><sqlid="selectCustomerListWhere"><where><iftest="cust_name != null">
- cust_name like "%"#{cust_name}"%"
- </if><iftest="cust_source != null">
- and cust_source = #{cust_source}
- </if><iftest="cust_industry != null">
- and cust_industry = #{cust_industry}
- </if><iftest="cust_level != null">
- and cust_level = #{cust_level}
- </if></where></sql><!-- 查询客户列表--><selectid="selectCustomerList"parameterType="com.wangze.core.entity.Customer"resultType="com.wangze.core.entity.Customer">
- SELECT
- cust_id,
- cust_name,
- cust_user_id,
- cust_create_id,
- b.dict_item_name,cust_source,
- c.dict_item_name,cust_industry,
- d.dict_item_name,cust_level,
- cust_linkman,
- cust_phone,
- cust_mobile,
- cust_createtime
- FROM
- customer a
- LEFT JOIN (
- SELECT
- dict_id,
- dict_item_name
- FROM
- base_dict
- WHERE
- dict_type_code = '002'
- ) b ON a.cust_source = b.dict_id
- LEFT JOIN (
- SELECT
- dict_id,
- dict_item_name
- FROM
- base_dict
- WHERE
- dict_type_code = '001'
- ) c ON a.cust_industry = c.dict_id
- LEFT JOIN (
- SELECT
- dict_id,
- dict_item_name
- FROM
- base_dict
- WHERE
- dict_type_code = '006'
- ) d ON a.cust_level = d.dict_id
- <includerefid="selectCustomerListWhere"/><!-- 执行分页查询 --><iftest="start !=null and rows != null">
- limit #{start},#{rows}
- </if></select><!-- 查询客户总数 --><selectid="selectCustomerListCount"parameterType="com.wangze.core.entity.Customer"resultType="Integer">
- select count(*) from customer
- <includerefid="selectCustomerListWhere"/></select></mapper>
复制代码 - BaseDictMapper.xml
- <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.wangze.core.dao.BaseDictDao"><selectid="selectBaseDictByTypeCode"parameterType="String"resultType="com.wangze.core.entity.BaseDict">
- select * from base_dict where dict_type_code=#{typecode}
- </select></mapper>
复制代码 4.service
CustomerService- package com.wangze.core.service;import com.wangze.common.utils.Page;import com.wangze.core.entity.Customer;import org.springframework.stereotype.Service;@ServicepublicinterfaceCustomerService{// 查询客户列表public Page<Customer>findCustomerList(Integer page,Integer rows,String custName,String custSource,String custIndustry,String custLevel);}
复制代码 BaseDictService- package com.wangze.core.service;import com.wangze.core.entity.BaseDict;import org.springframework.stereotype.Service;import java.util.List;@ServicepublicinterfaceBaseDictService{//根据类别代码查询数据字典public List<BaseDict>findBaseDictByTypeCode(String typecode);}
复制代码 5.serviceimpl
- Custormer
- package com.wangze.core.service;import com.wangze.common.utils.Page;import com.wangze.core.dao.CustomerDao;import com.wangze.core.entity.Customer;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.util.List;/**
- * @author: 王泽20
- * 客户管理
- */@Service("customerService")@TransactionalpublicclassCustomerServiceImplimplementsCustomerService{@Autowiredprivate CustomerDao customerDao;@Overridepublic Page<Customer>findCustomerList(Integer page, Integer rows, String custName, String custSource, String custIndustry, String custLevel){//创建客户对象
- Customer customer =newCustomer();// 判断客户名称if(StringUtils.isNotBlank(custName)){
- customer.setCust_name(custName);}// 判断客户信息来源if(StringUtils.isNotBlank(custSource)){
- customer.setCust_source(custSource);}// 判断客户所属行业if(StringUtils.isNotBlank(custIndustry)){
- customer.setCust_industry(custIndustry);}// 判断客户级别if(StringUtils.isNotBlank(custLevel)){
- customer.setCust_level(custLevel);}// 当前页
- customer.setStart((page-1)* rows);// 每页数
- customer.setRows(rows);// 查询客户列表
- List<Customer> customers =
- customerDao.selectCustomerList(customer);// 查询客户列表总记录数
- Integer count = customerDao.selectCustomerListCount(customer);// 创建Page返回对象
- Page<Customer> result =newPage<>();
- result.setPage(page);
- result.setRows(customers);
- result.setSize(rows);
- result.setTotal(count);return result;}}
复制代码 - BaseDict
- package com.wangze.core.service;import com.wangze.core.dao.BaseDictDao;import com.wangze.core.entity.BaseDict;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;/**
- * @author: 王泽20
- */@Service("baseDictService")publicclassBaseDictServiceImplimplementsBaseDictService{@Autowiredprivate BaseDictDao baseDictDao;@Overridepublic List<BaseDict>findBaseDictByTypeCode(String typecpde){return baseDictDao.selectBaseDictByTypeCode(typecpde);}}
复制代码 6.Controller
- package com.wangze.core.controller;import com.wangze.common.utils.Page;import com.wangze.core.entity.BaseDict;import com.wangze.core.entity.Customer;import com.wangze.core.service.BaseDictService;import com.wangze.core.service.CustomerService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import java.util.List;/**
- * @author: 王泽20
- */@ControllerpublicclassCustomerController{@Autowiredprivate CustomerService customerService;@Autowiredprivate BaseDictService baseDictService;@Value("${customer.from.type}")private String FROM_TYPE;// 客户所属行业@Value("${customer.industry.type}")private String INDUSTRY_TYPE;// 客户级别@Value("${customer.level.type}")private String LEVEL_TYPE;/**
- * 客户列表
- */@RequestMapping(value ="/customer/list")public String list(@RequestParam(defaultValue="1")Integer page,@RequestParam(defaultValue="10")Integer rows,
- String custName, String custSource, String custIndustry,
- String custLevel, Model model){// 条件查询所有客户
- Page<Customer> customers = customerService.findCustomerList(page, rows, custName,
- custSource, custIndustry, custLevel);
- model.addAttribute("page", customers);// 客户来源
- List<BaseDict> fromType = baseDictService.findBaseDictByTypeCode(FROM_TYPE);// 客户所属行业
- List<BaseDict> industryType = baseDictService.findBaseDictByTypeCode(INDUSTRY_TYPE);// 客户级别
- List<BaseDict> levelType = baseDictService.findBaseDictByTypeCode(LEVEL_TYPE);// 添加参数
- model.addAttribute("fromType", fromType);
- model.addAttribute("industryType", industryType);
- model.addAttribute("levelType", levelType);
- model.addAttribute("custName", custName);
- model.addAttribute("custSource", custSource);
- model.addAttribute("custIndustry", custIndustry);
- model.addAttribute("custLevel", custLevel);return"customer";}}
复制代码 二、添加客户(增)
分析:增添用户实则在客户表中增加数据,前面已经创建了客户的实体类,所以只用编写controller层和service以及Dao中的方法。
此过程是添加,所以需要注意事务的提交。前面有配置…可以查看spring配置文件
1.Dao层
- // 创建客户publicintcreateCustomer(Customer customer);
复制代码 2.mapper
- <!-- 添加客户 --><insertid="createCustomer"parameterType="com.wangze.core.entity.Customer">
- insert into customer(
- cust_name,
- cust_user_id,
- cust_create_id,
- cust_source,
- cust_industry,
- cust_level,
- cust_linkman,
- cust_phone,
- cust_mobile,
- cust_zipcode,
- cust_address,
- cust_createtime
- )
- values(#{cust_name},
- #{cust_user_id},
- #{cust_create_id},
- #{cust_source},
- #{cust_industry},
- #{cust_level},
- #{cust_linkman},
- #{cust_phone},
- #{cust_mobile},
- #{cust_zipcode},
- #{cust_address},
- #{cust_createtime}
- )
- </insert>
复制代码 3.Service
- //新建客户intcreateCustomer(Customer customer);
复制代码 4.ServiceImpl
- /**
- * 创建客户
- */@OverridepublicintcreateCustomer(Customer customer){return customerDao.createCustomer(customer);}
复制代码 5.Controller
- /**
- * 创建客户
- */@RequestMapping("/customer/create")@ResponseBodypublic String customerCreate(Customer customer, HttpSession session){// 获取Session中的当前用户信息
- User user =(User) session.getAttribute("USER_SESSION");// 将当前用户id存储在客户对象中
- customer.setCust_create_id(user.getUser_Id());// 创建Date对象
- Date date =newDate();// 得到一个Timestamp格式的时间,存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
- Timestamp timeStamp =newTimestamp(date.getTime());
- customer.setCust_createtime(timeStamp);// 执行Service层中的创建方法,返回的是受影响的行数int rows = customerService.createCustomer(customer);if(rows >0){return"OK";}else{return"FAIL";}}
复制代码 三、修改、删除 客户
由于增删改大同小异,所以这两个合起来写了。
1.Dao层
- // 通过id查询客户public Customer getCustomerById(Integer id);// 更新客户信息publicintupdateCustomer(Customer customer);// 删除客户int deleteCustomer (Integer id);
复制代码 2.mapper
- <!-- 根据id获取客户信息 --><selectid="getCustomerById"parameterType="Integer"resultType="com.wangze.core.entity.Customer">
- select * from customer where cust_id = #{id}
- </select><!-- 更新客户 --><updateid="updateCustomer"parameterType="com.wangze.core.entity.Customer">
- update customer
- <set><iftest="cust_name!=null">
- cust_name=#{cust_name},
- </if><iftest="cust_user_id!=null">
- cust_user_id=#{cust_user_id},
- </if><iftest="cust_create_id!=null">
- cust_create_id=#{cust_create_id},
- </if><iftest="cust_source!=null">
- cust_source=#{cust_source},
- </if><iftest="cust_industry!=null">
- cust_industry=#{cust_industry},
- </if><iftest="cust_level!=null">
- cust_level=#{cust_level},
- </if><iftest="cust_linkman!=null">
- cust_linkman=#{cust_linkman},
- </if><iftest="cust_phone!=null">
- cust_phone=#{cust_phone},
- </if><iftest="cust_mobile!=null">
- cust_mobile=#{cust_mobile},
- </if><iftest="cust_zipcode!=null">
- cust_zipcode=#{cust_zipcode},
- </if><iftest="cust_address!=null">
- cust_address=#{cust_address},
- </if><iftest="cust_createtime!=null">
- cust_createtime=#{cust_createtime},
- </if></set>
- where cust_id=#{cust_id}
- </update><!-- 删除客户 --><deleteid="deleteCustomer"parameterType="Integer">
- delete from customer where cust_id=#{id}
- </delete>
复制代码 3.service
- // 通过id查询客户public Customer getCustomerById(Integer id);// 更新客户publicintupdateCustomer(Customer customer);// 删除客户publicintdeleteCustomer(Integer id);
复制代码 4.serviceImpl
- /**
- * 创建客户
- */@OverridepublicintcreateCustomer(Customer customer){return customerDao.createCustomer(customer);}/**
- * 通过id查询客户
- */@Overridepublic Customer getCustomerById(Integer id){
- Customer customer = customerDao.getCustomerById(id);return customer;}/**
- * 更新客户
- */@OverridepublicintupdateCustomer(Customer customer){return customerDao.updateCustomer(customer);}/**
- * 删除客户
- */@OverridepublicintdeleteCustomer(Integer id){return customerDao.deleteCustomer(id);}
复制代码 5.controller
- /**
- * 通过id获取客户信息
- */@RequestMapping("/customer/getCustomerById")@ResponseBodypublic Customer getCustomerById(Integer id){
- Customer customer = customerService.getCustomerById(id);return customer;}/**
- * 更新客户
- */@RequestMapping("/customer/update")@ResponseBodypublic String customerUpdate(Customer customer){int rows = customerService.updateCustomer(customer);if(rows >0){return"OK";}else{return"FAIL";}}/**
- * 删除客户
- */@RequestMapping("/customer/delete")@ResponseBodypublic String customerDelete(Integer id){int rows = customerService.deleteCustomer(id);if(rows >0){return"OK";}else{return"FAIL";}}
- Customer getCustomerById(Integer id){
- Customer customer = customerDao.getCustomerById(id);return customer;}/**
- * 更新客户
- */@OverridepublicintupdateCustomer(Customer customer){return customerDao.updateCustomer(customer);}/**
- * 删除客户
- */@OverridepublicintdeleteCustomer(Integer id){return customerDao.deleteCustomer(id);}
复制代码 5.controller
- /**
- * 通过id获取客户信息
- */@RequestMapping("/customer/getCustomerById")@ResponseBodypublic Customer getCustomerById(Integer id){
- Customer customer = customerService.getCustomerById(id);return customer;}/**
- * 更新客户
- */@RequestMapping("/customer/update")@ResponseBodypublic String customerUpdate(Customer customer){int rows = customerService.updateCustomer(customer);if(rows >0){return"OK";}else{return"FAIL";}}/**
- * 删除客户
- */@RequestMapping("/customer/delete")@ResponseBodypublic String customerDelete(Integer id){int rows = customerService.deleteCustomer(id);if(rows >0){return"OK";}else{return"FAIL";}}
复制代码 |