基于Spring MVC + Spring + MyBatis的物流系统 - 公司信息管理

Posted 明金同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Spring MVC + Spring + MyBatis的物流系统 - 公司信息管理相关的知识,希望对你有一定的参考价值。

资源下载:https://download.csdn.net/download/weixin_44893902/45601768

练习点设计:模糊查询、删除、新增

一、语言和环境

  1. 实现语言:JAVA语言。
  2. 环境要求:MyEclipse/Eclipse + Tomcat + mysql
  3. 使用技术:Jsp+Servlet+JavaBeanSpringMVC + Spring + Mybatis

二、实现功能

使用 SSM(Spring+SpringMVC+MyBatis)框架开发物流系统 - 公司信息管理模块,具体实现如下功能:

1,首页显示所有公司信息,如图 1 所示。

2,实现按公司名称模糊查询功能:在输入框中输入要查询的公司名称,点击“查找”按钮,显示相关的公司信息。

3,实现删除功能,删除之前弹出“删除确认”提示,删除成功后显示最新数据。效果图如图3所示。

4,实现增加公司信息的功能:点击首页上的“添加公司信息”,跳转到添加公司信息页面,页面效果如图4所示,完成增加功能,公司编号和公司名称必须输入。

三、数据库设计

1.创建数据库(logisticsDB)。

2.创建公司信息(tb_company)表,结构见表 1。
表1 公司信息表结构

四、推荐实现步骤

  1. 建立数据库和数据表,并且添加测试数据(至少添加5条测试数据)。

  2. 打开 Eclipse 或 Idea,创建 Web 工程,并创建相应包。

  3. 为工程添加 Spring、SpringMVC 和 MyBatis 支持。

  4. 在工程中创建实体类和与实体对应的 MyBatis 映射文件,正确配置MySQL 的 SQL 映射文件。

  5. 创建 Mapper映射器、业务类。在 映射器或映射文件中 编写 SQL 语句实现查询和增加操作。

  6. 创建 Controller 类,正确配置 SpringMVC 的配置文件。

  7. 正确配置 Spring 的配置文件,并有效管理 SQLSessionFactory。

五、实现代码

1、mysql代码

1.创建数据库(logisticsDB)。

2.创建公司信息(tb_company)表。

/*
 Date: 25/07/2021 22:02:55
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tb_company
-- ----------------------------
DROP TABLE IF EXISTS `tb_company`;
CREATE TABLE `tb_company`  (
  `Company_id` int(11) NOT NULL AUTO_INCREMENT,
  `Company_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `Company_city` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `Company_phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `Company_fax` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `Company_adress` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `Company_remark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`Company_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1006 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of tb_company
-- ----------------------------
INSERT INTO `tb_company` VALUES (1001, '长沙飞马传媒有限公司', '湖南湘潭', '0731-2784651', '0731-2784651', '望城', NULL);
INSERT INTO `tb_company` VALUES (1002, '长沙长奔汽车', '湖南株洲', '0731-2784555', '0731-2784555', '茶陵', NULL);
INSERT INTO `tb_company` VALUES (1003, '湖南汇通科技', '湖南长沙', '0731-2784777', '0731-2784777', '长沙', NULL);
INSERT INTO `tb_company` VALUES (1004, '长沙科技', '湖南长沙', '0731-2784651', '0731-2784651', '湘潭', NULL);

SET FOREIGN_KEY_CHECKS = 1;

2、项目Java代码

目录结构
Company

JAR包:

src

com.controller

CompanyController.java

package com.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.TbCompany;
import com.service.CompanyService;

@Controller
public class CompanyController 
	@Resource
	CompanyService companyService;

	// 查询与模糊查询
	@RequestMapping("/CompanysList")
	public String CompanysList(Model model, String Company_name) 
		List<TbCompany> companyList = companyService.selectAll(Company_name);
		model.addAttribute("companyList", companyList);
		return "/company";
	

	// 跳转到添加页面
	@RequestMapping("/insertInto")
	public String insertInto() 
		return "/addCompany";
	

	// 添加
	@RequestMapping("/insertCompany")
	public String insertCompany(TbCompany tbCompany) 
		int insertCompany = companyService.insertCompany(tbCompany);
		return "redirect:/CompanysList.do";
	

	// 删除
	@RequestMapping("/delCompany")
	public String delCompany(int companyId) 
		int delCompany = companyService.delCompany(companyId);
		return "redirect:/CompanysList.do";
	



com.dao

TbCompanyMapper.java

package com.dao;

import com.entity.TbCompany;
import java.util.List;

import org.apache.ibatis.annotations.Param;

public interface TbCompanyMapper 
    int deleteByPrimaryKey(Integer companyId);

    int insert(TbCompany record);

    List<TbCompany> selectAll(@Param("Company_name")String Company_name);
 

TbCompanyMapper.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" >
<mapper namespace="com.dao.TbCompanyMapper" >
  <resultMap id="BaseResultMap" type="com.entity.TbCompany" >
    <id column="Company_id" property="companyId" jdbcType="INTEGER" />
    <result column="Company_name" property="companyName" jdbcType="VARCHAR" />
    <result column="Company_city" property="companyCity" jdbcType="VARCHAR" />
    <result column="Company_phone" property="companyPhone" jdbcType="VARCHAR" />
    <result column="Company_fax" property="companyFax" jdbcType="VARCHAR" />
    <result column="Company_adress" property="companyAdress" jdbcType="VARCHAR" />
    <result column="Company_remark" property="companyRemark" jdbcType="VARCHAR" />
  </resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tb_company
    where Company_id = #companyId,jdbcType=INTEGER
  </delete>
  <insert id="insert" parameterType="com.entity.TbCompany" >
    insert into tb_company (Company_id, Company_name, Company_city, 
      Company_phone, Company_fax, Company_adress, 
      Company_remark)
    values (#companyId,jdbcType=INTEGER, #companyName,jdbcType=VARCHAR, #companyCity,jdbcType=VARCHAR, 
      #companyPhone,jdbcType=VARCHAR, #companyFax,jdbcType=VARCHAR, #companyAdress,jdbcType=VARCHAR, 
      #companyRemark,jdbcType=VARCHAR)
  </insert>

  <select id="selectAll" resultMap="BaseResultMap" >
    select Company_id, Company_name, Company_city, Company_phone, Company_fax, Company_adress, 
    Company_remark
    from tb_company
    <where>
		<if test="Company_name!= null">Company_name like "%"#Company_name"%"   </if>
	</where>
  </select>
</mapper>

com.entity

TbCompany.java

package com.entity;

public class TbCompany 
    private Integer companyId;

    private String companyName;

    private String companyCity;

    private String companyPhone;

    private String companyFax;

    private String companyAdress;

    private String companyRemark;

    public Integer getCompanyId() 
        return companyId;
    

    public void setCompanyId(Integer companyId) 
        this.companyId = companyId;
    

    public String getCompanyName() 
        return companyName;
    

    public void setCompanyName(String companyName) 
        this.companyName = companyName == null ? null : companyName.trim();
    

    public String getCompanyCity() 
        return companyCity;
    

    public void setCompanyCity(String companyCity) 
        this.companyCity = companyCity == null ? null : companyCity.trim();
    

    public String getCompanyPhone() 
        return companyPhone;
    

    public void setCompanyPhone(String companyPhone) 
        this.companyPhone = companyPhone == null ? null : companyPhone.trim();
    

    public String getCompanyFax() 
        return companyFax;
    

    public void setCompanyFax(String companyFax) 
        this.companyFax = companyFax == null ? null : companyFax.trim();
    

    public String getCompanyAdress() 
        return companyAdress;
    

    public void setCompanyAdress(String companyAdress) 
        this.companyAdress = companyAdress == null ? null : companyAdress.trim();
    

    public String getCompanyRemark() 
        return companyRemark;
    

    public void setCompanyRemark(String companyRemark) 
        this.companyRemark = companyRemark == null ? null : companyRemark.trim();
    

com.service

CompanyService.java

package com.service;

import java.util.List;

import com.entity.TbCompany;

public interface CompanyService 
	
	//查询
	List<TbCompany> selectAll(String Company_name);
	//添加
	int insertCompany(TbCompany tbCompany);
	//删除
	int  delCompany(int companyId);


com.serviceImpl

CompanyServiceImpl.java

package com.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dao.TbCompanyMapper;
import com.entity.TbCompany;
import com.service.CompanyService;
@Service
public class CompanyServiceImpl implements CompanyService 

	@Resource
	TbCompanyMapper mapper;
	@Override
	public List<TbCompany> selectAll(String Company_name) 
		List<TbCompany> selectAll=mapper.selectAll(Company_name);
		return selectAll;
	

	@Override
	public int insertCompany(TbCompany tbCompany) 
		int insertCompany=mapper.insert(tbCompany);
		return insertCompany;
	

	@Override
	public int delCompany(int companyId) 
		int delCompany=mapper.deleteByPrimaryKey(companyId);
		return delCompany;
	



mybatis

sqlMapConfig.xml.java

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 别名 -->
	<typeAliases>
		<package name="com.entity" />
	</typeAliases>
</configuration>

spring

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.2.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
	
	<!-- 指定spring容器读取db.properties文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
	<!-- 将连接池注册到bean容器中 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="$jdbc.driver"></property>
		<property name="Url" value="$jdbc.url"></property>
		<property name="username" value="$jdbc.username"></property>
		<property name="password" value="$jdbc.password"></property>
	</bean>
	<!-- 配置SqlSessionFactory -->
	<bean class="org.my

以上是关于基于Spring MVC + Spring + MyBatis的物流系统 - 公司信息管理的主要内容,如果未能解决你的问题,请参考以下文章

Spring+Spring mvc+Mybatis整合教程(基于Maven)

Spring : 基于注解的 Spring MVC( 上 )

基于Spring MVC + Spring + MyBatis的医院就诊挂号系统

基于Spring MVC + Spring + MyBatis的医院就诊挂号系统

基于Spring MVC + Spring + MyBatis的网上购物系统

基于Spring MVC + Spring + MyBatis的银行卡系统