简易的CRM系统案例SpringBoot + thymeleaf + MySQL + MyBatis版本

创建maven项目

pop.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.loaderman</groupId>
    <artifactId>crm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>crm</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 数据库连接池 -->
        <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

resources下创建

application.yml

spring:
  thymeleaf:
    mode: HTML5
    encoding: UTF-8
    sourceType: text/html; charset=utf-8
    type: text/html; charset=utf-8
    # 开发禁用缓存
    cache: false
  datasource:
    druid:
      #监控统计拦截的filters
      filters: stat
      driver-class-name: com.mysql.jdbc.Driver
      #基本属性
      url: jdbc:mysql://localhost:3306/infos
      username: root
      password: root
      #配置初始化大小/最小/最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      #获取连接等待超时时间
      max-wait: 60000
      #间隔多久进行一次检测,检测需要关闭的空闲连接
      time-between-eviction-runs-millis: 60000
      #一个连接在池中最小生存的时间
      min-evictable-idle-time-millis: 300000

      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
    hikari:
      connection-timeout: 60000
    mvc:
        view:
          prefix: /WEB-INF/
          suffix: .jsp
        resources:
          add-mappings: false
        jackson:
          serialization:
            write-dates-as-timestamps: true
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.loaderman.crm.entity
  configuration:
    use-column-label: true
    use-generated-keys: true
    map-underscore-to-camel-case: true
# spring-boot默认打印输出info级别以上的,可在此处修改输出级别
logging:
  config: classpath:logback-spring.xml
  path: /var/log
  debug: true

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!-- 控制台输出 -->
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 时间滚动输出 level为 INFO 日志 -->
    <appender name="file-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY </onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>./logs/info.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 时间滚动输出 level为 DEBUG 日志 -->
    <appender name="file-debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY </onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>./logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 时间滚动输出 level为 ERROR 日志 -->
    <appender name="file—error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY </onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>./logs/error.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.apache.ibatis" level="INFO"/>
    <logger name="java.sql.Connection" level="debug" />
    <logger name="java.sql.Statement" level="debug" />
    <logger name="java.sql.PreparedStatement" level="debug" />

    <root level="info">
        <appender-ref ref="stdout" />
        <appender-ref ref="file-info" />
    </root>
</configuration>

创建用户MyBatis 的 Mapper

userMapper.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">

<!-- namespace属性是名称空间,必须唯一 -->
<mapper namespace="com.loaderman.crm.dao.UserDao">

    <update id="modifyUser" parameterType="com.loaderman.crm.entity.User">
        update t_user set id=#{id},name=#{name},telephone=#{telephone},age=#{age},telephone=#{telephone},idCard=#{idCard},address=#{address},weixin=#{weixin},qq=#{qq},email=#{email},job=#{job},area=#{area},grade=#{grade},remark=#{remark} where id=#{id}

    </update>


    <delete id="delUser" parameterType="com.loaderman.crm.entity.User">
        delete from t_user where id = #{id}
    </delete>
    <insert id="addUser" parameterType="com.loaderman.crm.entity.User" >
        insert into t_user (name,sex,age,telephone,idCard,address,weixin,qq,email,job,area,grade,remark) values(#{name},#{sex},#{age},#{telephone},#{idCard},#{address},#{weixin},#{qq},#{email},#{job},#{area},#{grade},#{remark})

    </insert>
    <select  id="getUserByName" parameterType="String" resultType="com.loaderman.crm.entity.User">
        select * from t_user where name=#{name}
    </select >
    <select id="getUserMoreInfo"  parameterType="com.loaderman.crm.entity.User" resultType="com.loaderman.crm.entity.User">
        select * from t_user where id=#{id}
    </select>
    <select id="findUser"  parameterType="com.loaderman.crm.entity.User" resultType="com.loaderman.crm.entity.User">
        select * from t_user where id=#{id}
    </select>

    <select  id="getAllUser"  resultType="com.loaderman.crm.entity.User">
        select * from t_user
    </select >




</mapper>

User实体类

package com.loaderman.crm.entity;

import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class User  {
    private int id;
    private String name;

    private String sex;
    private String age;
    private String telephone;
    private String idCard;
    private String address;
    private String weixin  ;
    private String qq;
    private String email;
    private String job;
    private String grade;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    private String remark;
    private String area;
    public User() {
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getWeixin() {
        return weixin;
    }

    public void setWeixin(String weixin) {
        this.weixin = weixin;
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                ", telephone='" + telephone + '\'' +
                ", idCard='" + idCard + '\'' +
                ", address='" + address + '\'' +
                ", weixin='" + weixin + '\'' +
                ", qq='" + qq + '\'' +
                ", email='" + email + '\'' +
                ", job='" + job + '\'' +
                ", grade='" + grade + '\'' +
                ", remark='" + remark + '\'' +
                ", area='" + area + '\'' +
                '}';
    }
}

dao层

package com.loaderman.crm.dao;


import com.loaderman.crm.entity.User;

import java.util.List;


/*
 *
 * @ class description:对客户表的操作接口
 *
 */
public interface UserDao {
    //获取所有客户信息
    public List<User> getAllUser();
    //获取指定客户信息
    public User getUserMoreInfo(User user);
    public List<User> getUserByName(String name);
    //添加客户
    public int addUser(User user);
    //删除客户
    public int delUser(User user);
    //修改客户
    public int modifyUser(User user);
    //查找指定的客户存在不存在
    public boolean findUser(User user);

}

service层

package com.loaderman.crm.service;


import com.loaderman.crm.entity.User;

import java.util.List;


public interface UserService {
    public List<User> getAllUser();
    public User getUserMoreInfo(User user);
    public List<User> getUserByName(String name);
    public int addUser(User user);
    public int delUser(User user);
    public int modifyUser(User user);
    public boolean findUser(User user);
}
package com.loaderman.crm.service.impl;

import com.loaderman.crm.entity.User;
import com.loaderman.crm.dao.UserDao;
import com.loaderman.crm.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/*
 *
 * @ class description:操作客户
 *
 */
@Service
public class UserServiceimp implements UserService {

    @Resource
    private UserDao userDao;
    @Override
    public List<User> getAllUser() {
        return userDao.getAllUser();
    }

    @Override
    public User getUserMoreInfo(User user) {
        return userDao.getUserMoreInfo(user);
    }

    @Override
    public List<User> getUserByName(String name) {
        return userDao.getUserByName(name);
    }

    @Override
    public int addUser(User user) {
        return userDao.addUser(user);
    }

    @Override
    public int delUser(User user) {
        return userDao.delUser(user);
    }

    @Override
    public int modifyUser(User user) {
        return userDao.modifyUser(user);
    }

    public boolean findUser(User user) {
        return userDao.findUser(user);

    }

}

控制层

package com.loaderman.crm.controller;

import com.loaderman.crm.entity.Account;
import com.loaderman.crm.entity.User;
import com.loaderman.crm.service.impl.UserServiceimp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private UserServiceimp userService;
    @Autowired
    private User user;

    @RequestMapping(value = "/getUserMoreInfo")
    public String getUserMoreInfo(Model model, @RequestParam(required = true) int id) {
        user.setId(id);
        User user1 = userService.getUserMoreInfo(user);
        model.addAttribute("user", user1);
        return "/userDetail";
    }

    @RequestMapping(value = "/adduser")
    public String adduser() {
        return "/addUser";
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    public void addUser(HttpServletResponse response, @ModelAttribute User user, @RequestParam(required = true) String opr, @RequestParam(required = false) Integer id) {
        int n = 0;
        if (opr.equals("addUser")) {
            n = userService.addUser(user);
        } else if (opr.equals("modifyUser")) {
            user.setId(id);
            n = userService.modifyUser(user);
        }
        if (n > 0) {
            try {
                response.sendRedirect("/getUserList");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.sendRedirect("/addUser");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/delUser", method = RequestMethod.GET)
    public void delUser(@RequestParam(required = true) int id, HttpServletResponse response) {
        user.setId(id);
        userService.delUser(user);
        try {
            response.sendRedirect("/getUserList");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/getUserList")
    public String getUserList(HttpServletResponse response, @RequestParam(required = false) String name, ModelMap model, HttpServletRequest request, Model model1) throws IOException {
        Account account = (Account) request.getSession().getAttribute("account");
        System.out.println(account);

        if (name != null && (!name.isEmpty())) {

            List<User> list = userService.getUserByName(name);
            model.addAttribute("list", list);
        } else {
            List<User> list = userService.getAllUser();
            model.addAttribute("list", list);
        }
        return "/getUserList";
    }

    @RequestMapping(value = "/modifyUser", method = RequestMethod.GET)
    public String modifyUser(@RequestParam(required = true) int id, Model model) {
        user.setId(id);
        User user1 = userService.getUserMoreInfo(user);
        model.addAttribute("user", user1);
        return "/modifyUser";
    }
}

web 页面演示,具体查看demo

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Home</title>
    <link href="css/common.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div align="right"  >  欢迎你 <td  th:text="${session.account.username} "></td><a th:href="@{/}">  退出</a> </div>

<h2>客户信息列表</h2>
<div align="right" style="margin-right: 50px">
    <form action="/getUserList" style="text-align: right" >
        <tr>
            <input type="text"  placeholder="请输入要查询姓名" name="name"></td>
        </tr>
        <td  ><input type="submit" value="查询"></td>
        </tr>
    </form>
    <p>
        <a th:href="@{/adduser}" ><input type="button" value="添加客户"></a>
        <a href="/getPolicyList"><input type="button" value="保单列表"></a>
    </p>

</div>

<table>
    <tr>
        <td class="listTable">编号</td>
        <td class="listTable">姓名</td>
        <td class="listTable">性别</td>
        <td class="listTable">年龄</td>
        <td class="listTable">电话</td>
        <td class="listTable">等级</td>
        <td class="listTable">身份证</td>
        <td class="listTable">地区</td>
        <td class="listTable">地址</td>
        <td class="listTable">微信</td>
        <td class="listTable">QQ</td>
        <td class="listTable">邮箱</td>
        <td class="listTable">职业</td>
        <td id="profile">备注</td>
        <td id="operate">操作</td>
    </tr>

    </td>
    <!--/*@thymesVar id="list" type="java"*/-->
    <li th:each = "user, iterStat : ${list} " >
        <tr>
            <td  th:text="${iterStat.count}">
            </td>
            <td   th:text="${user.getName()}">
            </td>
            <td th:text="${user.getSex()}">
            </td>
            <td th:text="${user.getAge()}">
            </td>
            <td th:text="${user.getTelephone()}">
            </td>
            <td th:text="${user.getGrade()}">
            </td>
            <td th:text="${user.getIdCard()}">
            </td>
            <td th:text="${user.getArea()}">${
            </td>
            <td th:text="${user.getAddress()}">
            </td>
            <td th:text="${user.getWeixin()}">
            </td>
            <td th:text="${user.getQq()}">$
            </td >
            <td th:text="${user.getEmail()}">$
            </td>
            <td th:text="${user.getJob()}">
            </td>
            <td th:text="${user.getRemark()}">
            </td>
            <td><a  th:href="@{/getUserMoreInfo(id = ${user.getId()})}" >查看&nbsp;&nbsp;</a>
                <a th:href="@{/modifyUser(id = ${user.getId()})} ">修改&nbsp;&nbsp;</a>
                <a th:href="@{/delUser(id = ${ user.getId()})} "
                   onclick="return check()">删除&nbsp;&nbsp;</a></td>
        </tr>

    </li>


</table>

</body>
</html>

点击 源码下载


 

posted on 2019-01-25 10:57  LoaderMan  阅读(1581)  评论(0编辑  收藏  举报

导航