之前编写了servlet+jsp版本的客户信息管理系统,在学习了ssm框架和maven的相关知识后,尝试着使用ssm+maven来实现客户信息管理系统。
项目下载地址:ssm+maven实现客户信息管理系统。
这里直接使用idea创建出maven项目,并且在补全相应目录后,构建所得的项目结构如下:
使用maven管理项目的好处就是,不需要你自己去官网下载项目所需要的jar包,maven会自动帮你从本地仓库或者中央仓库寻找jar包。具体方式是通过pom.xml引入。
pom.xml
4.0.0
com.silver
customer
1.0-SNAPSHOT
war
customer Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.12
test
taglibs
standard
1.1.2
jstl
jstl
1.2
javax.servlet
javax.servlet-api
3.1.0
mysql
mysql-connector-java
5.1.46
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
3.4.6
org.mybatis
mybatis-spring
1.3.2
org.springframework
spring-core
4.3.8.RELEASE
org.springframework
spring-beans
4.3.8.RELEASE
org.springframework
spring-context
4.3.8.RELEASE
org.springframework
spring-tx
4.3.8.RELEASE
org.springframework
spring-jdbc
4.3.8.RELEASE
org.springframework
spring-web
4.3.8.RELEASE
org.springframework
spring-webmvc
4.3.8.RELEASE
org.springframework
spring-test
4.3.8.RELEASE
customer
maven-clean-plugin
3.0.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.7.0
maven-surefire-plugin
2.20.1
maven-war-plugin
3.2.0
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
dao层主要是进行数据的持久化操作,连接数据库并实现CURD操作。
创建客户信息表customer以及插入数据,这里使用ddl语句建表。对应的sql文件放在java/sql目录下
-- 数据库初始化脚本
-- 创建数据库
CREATE DATABASE customer;
-- 切换到customer数据库
use customer;
-- 建表
CREATE TABLE `customer` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`gender` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`description` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入数据
INSERT INTO customer(name,gender,phone,email,description)
VALUES ('小明','男','13723232333','小明@126.com','我是小明'),
('小红','女','15689897568','小红@126.com','我是小红'),
('张飞','男','15826547965','张飞@126.com','我是张飞'),
('关羽','男','13621214896','关羽@126.com','我是关羽'),
('刘备','男','12345678910','刘备@126.com','我是刘备');
在java/com/silver目录下新建entity包,用来存放实体类。entity包下新建Customer.java
package com.silver.entity;
/**
* 封装客户信息(id,姓名,性别,电话,邮箱,个人描述)的pojo类
* @author 光玉
* @create 2018-05-16 22:36
**/
public class Customer {
private long id;
private String name;
private String gender;
private String phone;
private String email; // 不是必填项
private String description; // 不是必填项
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean isNull(){ // 判断除id外的属性是否为空,用于后面编写controller模糊查询时的判断
if(this.name == null && this.gender == null && this.phone == null
&& this.email == null && this.description == null){
return true;
}
return false;
}
@Override
public String toString() {
return "Customer{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
", description='" + description + '\'' +
'}';
}
}
还需要一个分页操作的PageBean.java
package com.silver.entity;
import java.util.List;
/**
* 进行分页操作的Java Bean封装
* @author 光玉
* @create 2018-05-17 14:09
**/
public class PageBean
在java/com/silver下新建dao包,在dao包中新建CustomerDao接口,用来实现数据库CURD操作。
package com.silver.dao;
import com.silver.entity.Customer;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 客户信息dao层编写(增,删,查,改操作)
* @author 光玉
* @create 2018-05-16 22:49
**/
public interface CustomerDao {
/**
* 由于有多个形参,在spring中形参是以arg0,arg1...的形式传递的,需要使用@Param设置对应的名字
* 分页查询客户
* @param offset 偏移量
* @param pageRecord 每页记录数
* @return
*/
public List findAll(@Param("offset") int offset,@Param("pageRecord") int pageRecord);
/**
* 通过id查找应客户信息
* @param id
* @return
*/
public Customer findById(long id) throws Exception;
/**
* 通过传入的customer对象获取对应客户信息,并将其插入数据库
* @param customer
*/
public void addCustomer(Customer customer) throws Exception;
/**
* 通过id来删除对应的客户
* @param id
*/
public void deleteCustomer(long id) throws Exception;
/**
* 根据id修改信息,传入要修改的信息
* @param id
* @param customer
* @throws Exception
*/
public void editCustomer(@Param("id") long id,@Param("customer") Customer customer)throws Exception;
/**
* 返回客户列表的总记录数(模糊查询时,返回模糊查询总记录数)
* @param customer
* @return
* @throws Exception
*/
public int countCustomers(@Param("customer") Customer customer)throws Exception;
/**
* 分页查询客户,并且有模糊查询功能
* @param offset
* @param pageRecord
* @param customer
* @return
* @throws Exception
*/
public List queryAll(@Param("offset") int offset,@Param("pageRecord") int pageRecord,@Param("customer") Customer customer) throws Exception;
}
mybatis作为持久层框架,用来和数据库进行交互。
在resources目录下新建mybatis-config.xml文件。
在resources目录下新建mapper包,用来存放映射文件。在mapper包下新建CustomerDao.xml文件。
INSERT INTO customer(id,name,gender,phone,email,description)
VALUES (null,#{name},#{gender},#{phone},#{email},#{description})
DELETE FROM customer
WHERE id = #{id}
UPDATE customer
SET name = #{customer.name},
phone = #{customer.phone},
gender = #{customer.gender},
email = #{customer.email},
description = #{customer.description}
WHERE id = #{id}
WHERE 1 = 1
and name like "%${customer.name}%"
and gender like "%${customer.gender}%"
and phone like "%${customer.phone}%"
and email like "%${customer.email}%"
and description like "%${customer.description}%"
首先,在resources目录下新建jdbc.properties文件,用来设置数据库相关参数。(这里使用的是mysql数据库,如果使用的 是其他数据库,请自行修改参数)
# url中“useSSL=false”表示不使用SSL连接(也可以设置为true),如果使用高版本的mysql,不写上的话会在控制台输出警告
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/customer?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=mysql
jdbc.password=123456
接着,在resources目录下新建spring目录,在spring目录下新建spring-dao.xml文件,用来整合spring和mybatis。
测试类保存在test目录中,生成的测试类CustomerDaoTest.java,目录结构及代码如下:
package com.silver.dao;
import com.silver.entity.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
// 配置文件路径
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class CustomerDaoTest {
// 注入依赖
@Resource
private CustomerDao customerDao;
@Test
public void findAll() {
int pageNum = 1; // 当前页数
int pageRecord = 10; // 每页记录数
List customers = customerDao.findAll((pageNum - 1) * pageRecord, pageRecord);
for (Customer c : customers) {
System.out.println(c);
}
}
@Test
public void findById() throws Exception {
// 根据id查找对应客户
long id = 3L;
Customer customer = customerDao.findById(id);
System.out.println(customer);
}
@Test
public void addCustomer() throws Exception {
// 添加客户
Customer customer = new Customer();
customer.setName("曹操");
customer.setGender("男");
customer.setPhone("13245987562");
//customer.setEmail(null);
customer.setDescription("我是曹操");
customerDao.addCustomer(customer);
}
@Test
public void deleteCustomer() throws Exception {
// 通过id删除客户
long id = 4L;
customerDao.deleteCustomer(id);
}
@Test
public void editCustomer() throws Exception {
// 修改客户信息
long id = 1L;
Customer customer = customerDao.findById(id);
customer.setDescription("小明同学"); // 修改id为1的客户的个人描述
customerDao.editCustomer(id,customer);
System.out.println(customerDao.findById(id)); // 输出修改后的信息
}
@Test
public void countCustomers() throws Exception{
// 查找客户总记录数
Customer customer = new Customer(); // 进行模糊查询所要传入的客户信息
customer.setName("小");
int count = customerDao.countCustomers(customer);
System.out.println("客户数量:"+count);
}
@Test
public void queryAll() throws Exception {
// 分页显示客户列表,并且有模糊查询的功能
int pageNum = 1; // 页数
int pageRecord = 10; // 每页记录数
Customer customer = new Customer(); // 设置要模糊查询的客户信息
customer.setName("小"); // 按照名字模糊查询
List customers = customerDao.queryAll((pageNum - 1) * pageRecord, pageRecord, customer);
for (Customer c : customers) {
System.out.println(c);
}
}
}
这里使用的是junit4测试工具,在pom.xml中有进行配置。
service层主要是处理相关的业务逻辑,通过调用dao层实现相应操作。
在com/silver目录下新建service包,在service包下新建CustomerService接口。
package com.silver.service;
import com.silver.entity.Customer;
import com.silver.entity.PageBean;
import java.util.List;
/**
* 业务层,编写业务逻辑,调用Dao层
* @author 光玉
* @create 2018-05-17 16:07
**/
public interface CustomerService {
/**
* 分页查询
* @param offset
* @param pageRecord
* @return
*/
public PageBean allList(int offset, int pageRecord) throws Exception;
/**
* 通过id查找客户
* @param id
* @return
* @throws Exception
*/
public Customer getById(long id)throws Exception;
/**
* 添加客户
* @param customer
* @throws Exception
*/
public void insert(Customer customer)throws Exception;
/**
* 通过id删除客户
* @param id
* @throws Exception
*/
public void delete(long id)throws Exception;
/**
* 修改客户信息
* @param id
* @param customer
* @throws Exception
*/
public void update(long id,Customer customer)throws Exception;
/**
* 分页查询,模糊查询,还需要在Service实现类中编写一些业务逻辑
* @param offset
* @param pageRecord
* @param customer
* @return
* @throws Exception
*/
public PageBean queryList(int offset, int pageRecord, Customer customer)throws Exception;
}
在service包下新建impl包,在impl包下新建service实现类CustomerServiceImpl。在实现类中编写相关业务逻辑代码。
package com.silver.service.impl;
import com.silver.dao.CustomerDao;
import com.silver.entity.Customer;
import com.silver.entity.PageBean;
import com.silver.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Service接口的实现类
* @author 光玉
* @create 2018-05-17 16:54
**/
@Service
public class CustomerServiceImpl implements CustomerService{
// 注入依赖
@Autowired
private CustomerDao customerDao;
@Override
public PageBean allList(int offset, int pageRecord) throws Exception{
List customers = customerDao.findAll(offset,pageRecord);
Customer customer = new Customer();
int totalRecord = customerDao.countCustomers(customer); // 获取总记录数
int pageNum = offset/pageRecord + 1; // 当前页数
// 计算总页数
int totalPage;
if (totalRecord % pageRecord == 0){
totalPage = totalRecord/pageRecord;
} else {
totalPage = totalRecord/pageRecord + 1;
}
PageBean pageBean = new PageBean<>(); // 将分页数据封装到PageBean中
pageBean.setBeanList(customers);
pageBean.setPageNum(pageNum);
pageBean.setPageRecord(pageRecord);
pageBean.setTotalRecord(totalRecord);
pageBean.setTotalPage(totalPage);
return pageBean; // 返回封装后的数据
}
@Override
public Customer getById(long id) throws Exception {
Customer customer = customerDao.findById(id);
return customer;
}
@Override
public void insert(Customer customer) throws Exception {
customerDao.addCustomer(customer);
}
@Override
public void delete(long id) throws Exception {
customerDao.deleteCustomer(id);
}
@Override
public void update(long id,Customer customer) throws Exception {
customerDao.editCustomer(id,customer);
}
@Override
public PageBean queryList(int offset, int pageRecord, Customer customer) throws Exception {
List customers = customerDao.queryAll(offset,pageRecord,customer); // 获取每页的所有记录
int totalRecord = customerDao.countCustomers(customer); // 获取模糊查询总记录数
int pageNum = offset/pageRecord + 1; // 当前页数
// 计算总页数
int totalPage;
if (totalRecord % pageRecord == 0){
totalPage = totalRecord/pageRecord;
} else {
totalPage = totalRecord/pageRecord + 1;
}
PageBean pageBean = new PageBean<>(); // 将分页数据封装到PageBean中
pageBean.setBeanList(customers);
pageBean.setPageNum(pageNum);
pageBean.setPageRecord(pageRecord);
pageBean.setTotalRecord(totalRecord);
pageBean.setTotalPage(totalPage);
return pageBean; // 返回封装后的数据
}
}
在resources/spring目录下,新建spring-service.xml文件,用来管理service层。
package com.silver.service;
import com.silver.entity.Customer;
import com.silver.entity.PageBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml",
"classpath:spring/spring-service.xml"})
public class CustomerServiceTest {
@Autowired
private CustomerService customerService;
@Test
public void allList() throws Exception{
PageBean pageBean = customerService.allList(0, 8); // 设置每页8条记录
System.out.println(pageBean);
}
@Test
public void getById() throws Exception{
long id = 1L;
Customer customer = customerService.getById(id);
System.out.println(customer);
}
@Test
public void insert() throws Exception{
Customer customer = new Customer();
customer.setName("吕布");
customer.setGender("男");
customer.setPhone("15823232333");
customerService.insert(customer);
}
@Test
public void delete() throws Exception{
long id = 7L;
customerService.delete(id);
}
@Test
public void update() throws Exception{
long id = 1L;
Customer customer = customerService.getById(id);
customer.setDescription("我是小明");
customerService.update(id,customer);
System.out.println(customer);
}
@Test
public void queryList() throws Exception{
int offset = 0;
int pageRecord = 8;
Customer customer = new Customer();
customer.setName("小");
PageBean pageBean = customerService.queryList(offset, pageRecord, customer);
System.out.println(pageBean);
}
}
controller(handler),也就是控制器,负责和前端页面进行交互(如:显示前端页面视图,接收前端传过来的数据等)。
在com/silver下新建controller包,在controller包下新建CustomerController.java
package com.silver.controller;
import com.silver.entity.Customer;
import com.silver.entity.PageBean;
import com.silver.service.CustomerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 编写控制器,用来和前端页面进行交互
* @author 光玉
* @create 2018-05-18 15:40
**/
@Controller
@RequestMapping("/customer") // 相当于模块化,这个模块下的请求格式为“/customer/**”
public class CustomerController {
@Autowired
private CustomerService customerService;
private Customer myCustomer; // 用来保存模糊查询的客户信息
// 这里设置了只能使用get方法传递url
@RequestMapping(value = "/frame", method = RequestMethod.GET)
public String mainFrame(){
// 显示主界面
return "frame";
}
@RequestMapping(value = "/allCustomerList/{pageNum}", method = RequestMethod.GET)
public String allCustomerList(@PathVariable("pageNum") Integer pageNum,Model model) throws Exception{
// 显示所有客户列表
// 获取从前端传过来的当前页数,并进行分页操作,显示列表
if(pageNum == null){
pageNum = 1; // 空则当前页面设置为1
}
int pageRecord = 8; // 设置每页8条记录
PageBean pb = customerService.allList((pageNum-1)*pageRecord, pageRecord);
myCustomer = new Customer(); // 实例化myCustomer对象(如果之前有进行模糊查询操作,则相当于清除保存在myCustomer对象中的信息)
model.addAttribute("pb",pb); // 将pb对象传到前端页面中
return "list";
}
@RequestMapping(value = "/add",method = RequestMethod.GET) // 设置传递的url以及设置访问方式
public String addCustomer() throws Exception{
return "add"; // 返回添加客户的jsp页面
}
@RequestMapping(value = "/addCustomerSubmit",method = RequestMethod.POST)
public String addCustomerSuccess(Customer customer) throws Exception{
// 进行添加客户操作
customerService.insert(customer);
return "redirect:allCustomerList/1"; // 重定向到列表页面,显示第一页
}
@RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
public String editCustomer(@PathVariable("id") Long id, Model model)throws Exception{
// 到要修改客户的信息页
Customer customer = customerService.getById(id);
model.addAttribute("customer",customer);
return "edit";
}
@RequestMapping(value = "/editCustomerSubmit",method = RequestMethod.POST)
public String editCustomerSubmit(@Param("id") Long id,@Param("customer") Customer customer) throws Exception{
// 传入id和要修改的相关客户信息,执行更新操作
customerService.update(id,customer);
return "redirect:allCustomerList/1";
}
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
public void deleteCustomer(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response)throws Exception{
// 删除客户信息
customerService.delete(id);
String url = request.getRequestURL().toString(); // 获取当前请求的url
String newUrl = url.substring(0,url.lastIndexOf("delete"))
+ "allCustomerList/1"; // 重新拼接url
response.sendRedirect(newUrl); // 重定向回客户列表的第一页
}
@RequestMapping(value = "/query",method = RequestMethod.GET)
public String queryList(){
// 进入模糊查询页面
return "query";
}
// 由于模糊查询需要使用post提交表单,这里设置了可以使用get和post方法传递url
@RequestMapping(value = "/list/{pageNum}", method = {RequestMethod.GET, RequestMethod.POST})
public String queryCustomerList(@PathVariable("pageNum") Integer pageNum,Customer customer, Model model) throws Exception{
// 获取从前端传过来的当前页数,并进行分页操作,显示列表,具有模糊查询的功能
if(pageNum == null){
pageNum = 1; // 空则当前页面设置为1
}
int pageRecord = 8; // 设置每页记录数为8
if(!customer.isNull()){ // 如果模糊查询中有设置查询信息,则将信息保存到myCustomer对象中
myCustomer = customer;
}
PageBean pb = customerService.queryList((pageNum-1)*pageRecord, pageRecord,myCustomer);
model.addAttribute("pb",pb);
return "list";
}
@RequestMapping(value = "/DevelopDoc",method = RequestMethod.GET)
public String developDoc(){
// 显示开发日志页面
return "DevelopDoc";
}
修改webapp/WEB-INF目录下的web.xml文件。
customer-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-*.xml
customer-dispatcher
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
在resources/spring目录下新建spring-web.xml文件。用来进行视图,静态资源处理等相关配置。
在spring-web.xml配置文件中配置了一个去除两边空格的转换器TrimStringConverter.java,所在目录为com/silver/controller/converter。因此,我们需要在com/silver/controller下新建converter包,在converter包下新建TrimStringConverter.java。
package com.silver.controller.converter;
import org.springframework.core.convert.converter.Converter;
/**
* 去除前后空格的转换器
* @author 光玉
* @create 2018-05-20 16:45
**/
public class TrimStringConverter implements Converter {
// 编写去除前后空格的转换器
// 若去除空格后字符串为空,则返回null,否则返回去空格后的字符串
@Override
public String convert(String source){
try{
if(source != null){
source=source.trim();
if(source.equals("")){
return null;
}
}
}catch(Exception e){
e.printStackTrace();
}
return source;
}
}
jsp页面放在WEB-INF/jsp目录下,layui前端插件放置在webapp/resources目录下。如有需要,请自行到customer-maven项目下载。
输入http://localhost:8080/customer/frame查看效果
1. 客户列表页
2. 添加客户
3. 高级搜索
4. 开发日志
最后,项目源码放在ssm+maven实现客户信息管理系统中,有需要的请自行下载。