我是靠谱客的博主 奋斗自行车,最近开发中收集的这篇文章主要介绍【Java Web】【7】SSM spring+struts+mybatis整合(四)SSM中action,dao,service,serviceimpl,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
1. POJO,也就是bean或者model,你们怎么称乎都可以
package com.shdl.hterp.beans;
import org.apache.ibatis.type.Alias;
import com.shdl.hterp.common.Page;
@Alias("companyBean")
public class CompanyBean extends Page {
private String companyid; // 主键ID
private String companyname; // 公司名称
private String companycode; // 纳税人识别号
private String companyaddress; // 企业经营地址
private String companyaccount; // 开户行以及账号
private String companytel; // 联系电话
private String companyremark; // 备注
private String companytype; // 公司类型
private String companyorderno; // 排序字段
private String deletetime; // 删除时间
public String getCompanyid() {
return companyid;
}
public void setCompanyid(String companyid) {
this.companyid = companyid;
}
public String getCompanyname() {
return companyname;
}
public void setCompanyname(String companyname) {
this.companyname = companyname;
}
public String getCompanycode() {
return companycode;
}
public void setCompanycode(String companycode) {
this.companycode = companycode;
}
public String getCompanyaddress() {
return companyaddress;
}
public void setCompanyaddress(String companyaddress) {
this.companyaddress = companyaddress;
}
public String getCompanyaccount() {
return companyaccount;
}
public void setCompanyaccount(String companyaccount) {
this.companyaccount = companyaccount;
}
public String getCompanytel() {
return companytel;
}
public void setCompanytel(String companytel) {
this.companytel = companytel;
}
public String getCompanyremark() {
return companyremark;
}
public void setCompanyremark(String companyremark) {
this.companyremark = companyremark;
}
public String getCompanytype() {
return companytype;
}
public void setCompanytype(String companytype) {
this.companytype = companytype;
}
public String getCompanyorderno() {
return companyorderno;
}
public void setCompanyorderno(String companyorderno) {
this.companyorderno = companyorderno;
}
public String getDeletetime() {
return deletetime;
}
public void setDeletetime(String deletetime) {
this.deletetime = deletetime;
}
}
2. action
package com.shdl.hterp.business.company;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.shdl.hterp.beans.CarBean;
import com.shdl.hterp.beans.CompanyBean;
import com.shdl.hterp.beans.Field;
import com.shdl.hterp.beans.SupplierBean;
import com.shdl.hterp.business.base.BaseAction;
import com.shdl.hterp.common.Result;
import com.shdl.hterp.common.ReturnCode;
import com.shdl.hterp.utils.ResponseUtil;
/**
* 【公司信息管理】业务Action
* @author liangayang
*
*/
public class CompanyAction extends BaseAction {
private static final long serialVersionUID = 1L;
/** properties define **/
private CompanyService companyService;
private CompanyBean bean;
private File update;
private String updateContentType;
private String updateFileName;
private String test;
public File getUpdate() {
return update;
}
public void setUpdate(File update) {
this.update = update;
}
public String getUpdateContentType() {
return updateContentType;
}
public void setUpdateContentType(String updateContentType) {
this.updateContentType = updateContentType;
}
public String getUpdateFileName() {
return updateFileName;
}
public void setUpdateFileName(String updateFileName) {
this.updateFileName = updateFileName;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
public CompanyService getCompanyService() {
return companyService;
}
public void setCompanyService(CompanyService companyService) {
this.companyService = companyService;
}
public CompanyBean getBean() {
return bean;
}
public void setBean(CompanyBean bean) {
this.bean = bean;
}
/**
* 所有数据
*/
public void all(){
Result result = new Result();
try {
List<CompanyBean> list = companyService.infoByField(null, null);
result.setRows(list);
result.setTotal(list.size());
} catch (Exception e) {
e.printStackTrace();
log.error("[CompanyAction.all] 查询所有数据出错", e);
result.fail();
}
ResponseUtil.sendResult(result.toJSONString());
}
/**
* 列表
*/
public void list(){
Result result = new Result();
try {
if (null == bean) bean = new CompanyBean();
if (qm == null) qm = new HashMap<String, Object>();
bean.setQm(qm);
bean.setCurrentPage(page);
bean.setLimit(rows);
bean.setPage();
List<CompanyBean> list = companyService.getList(bean);
result.setRows(list);
result.setTotal(companyService.getListCount(bean));
} catch (Exception e) {
e.printStackTrace();
log.error("[CompanyAction.list] 查询列表出错", e);
result.fail();
}
ResponseUtil.sendResult(result.toJSONString());
}
/**
* 新增
*/
public void add() {
Result result = new Result();
try {
// 校验纳税人识别号号唯一性
List<CompanyBean> list = companyService.infoByField("companyname", bean.getCompanyname().trim());
if (0 != list.size()) {
result.setRe(ReturnCode.R__2, "公司已存在");
ResponseUtil.sendResult(result);
return;
}
/**
* 随机生成公司id
*/
/*bean.setCompanyid(ID.createUID());*/
companyService.add(bean);
}catch (Exception e) {
e.printStackTrace();
result.fail();
log.error("[CarAction.add] 新增出错", e);
}
ResponseUtil.sendResult(result.toJSONString());
}
/**
* 编辑
*/
public void mod() {
Result result = new Result();
try {
// 校验公司名称唯一性
List<CompanyBean> list = companyService.infoByField("companyname", bean.getCompanyname().trim());
if(0 != list.size()){
CompanyBean cb = list.get(0);
if (!cb.getCompanyid().equals(bean.getCompanyid())) {
result.setRe(ReturnCode.R__2, "公司已存在");
ResponseUtil.sendResult(result);
return;
}
}
companyService.modify(bean);
}catch (Exception e) {
e.printStackTrace();
result.fail();
log.error("[CompanyAction.mod] 编辑出错", e);
}
ResponseUtil.sendResult(result.toJSONString());
}
/**
* 详情
*/
public void info(){
Result result = new Result();
try {
result.data.put("info", companyService.info(bean.getCompanyid()));
} catch (Exception e) {
e.printStackTrace();
result.fail();
log.error("[CompanyAction.info] 公司查询详情出错", e);
}
ResponseUtil.sendResult(result.toJSONString());
}
/**
* 删除
*/
public void delete() {
Result result = new Result();
try {
List<Field> updatelist = new ArrayList<Field>();
updatelist.add(new Field("deletetime", "sysdate"));
List<Field> wherelist = new ArrayList<Field>();
wherelist.add(new Field("companyid", bean.getCompanyid()));
companyService.updateByField(updatelist, wherelist);
}catch (Exception e) {
e.printStackTrace();
result.fail();
log.error("[CompanyAction.delete] 删除出错", e);
}
ResponseUtil.sendResult(result.toJSONString());
}
/**
* 查询项目公司
*/
public void getProduct(){
Result result = new Result();
try {
List<CompanyBean> list = companyService.getProduct();
result.setRows(list);
} catch (Exception e) {
e.printStackTrace();
result.fail();
}
ResponseUtil.sendResult(result.toJSONString());
}
}
PS:查询数据库的时候:companyService.getXXXXX,中的get方法与mybatis-company.xml的标签id都要一一对应
3. Service
package com.shdl.hterp.business.company;
import java.util.List;
import com.shdl.hterp.beans.CompanyBean;
import com.shdl.hterp.business.base.BaseService;
/**
* 【车辆基本信息维护】业务接口
* @author CodeMaker
* @createTime 2017-6-22 15:18:31
*/
public interface CompanyService extends BaseService<CompanyBean> {
/**
* 查询查询项目公司
* @return
* @throws Exception
*/
public List<CompanyBean> getProduct() throws Exception;
}
4. ServiceImpl
package com.shdl.hterp.business.company;
import java.util.List;
import com.shdl.hterp.beans.CompanyBean;
import com.shdl.hterp.business.base.BaseDao;
import com.shdl.hterp.business.base.BaseServiceImpl;
/**
* 【车辆基本信息维护】业务实现类
* @author CodeMaker
* @createTime 2017-6-22 15:18:31
*/
public class CompanyServiceImpl extends BaseServiceImpl<CompanyBean> implements CompanyService{
// 属性定义
private CompanyDao companyDao;
public CompanyDao getCompanyDao() {
return companyDao;
}
public void setCompanyDao(CompanyDao companyDao) {
this.companyDao = companyDao;
}
@Override
public BaseDao<CompanyBean> getBaseDao() {
return companyDao;
}
@Override
public List<CompanyBean> getProduct() throws Exception {
return companyDao.getProduct();
}
}
5. Dao
package com.shdl.hterp.business.company;
import java.util.List;
import com.shdl.hterp.beans.CompanyBean;
import com.shdl.hterp.business.base.BaseDao;
public interface CompanyDao extends BaseDao<CompanyBean>{
/**
* 查询项目公司
* @return
* @throws Exception
*/
public List<CompanyBean> getProduct() throws Exception;
}
PS:所有的action,dao,service,serviceImpl都是继承自公共Base方法,其实也不用写公共方法,直接写也可以,后续项目多的话,就不用一个一个写,继承也可以
最后
以上就是奋斗自行车为你收集整理的【Java Web】【7】SSM spring+struts+mybatis整合(四)SSM中action,dao,service,serviceimpl的全部内容,希望文章能够帮你解决【Java Web】【7】SSM spring+struts+mybatis整合(四)SSM中action,dao,service,serviceimpl所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复