Bootstrap+Thymeleaf+Sping boot+Mybatis整合
前言:项目是做一个进销存管理中的供应商管理和联系人管理
供应商和联系人是一对多表的关系,需求就是可以对供应商和联系人做增删改查,可以通过供应商的编号名称检索出联系人的信息
一、环境配置
- IDE:Intellij IDEA 2018.2
- JDK:1.8.0_181
- maven:4.0.0
- Spring boot:2.0.4
- mybatis-generator-core-1.3.7
1. pom.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> <groupId>com.power.inventory</groupId> <artifactId>inventory</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>inventory</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.13</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/.svn/*</exclude> </excludes> </resource> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/.svn/*</exclude> </excludes> <includes> <include>**/*.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> </project>
2.数据库(我用的是放在虚拟机中的mysql)
由于表早就建好了 我就直接show create table了
供应商表:
mysql> show create table SYS_SUPPLIER; +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | SYS_SUPPLIER | CREATE TABLE `SYS_SUPPLIER` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, `remark` varchar(200) DEFAULT NULL, `on_off` tinyint(1) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
联系人表:
mysql> show create table SYS_LINKMAN; +-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | SYS_LINKMAN | CREATE TABLE `SYS_LINKMAN` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, `tel` bigint(50) NOT NULL, `email` varchar(200) NOT NULL, `address` varchar(200) DEFAULT NULL, `main_linkman` tinyint(1) DEFAULT NULL, `sys_supplier_id` bigint(20) unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `sys_supplier_id` (`sys_supplier_id`), CONSTRAINT `SYS_LINKMAN_ibfk_2` FOREIGN KEY (`sys_supplier_id`) REFERENCES `SYS_SUPPLIER` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 | +-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
从Axure RP图中可以看出供应商表需要为供应商做一个开关按钮,联系人表中也有一个字段main_linkman代表是否为首要联系人,这很明显都是boolean类型的,值是true开关开启(是),值为false开关关闭(否),但是MYSQL中没有boolean类型,MYSQL保存boolean值时用1代表true,0代表false,boolean在MySQL里的类型为tinyint(1),另外如果在建表的时候这样写:
create table student
(
id int primary key,
sex boolean
)
表是可以创建成功的,但查看一下建表后的语句,就会发现,mysql把它替换成tinyint(1)。所以在MYSQL中boolean类型的变量就是用tinyint(1)来定义的。
还有需要注意的就是在联系人表的外键sys_supplier_id后面加上ON DELETE CASCADE 级联删除的时候用,就是删除一条供应商的信息(主表)与其相关的所有联系人的信息(从表)也会被删除
二、前台页面
supplierManager.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title>供应商管理</title> <link th:href="@{/power-0.1.css}" rel="stylesheet"/> <link th:href="@{/Chen.css}" rel="stylesheet"/> <!-- Bootstrap Core CSS --> <link th:href="@{/vendor/bootstrap/css/bootstrap.css}" rel="stylesheet"/> <!-- MetisMenu CSS --> <link th:href="@{/vendor/metisMenu/metisMenu.min.css}" rel="stylesheet"> <!-- Custom CSS --> <link th:href="@{/dist/css/sb-admin-2.css}" rel="stylesheet"> <!-- Morris Charts CSS --> <link th:href="@{/vendor/morrisjs/morris.css}" rel="stylesheet"> <!-- Custom Fonts --> <link th:href="@{/vendor/font-awesome/css/font-awesome.min.css}" rel="stylesheet" type="text/css"> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <!--<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>--> <!--<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>--> <![endif]--> </head> <!--取消横向滚动条--> <body style="overflow:-Scroll;overflow-x:hidden"> <div id="wrapper"> <!-- Navigation --> <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0"> <div class="navbar-header"> <!--<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">--> <!--<span class="sr-only">Toggle navigation</span>--> <!--<span class="icon-bar"></span>--> <!--<span class="icon-bar"></span>--> <!--<span class="icon-bar"></span>--> <!--</button>--> <a class="navbar-brand" href="main">Power 进销存 0.1</a> </div> <!-- /.navbar-header --> <ul class="nav navbar-top-links navbar-right"> <!-- /.dropdown --> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i> </a> <ul class="dropdown-menu dropdown-user"> <li><a href="login"><i class="fa fa-sign-out fa-fw"></i> Logout</a> </li> </ul> <!-- /.dropdown-user --> </li> <!-- /.dropdown --> </ul> <!-- /.navbar-top-links --> <div class="navbar-default sidebar" role="navigation"> <div class="sidebar-nav navbar-collapse"> <ul class="nav" id="side-menu"> <li> <a href="#"><i class="fa fa-database fa-fw"></i>基础数据管理<span class="fa arrow"></span></a> <ul class="nav nav-second-level"> <li> <a href="/base_data/user">用户管理</a> </li> <li> <a href="#">员工管理</a> </li> <li> <a href="http://localhost:8081/supplierManager">供应商管理</a> </li> <li> <a href="http://localhost:8081/linkmanManager">联系人管理</a> </li> <li> <a href="#">商品管理</a> </li> <li> <a href="#">仓库管理</a> </li> <li> <a href="#">客户管理</a> </li> <li> <a href="#">费用类别管理</a> </li> </ul> <!-- /.nav-second-level --> </li> <li> <a href="#"><i class="fa fa-database fa-fw"></i>采购管理<span class="fa arrow"></span></a> <ul class="nav nav-second-level"> <li> <a href="#">采购管理</a> </li> <li> <a href="#">采购退货管理</a> </li> </ul> </li> <li> <a href="#"><i class="fa fa-database fa-fw"></i>销售管理<span class="fa arrow"></span></a> <ul class="nav nav-second-level"> <li> <a href="#">销售管理</a> </li> <li> <a href="#">销售退货管理</a> </li> </ul> </li> <li> <a href="#"><i class="fa fa-database fa-fw"></i>费用管理<span class="fa arrow"></span></a> <ul class="nav nav-second-level"> <li> <a href="#">费用清单管理</a> </li> </ul> </li> <li> <a href="#"><i class="fa fa-database fa-fw"></i>库存管理<span class="fa arrow"></span></a> <ul class="nav nav-second-level"> <li> <a href="#">商品库存余额表</a> </li> </ul> </li> <li> <a href="#"><i class="fa fa-database fa-fw"></i>统计分析<span class="fa arrow"></span></a> <ul class="nav nav-second-level"> <li> <a href="#">利润表</a> </li> <li> <a href="#">费用表</a> </li> </ul> </li> </ul> </div> <!-- /.sidebar-collapse --> </div> <!-- /.navbar-static-side --> </nav> <div id="page-wrapper"> <div class="row"> <div class="col-lg-12"> <h1 class="page-header">供应商管理</h1> </div> <!-- /.col-lg-12 --> </div> <div class="anniu"> <!--添加供应商--> <button type="button" class="btn btn-info bot" data-toggle="modal" data-target="#addSupplierModal"> <span class="glyphicon glyphicon-plus aria-hidden="true"">添加</span></button> <div class="modal fade" id="addSupplierModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <!--模态框头部--> <div class="modal-header"> <h4 class="modal-title" id="myModalLabel">新增供应商</h4> </div> <!--表单提交模态框内部底部--> <form action="/addSupplier" method="post" class="form-horizontal" role="form"> <div class="modal-body"> <!--联系人的编号--> <div class="form-group"> <label class="col-sm-4 control-label">编号</label> <div class="col-sm-5"> <input type="text" class="form-control" name="supplierId" placeholder="编号"> </div> </div> <!--联系人的姓名--> <div class="form-group"> <label class="col-sm-4 control-label">名称</label> <div class="col-sm-5"> <input type="text" class="form-control" name="supplierName" placeholder="名称"> </div> </div> <div class="form-group"> <label class="col-sm-4 control-label">备注</label> <div class="col-sm-5"> <input type="text" class="form-control" name="supplierRemark" placeholder="备注"> </div> </div> <div class="form-group"> <label class="col-sm-4 control-label">状态</label> <div class="col-sm-5"> <input type="text" class="form-control" name="supplierOnOff" placeholder="状态"> </div> </div> </div> <!--模态框底部--> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> <button type="submit" class="btn btn-primary">保存</button> </div> </form> </div><!-- /.modal-content --> </div><!-- /.modal --> </div> <!--删除供应商--> <button type="button" class="btn btn-info bot" data-toggle="modal" data-target="#deleteSupplierModal"> <span class="glyphicon glyphicon-trash aria-hidden="true"">删除</span></button> <div class="modal fade" id="deleteSupplierModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <!--模态框头部--> <div class="modal-header"> <h4 class="modal-title" id="myModalLabel">删除供应商</h4> </div> <!--表单提交模态框内部底部--> <form action="/deleteSupplier" method="post" class="form-horizontal" role="form"> <div class="modal-body"> <!--联系人的编号--> <div class="form-group"> <label class="col-sm-4 control-label">编号</label> <div class="col-sm-5"> <input type="text" class="form-control" name="SupplierId" placeholder="编号"> </div> </div> </div> <!--模态框底部--> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> <button type="submit" class="btn btn-primary">保存</button> </div> </form> </div><!-- /.modal-content --> </div><!-- /.modal --> </div> <!--<!–联系人管理–>--> <!--<a href="/linkmanManager"><button type="button" class="btn btn-info bot">编辑</button></a>--> </div> <div class="table-responsive"> <table class="table table-striped table-bordered table-hover"> <thead> <tr> <th><input type="checkbox" name="ck"></th> <th>编号</th> <th>名称</th> <th>首要联系人</th> <th>手机号</th> <th>qq微信/Email</th> <th>地址</th> <th>备注</th> <th>状态</th> </tr> </thead> <tbody> <tr th:each="supplier:${suppliers}"> <td><input type="checkbox" name="ck"></td> <td th:text="${supplier.getId()}"></td> <td th:text="${supplier.getName()}"></td> <!--问号的作用是判断是否为空,为空的话就不会执行会面的语句就不会报错--> <td th:text="${supplier.getLinkman()?.getName()}"></td> <td th:text="${supplier.getLinkman()?.getTel()}"></td> <td th:text="${supplier.getLinkman()?.getEmail()}"></td> <td th:text="${supplier.getLinkman()?.getAddress()}"></td> <td th:text="${supplier.getRemark()}"></td> <td> <div class="onoffswitch"> <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" th:id="${supplier.getId()}" th:checked="${supplier.getOnOff()}" > <label class="onoffswitch-label" th:for="${supplier.getId()}"> <span class="onoffswitch-inner"></span> <span class="onoffswitch-switch"></span> </label> </div></td> </tr> </tbody> </table> </div> </div> <!-- /#wrapper --> <!-- jQuery --> <script th:src="@{/vendor/jquery/jquery.min.js}"></script> <!-- Bootstrap Core JavaScript --> <script th:src="@{/vendor/bootstrap/js/bootstrap.min.js}"></script> <!-- Metis Menu Plugin JavaScript --> <script th:src="@{/vendor/metisMenu/metisMenu.min.js}"></script> <!-- Morris Charts JavaScript --> <script th:src="@{/vendor/raphael/raphael.min.js}"></script> <script th:src="@{/vendor/morrisjs/morris.min.js}"></script> <!--<script src="../data/morris-data.js"></script>--> <!-- Custom Theme JavaScript --> <script th:src="@{/dist/js/sb-admin-2.js}"></script> </body> </html>
显示:
可以看出页面分为上左右三个部分
1、上部标题栏包含产品的名称以及右边的图标点击可以出现下拉列表栏可以选择退出和修改账户
2、左部导航栏点击进入相应的内容
3、右部内容区域显示信息
供应商的信息我通过表格的方式呈现用th:each将后台传过来的suppliers对象遍历一下有点类似于jstl中的<c:forEach>标签。然后用th:text进行文本展示get一下供应商各个属性的值,这里面有些地方加了问号非常非常重要,这个地方卡了我很久到后面会说
另外状态那栏里的开关按钮我用的是一位大神写好的并稍微改动了一些大小和样式,具体的css代码我就不放了
开关的html代码:
<div class="onoffswitch"> <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" th:id="${supplier.getId()}" th:checked="${supplier.getOnOff()}" > <label class="onoffswitch-label" th:for="${supplier.getId()}"> <span class="onoffswitch-inner"></span> <span class="onoffswitch-switch"></span> </label> </div>
这里面用label标签的for属性绑定input的id,原文是一个固定的id,但是经过for:each遍历之后就会出错,导致不管你按哪个按钮只有第一行的动,所以我替换成了一个动态的id,用supplier.getId()得到每一行的供应商id,是变化的,就不会出错了。
原文链接
https://www.geek-share.com/detail/2700713309.html
添加删除我是用的bootstrap的模态框来进行表单提交
三、后台
首先在控制台下用mybatis反向生成代码(实体类、dao层、映射文件)
1、在网上下好mybatis-generator-core-1.3.7文件
打开里面是这样的
2、修改generatorConfig.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包--> <classPathEntry location="D:\git\Git\mybatis-generator-core-1.3.7\lib\mysql-connector-java-8.0.12.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://192.168.47.131/inventory?useSSL=false&nullCatalogMeansCurrent=true" userId="root" password="root123"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.power.inventory.inventory.model" targetProject="model"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="com.power.inventory.inventory.mapping" targetProject="mapping"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.power.inventory.inventory.dao" targetProject="dao"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="SYS_LINKMAN" domainObjectName="Linkman" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
具体的修改上面都有注解,我就不详细说了
3、在控制台下用cd命令进入文件所在位置
输入:java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
如果控制台上出现:MyBatis Generator finished successfully.就表明成功了
查看一下文件的目录
dao mapping model都是生成的controller service是自己加的
resource下用来放一些静态资源
static下放一些css js代码
templates下放静态页面
看一下生成的文件
supplier.java
package com.power.inventory.inventory.modules.supplier.model; import com.power.inventory.inventory.modules.Linkman.model.Linkman; public class supplier { private Long id; private String name; private String remark; private Boolean onOff; private Linkman linkman; //这是我自己加的 public Linkman getLinkman() { return linkman; } public void setLinkman(Linkman linkman) { this.linkman = linkman; } 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 == null ? null : name.trim(); } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark == null ? null : remark.trim(); } public Boolean getOnOff() { return onOff; } public void setOnOff(Boolean onOff) { this.onOff = onOff; } }
model存放着supplier的实体类相当于javabean,与数据库中的属性值基本保持一致。这里面我加入了Linkman的实体类,与supplier类联系,联表查询时用。
supplierMapper.java
package com.power.inventory.inventory.modules.supplier.dao; import java.util.List; import com.power.inventory.inventory.modules.supplier.model.supplier; public interface supplierMapper { int deleteByPrimaryKey(Long id); int insert(supplier record); int insertSelective(supplier record); supplier selectByPrimaryKey(Long id); int updateByPrimaryKeySelective(supplier record); int updateByPrimaryKey(supplier record); //这个方式我自己加的 List<supplier> selectAllSupplier(boolean b); }
dao层是对数据库进行数据持久化操作,他的方法语句是直接针对数据库操作的,dao层只是个接口,里面也只有方法名字和参数,具体的实现还是在Mapper.xml里的,方法的名字要与xml里的select元素的id属性保持一致
supplierMapper.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.power.inventory.inventory.modules.supplier.dao.supplierMapper"> <resultMap id="BaseResultMap" type="com.power.inventory.inventory.modules.supplier.model.supplier"> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="remark" jdbcType="VARCHAR" property="remark" /> <result column="on_off" jdbcType="BIT" property="onOff" /> </resultMap> <resultMap id="QueryResultMap" type="com.power.inventory.inventory.modules.supplier.model.supplier"> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="remark" jdbcType="VARCHAR" property="remark" /> <result column="on_off" jdbcType="BIT" property="onOff" /> <collection property="linkman" ofType="com.power.inventory.inventory.modules.Linkman.model.Linkman"> <id column="l_id" jdbcType="BIGINT" property="id" /> <result column="l_name" jdbcType="VARCHAR" property="name" /> <result column="tel" jdbcType="BIGINT" property="tel" /> <result column="email" jdbcType="VARCHAR" property="email" /> <result column="address" jdbcType="VARCHAR" property="address" /> <result column="main_linkman" jdbcType="BIT" property="mainLinkman" /> <result column="sys_supplier_id" jdbcType="BIGINT" property="sysSupplierId" /> </collection> </resultMap> <sql id="Base_Column_List"> id, name, remark, on_off </sql> <select id="selectAllSupplier" resultMap="QueryResultMap"> select SYS_SUPPLIER.*,SYS_LINKMAN.id as l_id,SYS_LINKMAN.name as l_name,SYS_LINKMAN.tel,SYS_LINKMAN.email,SYS_LINKMAN.address from SYS_SUPPLIER left join SYS_LINKMAN on SYS_SUPPLIER.id=SYS_LINKMAN.sys_supplier_id and SYS_LINKMAN.main_linkman=#{mainLinkman,jdbcType=BIT}; </select> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from SYS_SUPPLIER where id = #{id,jdbcType=BIGINT} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> delete from SYS_SUPPLIER where id = #{id,jdbcType=BIGINT} </delete> <insert id="insert" parameterType="com.power.inventory.inventory.modules.supplier.model.supplier"> insert into SYS_SUPPLIER (id, name, remark, on_off) values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{onOff,jdbcType=BIT}) </insert> <insert id="insertSelective" parameterType="com.power.inventory.inventory.modules.supplier.model.supplier"> insert into SYS_SUPPLIER <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="name != null"> name, </if> <if test="remark != null"> remark, </if> <if test="onOff != null"> on_off, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="name != null"> #{name,jdbcType=VARCHAR}, </if> <if test="remark != null"> #{remark,jdbcType=VARCHAR}, </if> <if test="onOff != null"> #{onOff,jdbcType=BIT}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.power.inventory.inventory.modules.supplier.model.supplier"> update SYS_SUPPLIER <set> <if test="name != null"> name = #{name,jdbcType=VARCHAR}, </if> <if test="remark != null"> remark = #{remark,jdbcType=VARCHAR}, </if> <if test="onOff != null"> on_off = #{onOff,jdbcType=BIT}, </if> </set> where id = #{id,jdbcType=BIGINT} </update> <update id="updateByPrimaryKey" parameterType="com.power.inventory.inventory.modules.supplier.model.supplier"> update SYS_SUPPLIER set name = #{name,jdbcType=VARCHAR}, remark = #{remark,jdbcType=VARCHAR}, on_off = #{onOff,jdbcType=BIT} where id = #{id,jdbcType=BIGINT} </update> </mapper>
这里我用到了联表查询,在mybatis中联表查询用collection标签,property属性是在model文件里定义的另一个表实体类对象的名字,ofType是model文件所在位置,如果两个表中有相同的字段(column)时,尽量区别一下写t_id,t_name等等,sql语句写成xxx as t_id。这里链表查询用的是左连接(供应商只有四个字段,其余的属性都是首要联系人的,大家看sql语句就能看明白了),为的是查询出所有供应商的信息和与供应商有联系的联系人的信息,因为界面里只有添加了供应商才能添加联系人,所以当只添加了供应商没有添加联系人时,查询会将联系人的信息设置为null,就像这样:
mysql> select SYS_SUPPLIER.*,SYS_LINKMAN.id as l_id,SYS_LINKMAN.name as l_name,SYS_LINKMAN.tel,SYS_LINKMAN.email,SYS_LINKMAN.address -> from SYS_SUPPLIER left join SYS_LINKMAN -> on SYS_SUPPLIER.id=SYS_LINKMAN.sys_supplier_id -> and SYS_LINKMAN.main_linkman=1; +----+--------+--------+--------+------+-----------+-------------+------------------+---------+ | id | name | remark | on_off | l_id | l_name | tel | email | address | +----+--------+--------+--------+------+-----------+-------------+------------------+---------+ | 1 | 淘宝 | 电商 | 1 | 1 | 马云 | 15541326515 | 741982655@qq.com | beijing | | 2 | 腾讯 | 游戏 | 0 | 2 | 马化腾 | 13591571570 | 851885218@qq.com | beijing | | 3 | csgo | 开箱 | 1 | NULL | NULL | NULL | NULL | NULL | +----+--------+--------+--------+------+-----------+-------------+------------------+---------+ 3 rows in set (0.00 sec)
还有上面我说过这里一定要加问号?:
<td th:text="${supplier.getLinkman()?.getName()}"></td> <td th:text="${supplier.getLinkman()?.getTel()}"></td> <td th:text="${supplier.getLinkman()?.getEmail()}"></td> <td th:text="${supplier.getLinkman()?.getAddress()}"></td>
因为没有加联系人的话,值为null,这里?的作用就是判断getLinkman()值是否为空,如果为空就不会执行后面的get了,这里如果不写?,项目不会报错,但是打开页面的使用有些值会无法正成显示
加上Service层和Controller层
SupplierService.java
package com.power.inventory.inventory.modules.supplier.service; import java.util.List; import com.power.inventory.inventory.modules.supplier.model.supplier; /* 供应商Service层接口 */ public interface SupplierService { /* 获取所有供应商的信息 */ List<supplier> findAllSupplier(boolean b); int addSupplier(supplier supp); int deleteByPrimaryKey(Long id); }
SupplierServiceImpl
package com.power.inventory.inventory.modules.supplier.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.power.inventory.inventory.modules.supplier.model.supplier; import com.power.inventory.inventory.modules.supplier.dao.supplierMapper; import java.util.List; @Service public class SupplierServiceImpl implements SupplierService { /* 供应商Service层接口的实现类 */ @Autowired private supplierMapper suppliermapper; @Override public List<supplier> findAllSupplier(boolean b) { return suppliermapper.selectAllSupplier(b); } @Override public int addSupplier(supplier supp){ return suppliermapper.insert(supp); } @Override public int deleteByPrimaryKey(Long id){ return suppliermapper.deleteByPrimaryKey(id); } }
SupplierController.java
package com.power.inventory.inventory.modules.supplier.controller; import com.alibaba.fastjson.JSON; import com.power.inventory.inventory.modules.supplier.model.supplier; import com.power.inventory.inventory.modules.supplier.service.SupplierService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.util.List; @Controller public class SupplierController { @Autowired private SupplierService supplierService; @Autowired HttpServletRequest request; @RequestMapping(value = "/supplierManager" , produces = {"application/json;charset=UTF-8"}) public String findAllsupplier(Model model) { boolean b=true; List<supplier> suppliers=supplierService.findAllSupplier(b); // String supp= JSON.toJSONString(suppliers); // return supp; model.addAttribute("suppliers",suppliers); return "supplierManager"; } @PostMapping(value = "/addSupplier", produces = {"application/json;charset=UTF-8"}) public String addSupplier(){ supplier supp=new supplier(); // 添加供应商编号 String id=request.getParameter("supplierId"); if(id== null ||"".equals(id)) { id = "0"; } long supplierId=Long.valueOf(id); supp.setId(supplierId); // 添加供应商姓名 String supplierName=request.getParameter("supplierName"); if(supplierName== null ||"".equals(supplierName)) { supplierName = "0"; } supp.setName(supplierName); // 添加供应商备注 String supplierRemark=request.getParameter("supplierRemark"); if(supplierRemark== null ||"".equals(supplierRemark)) { supplierRemark = "0"; } supp.setRemark(supplierRemark); String supplierOnOff=request.getParameter("supplierOnOff"); if(supplierOnOff== null ||"".equals(supplierOnOff)) { supplierOnOff = "false"; } Boolean onOff=Boolean.parseBoolean(supplierOnOff); supp.setOnOff(onOff); supplierService.addSupplier(supp); return "redirect:/supplierManager"; } @PostMapping(value = "/deleteSupplier", produces = {"application/json;charset=UTF-8"}) public String deleteByPrimaryKey(){ String SupplierId=request.getParameter("SupplierId"); if(SupplierId== null ||"".equals(SupplierId)) { SupplierId = "0"; } long suppId=Long.valueOf(SupplierId); supplierService.deleteByPrimaryKey(suppId); return "redirect:/supplierManager"; } }
这我用了最笨的方法,防止前台传过来的值为空,判断一下是否为空。为空就都设为“0”。
第一次写文章,也是第一次做项目,挺锻炼人的,全篇我都是用来大白话,用我自己的理解来写的,可能有些地方不对,希望大家帮我指出来,谢谢观看。
- 点赞 2
- 收藏
- 分享
- 文章举报
- SpringBoot整合MyBatis及Thymeleaf
- thymeleaf :springboot整合mybatis的web项目中使用html而不是jsp当页面渲染
- 【个人学习】使用idea搭建SpringBoot,整合Mybatis、Thymeleaf,连接数据库,实现具有前端界面项目:主要功能登录,注册,个人信息查看、更改,不定时更新中...
- spingBoot myBatis neo4j整合项目案例
- SpringBoot_thymeleaf_mybatis和PageHelper分页插件整合
- IDEA整合springboot+Thymeleaf+Mybatis
- BabyLog SpringBoot+MyBatis+Thymeleaf+Amaze UI 整合学习
- 使用myeclipse搭建SpringBoot框架,整合thymeleaf模板和mybatis
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- SpingBoot(四)——整合Mybatis(druid连接池)
- SpringBoot2.0完美整合Mybatis并使用thymeleaf展示数据
- springboot2.0整合shiro+thymeleaf+mybatis [eclipse]
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- springboot、mybatis、thymeleaf、bootstrap整合框架
- BabyLog SpringBoot+MyBatis+Thymeleaf+Amaze UI 整合学习
- 【企业级框架整合】Springmvc+mybatis+restful+bootstrap框架整合
- SpringBoot整合Mybatis多数据源 (AOP+注解)
- Springboot整合mybatis
- SpringBoot 整合Mybatis3.x注解开发
- spring boot 学习(二)spring boot 框架整合 thymeleaf