最近忙于对H5页面的理解,工作大都是前端移动端的工作,虽然技能熟练和代码理解深刻了,但对于目前的状态来说,自我提升的时间太少,公司是主要开发java web的项目,所以不得不对java框架的增删改查做到熟练使用,起码不用等到后台java大神给到我接口数据,自己能理解原理,自己去数据库拿到数据,开发起来更方便些。
再来前端工程师掌握一门后台语言也没啥不好的,博主之前自己也是用php进行学习的,只能说面向对象的语言都大同小异吧,以下只是我这个java菜鸟对spring简单使用的理解,大家看看就好,当做学习记录保存。
首先可以先了解一下框架的基本目录结构(结构有很多种,此处以我的项目做参考)
这里是在已经配置好的框架上进行学习操作的
简单来说,前台连接数据库的原理。
通过建立实体类器调用对,控制应的service方法,service调用mapper层的接口方法,并且在xml上写好sql语句,通过相关配置完成操作。
这一套流程顺序因人而异,看个人的写法,对于新手的我来说,其实都差不多,仅供自己参考学习记录。
1.建立entity实体类
在已经连接好的数据库上进行操作,此处是对供应商表进行连接操作。
数据表
实体类,对应的是数据库的字段内容,补充getter和setter方法
package com.oil.admin.entity;
public class OilSupplier extends Base{
//供油商ID
private String supplierId;
//供油商名称
private String supplierName;
//联系人姓名
private String contactName;
//联系人手机号
private String mobile;
//备注
private String remark;
//创建时间(筛选条件)
private String beginDate;
//更新时间(筛选条件)
private String endDate;
public String getSupplierId() {
return supplierId;
}
public void setSupplierId(String supplierId) {
this.supplierId = supplierId;
}
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getBeginDate() {
return beginDate;
}
public void setBeginDate(String beginDate) {
this.beginDate = beginDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
}
2.在service层新增一个interface接口,用来得到列表,定义一些有List返回的抽象方法,抛出异常,标准增删改查
package com.oil.admin.service;
import java.util.List;
import com.oil.admin.entity.OilSupplier;
public interface OilSupplierService{
/**
* 获取油卡供应商列表
* @param oilSupplier
* @return
* @throws exception
*/
List getOilSupplierList(OilSupplier oilSupplier) throws Exception;
/**
* 根据Id获取供应商
* @param supplierId
* @return
* @throws exception
*/
OilSupplier getOilSupplierById(String supplierId) throws Exception;
/**
* 增加供应商
* @param oilSupplier
* @throws exception
*/
void addOilSupplier(OilSupplier oilSupplier) throws Exception;
/**
* 删除供应商
* @param oilSupplier
* @throws exception
*/
void deleteOilSupplier(String supplierId) throws Exception;
/**
* 编辑供应商
* @param oilSupplier
* @throws exception
*/
void updateOilSupplier(OilSupplier oilSupplier) throws Exception;
}
3.在impl接口继承层,新建一个.java class接口关联于以上接口,并自动注入mapper层的map,重写接口的静态方法。这里注意mapper层没写的话,会找不到路径,报错;
package com.oil.admin.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.oil.admin.entity.OilSupplier;
import com.oil.admin.mapper.OilSupplierMapper;
import com.oil.admin.service.OilSupplierService;
@Service
public class OilSupplierServiceImpl implements OilSupplierService{
@Autowired
private OilSupplierMapper oilSupplierMapper;
/**
* 获取油卡供应商列表
* @param oilSupplier
* @return
* @throws exception
*/
@Override
public List getOilSupplierList(OilSupplier oilSupplier) throws Exception{
return oilSupplierMapper.getOilSupplierList(oilSupplier);
}
/**
* 根据Id获取供应商
* @param supplierId
* @return
* @throws exception
*/
@Override
public OilSupplier getOilSupplierById(String supplierId) throws Exception{
return oilSupplierMapper.getOilSupplierById(supplierId);
}
/**
* 增加供应商
* @param oilSupplier
* @throws exception
*/
@Override
public void addOilSupplier(OilSupplier oilSupplier) throws Exception{
oilSupplierMapper.addOilSupplier(oilSupplier);
}
/**
* 删除供应商
* @param oilSupplier
* @throws exception
*/
@Override
public void deleteOilSupplier(String supplierId) throws Exception{
oilSupplierMapper.deleteOilSupplier(supplierId);
}
/**
* 编辑供应商
* @param oilSupplier
* @throws exception
*/
@Override
public void updateOilSupplier(OilSupplier oilSupplier) throws Exception{
oilSupplierMapper.updateOilSupplier(oilSupplier);
}
}
4.构造mapper层的接口静态方法,可以发现和service的方法类似,只是少了抛出异常throws Excpetion;
package com.oil.admin.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.oil.admin.entity.OilSupplier;
@Repository
public interface OilSupplierMapper{
/**
* 获取油卡供应商列表
* @param oilSupplier
* @return
* @throws exception
*/
List getOilSupplierList(OilSupplier oilSupplier);
/**
* 根据Id获取供应商
* @param supplierId
* @return
* @throws exception
*/
OilSupplier getOilSupplierById(String supplierId);
/**
* 增加供应商
* @param oilSupplier
* @throws exception
*/
void addOilSupplier(OilSupplier oilSupplier);
/**
* 删除供应商
* @param oilSupplier
* @throws exception
*/
void deleteOilSupplier(String supplierId);
/**
* 编辑供应商
* @param oilSupplier
* @throws exception
*/
void updateOilSupplier(OilSupplier oilSupplier);
}
5.在mapper层的xml文件中,开始写对应的映射和sql语句,完成和数据库交互的操作。
<mapper namespace="com.oil.admin.mapper.OilSupplierMapper">
<resultMap id="oilSupplierMap" type="com.oil.admin.entity.OilSupplier">
<result column="supplier_name" property="supplierName"/>
<result column="contact_name" property="contactName"/>
<result column="contact_mobile" property="mobile"/>
<result column="remark" property="remark"/>
<result column="created_date" property="createdDate"/>
<result column="updated_date" property="updatedDate"/>
resultMap>
<select id="getOilSupplierList" parameterType="com.oil.admin.entity.OilSupplier" resultMap="oilSupplierMap">
select supplier_id,supplier_name,contact_name,contact_mobile,created_date
from oil_supplier where valid_flag = 'Y'
<if test="supplierName != null and supplierName != ''">
and supplier_name like CONCAT('%',#{supplierName},'%')
if>
<if test="beginDate != null and beginDate != ''">
= DATE_FORMAT(CONCAT(#{beginDate},' 00:00:00'),'%Y-%m-%d %H:%i:%s')
]]>
if>
<if test="endDate != null and endDate != ''">
if>
order by created_date desc
select>
<select id="getOilSupplierById" parameterType="string" resultMap="oilSupplierMap">
select supplier_id,supplier_name,contact_name,contact_mobile,remark
from oil_supplier where valid_flag = 'Y' and supplier_id = #{supplierId}
select>
<insert id="addOilSupplier" parameterType="com.oil.admin.entity.OilSupplier">
insert into oil_supplier (supplier_name, contact_name, contact_mobile, remark, created_date)
values(#{supplierName},#{contactName},#{mobile},#{remark},now())
insert>
<update id="updateOilSupplier" parameterType="com.oil.admin.entity.OilSupplier" >
update oil_supplier
set supplier_name = #{supplierName},contact_name = #{contactName},contact_mobile = #{mobile},remark = #{remark},updated_date = now()
where valid_flag = 'Y' and supplier_id = #{supplierId}
update>
<update id="deleteOilSupplier" parameterType="String" >
update oil_supplier set valid_flag = 'N' where supplier_id = #{supplierId}
update>
mapper>
6.最后我们需要在controller层,完成controller控制器的编写,才能调用出这三层的方法数据。
将Service@Autowired进入控制器,通过注入service进行交集
package com.oil.admin.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.reflect.generics.tree.Tree;
import com.oil.admin.component.Response;
import com.oil.admin.entity.OilSupplier;
import com.oil.admin.service.OilSupplierService;
@Controller
@RequestMapping("/oilCard")
public class OilSupplierController extends CommonController{
@Autowired
private OilSupplierService oilSupplierService;
/**
* 供应商列表页面
* @return
*/
@RequestMapping("/supplierListPage")
public String oilSupplierPage(){
return "/supplier/oilSupplierList";
}
/**
* 供应商列表数据
* @param oilSupplier
* @return
*/
@ResponseBody
@RequestMapping("/supplierList")
public Response getSupplierList(OilSupplier oilSupplier){
List list;
try{
list = oilSupplierService.getOilSupplierList(oilSupplier);
}catch(Exception e){
logger.error("获取供油商列表出错!",e);
return Response.fail();
}
return Response.success(list);
}
/**
* 删除选中供应商
* @param supplierId
* @return
*/
@ResponseBody
@RequestMapping("/deleteOilSupplier")
public Response deleteOilSupplier(String supplierId){
try {
oilSupplierService.deleteOilSupplier(supplierId);
} catch (Exception e) {
logger.error("删除供应商出错!",e);
return Response.fail();
}
return Response.success();
}
/**
* 供应商编辑页面
* @return
*/
@RequestMapping("/updateSupplierPage")
public String updateSupplierPage(Model model,String supplierId){
try {
OilSupplier osEdit = oilSupplierService.getOilSupplierById(supplierId);
model.addAttribute("oilSupplier", osEdit);
} catch (Exception e) {
logger.error("供应商编辑页面加载出错!",e);
}
return "/supplier/updateSupplier";
}
/**
* 编辑供应商
* @param oilSuplier
* @return
*/
@ResponseBody
@RequestMapping("/updateOilSupplier")
public Response updateOilSupplier(OilSupplier oilSuplier){
try {
oilSupplierService.updateOilSupplier(oilSuplier);
} catch (Exception e) {
logger.error("编辑供应商出错!",e);
return Response.fail();
}
return Response.success();
}
/**
* 供应商新增页面
* @return
*/
@RequestMapping("/addSupplierPage")
public String addSupplierPage(){
return "/supplier/addSupplier";
}
/**
* 新增供应商
* @param oilSuplier
* @return
*/
@ResponseBody
@RequestMapping("/addOilSupplier")
public Response addOilSupplier(OilSupplier oilSuplier){
try {
oilSupplierService.addOilSupplier(oilSuplier);
} catch (Exception e) {
logger.error("新增供应商出错!",e);
return Response.fail();
}
return Response.success();
}
}
这里使用的ajax返回前端的数据遍历操作,使用EL表达式遍历也是可以的,结合项目需要。
最后无错误的话,输入地址localhost/项目名/映射/映射地址
可以显示页面。
完成以上的操作,其实花了很多时间去理解,许多代码也是尽量手动敲的,尽量不去看同事写的,磕磕碰碰,总算完成了数据的展示,其实我想框架的搭建和缓存等其他业务,才是比较难的,对于偏爱前端的我来说,理解java的使用,和相关javase基础,可能比较适合吧。。。
所以此博文仅供大家参考学习~~~~太浅显了