以MyBatis+SpringMVC3.0实现的,借鉴了Hibernate设计思想,采用封装、抽象、继承的设计思想,做到了数据与相应的操作的高内聚低耦合的实现

QQ:1138789752

Email.java类:

package com.lmc.ink.entity.seo;
import com.lmc.ink.entity.IEntity;
/**
 * 推广邮箱表(s_email)实体类
 * @author lmc
 * */
public class Email extends IEntity {
	/**
	 * 唯一标识
	 * */
	public static final long serialVersionUID = 537132705L;
	/**
	 * 主键
	 * 长度20位
	 * 不为空
	 * 备注:自增长
	 * */
	private Long id;
	/**
	 * 邮箱账户
	 * 长度40位
	 * 不为空
	 * 备注:唯一约束
	 * */
	private String accout;
	/**
	 * 推广状态
	 * 长度1位
	 * 不为空
	 * 备注:有效|无效|已用(1|2|3),默认为1
	 * */
	private Integer state;
	/**
	 * 构造方法
	 * */
	public Email() {
		super("s_email");
	}
	/**
	 * 获取主键
	 * 长度20位
	 * 不为空
	 * 备注:自增长
	 * @return Long 主键
	 * */
	public Long getId() {
		return id;
	}
	/**
	 * 设置主键
	 * 长度20位
	 * 不为空
	 * 备注:自增长
	 * @param id 主键
	 * @return String id
	 * */
	public String setId(Long id) {
		if(id!=null){
			this.id = id;
		}
		return "id";
	}
	/**
	 * 获取邮箱账户
	 * 长度40位
	 * 不为空
	 * 备注:唯一约束
	 * @return String 邮箱账户
	 * */
	public String getAccout() {
		return accout;
	}
	/**
	 * 设置邮箱账户
	 * 长度40位
	 * 不为空
	 * 备注:唯一约束
	 * @param accout 邮箱账户
	 * @return String accout
	 * */
	public String setAccout(String accout) {
		if(!super.xCheck(accout)){
			this.accout = accout;
		}
		return "accout";
	}
	/**
	 * 获取推广状态
	 * 长度1位
	 * 不为空
	 * 备注:有效|无效|已用(1|2|3),默认为1
	 * @return Integer 推广状态
	 * */
	public Integer getState() {
		return state;
	}
	/**
	 * 设置推广状态
	 * 长度1位
	 * 不为空
	 * 备注:有效|无效|已用(1|2|3),默认为1
	 * @param state 推广状态
	 * @return String state
	 * */
	public String setState(Integer state) {
		if(state!=null){
			this.state = state;
		}
		return "state";
	}
}


IService.java接口:

package com.lmc.ink.service;
import java.util.List;
import com.lmc.ink.entity.IEntity;
/**
 * 业务逻辑类父接口
 * @author lmc
 * @version 2015-04-08 16:47:41
 * */
public interface IService<T extends IEntity> {
	/**
	 * 调试标志
	 * */
	public static final boolean debug = true;
	/**
	 * 输处调试信息
	 * @param en 实体对象
	 * @param error 错误信息
	 * */
	public void debug(T en, String error);
	/**
	 * 保存记录
	 * @param en 实体对象
	 * @return boolean 操作结果
	 * */
	public boolean save(T en);
	/**
	 * 按主键删除记录
	 * @param en 实体对象
	 * @return boolean 操作结果
	 * */
	public boolean drop(T en);
	/**
	 * 按例删除记录
	 * @param en 实体对象
	 * @return int 操作结果
	 * */
	public int recycle(T en);
	/**
	 * 更新记录(整体)
	 * @param en 实体对象
	 * @return boolean 操作结果
	 * */
	public boolean replace(T en);
	/**
	 * 更新记录(局部)
	 * @param en 实体对象
	 * @return boolean 操作结果
	 * */
	public boolean modify(T en);
	/**
	 * 按键加载记录
	 * @param en 实体对象
	 * @return T 实体对象
	 * */
	public T get(T en);
	/**
	 * 按例查询记录
	 * @param en 实体对象
	 * @return List 对象集合
	 * */
	public List<T> list(T en);
	/**
	 * 按例统计记录
	 * @param en 实体对象
	 * @return long 统计数
	 * */
	public long sum(T en);
}

EmailService接口:

package com.lmc.ink.service.seo;
import com.lmc.ink.service.IService;
import com.lmc.ink.entity.seo.Email;
/**
 * 推广邮箱表(s_email)业务逻辑类接口
 * @author lmc
 * @version 2015-04-18 10:51:05
 * */
public interface EmailService extends IService<Email> {}

EmailService接口实现:

package com.lmc.ink.service.seo.impl;
import java.util.List;
import com.lmc.ink.dao.seo.EmailDao;
import com.lmc.ink.data.Laws;
import com.lmc.ink.entity.seo.Email;
import com.lmc.ink.service.seo.EmailService;
/**
 * 推广邮箱表(s_email)业务逻辑类接口
 * @author lmc
 * @version 2015-04-18 10:51:05
 * */
public class EmailServiceImpl implements EmailService {
	private EmailDao emailDao;
	public void setEmailDao(EmailDao emailDao) {
		this.emailDao = emailDao;
	}
	public void debug(Email en, String error) {
		if(en!=null){
			en.setXerror(error);
		}
		if(debug){
			System.err.println("【"+this.getClass().getName()+"】"+error);
		}
	}
	public boolean save(Email en) {
		try{
			if(en==null){
				this.debug(null,"参数为空");
				return false;
			}
			if(en.getAccout()==null){
				this.debug(en,en.setAccout(null)+"为空");
				return false;
			}
			if(en.getState()!=null){
				if(!Laws.legal(Laws.SEO,en.getState())){
					this.debug(en,en.setState(null)+"非法");
					return false;
				}
			}else{
				en.setState(1);
			}
			return emailDao.insert(en)>0;
		}catch(Exception e){
			this.debug(en,e.getMessage());
			e.printStackTrace();
		}
		return false;
	}
	public boolean drop(Email en) {
		try{
			if(en==null){
				this.debug(null,"参数为空");
				return false;
			}
			if(en.getId()==null){
				this.debug(en,en.setId(null)+"为空");
				return false;
			}
			return emailDao.delete(en)>0;
		}catch(Exception e){
			this.debug(en,e.getMessage());
			e.printStackTrace();
		}
		return false;
	}
	public int recycle(Email en) {
		try{
			if(en==null){
				this.debug(null,"参数为空");
				return 0;
			}
			return emailDao.remove(en);
		}catch(Exception e){
			this.debug(en,e.getMessage());
			e.printStackTrace();
		}
		return 0;
	}
	public boolean replace(Email en) {
		try{
			if(en==null){
				this.debug(null,"参数为空");
				return false;
			}
			if(en.getId()==null){
				this.debug(en,en.setId(null)+"为空");
				return false;
			}
			if(en.getState()==null||!Laws.legal(Laws.SEO,en.getState())){
				this.debug(en,en.setState(null)+"非法");
				return false;
			}
			return emailDao.merge(en)>0;
		}catch(Exception e){
			this.debug(en,e.getMessage());
			e.printStackTrace();
		}
		return false;
	}
	public boolean modify(Email en) {
		try{
			if(en==null){
				this.debug(null,"参数为空");
				return false;
			}
			if(en.getId()==null){
				this.debug(en,en.setId(null)+"为空");
				return false;
			}
			if(en.getState()!=null&&!Laws.legal(Laws.SEO,en.getState())){
				this.debug(en,en.setState(null)+"非法");
				return false;
			}
			return emailDao.update(en)>0;
		}catch(Exception e){
			this.debug(en,e.getMessage());
			e.printStackTrace();
		}
		return false;
	}
	public Email get(Email en) {
		try{
			if(en==null){
				this.debug(null,"参数为空");
				return null;
			}
			if(en.getId()==null){
				this.debug(en,en.setId(null)+"为空");
				return null;
			}
			return emailDao.load(en);
		}catch(Exception e){
			this.debug(en,e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	public List<Email> list(Email en) {
		try{
			if(en==null){
				this.debug(null,"参数为空");
				return null;
			}
			return emailDao.find(en);
		}catch(Exception e){
			this.debug(en,e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	public long sum(Email en) {
		try{
			if(en==null){
				this.debug(null,"参数为空");
				return 0;
			}
			return emailDao.count(en);
		}catch(Exception e){
			this.debug(en,e.getMessage());
			e.printStackTrace();
		}
		return 0;
	}
}

测试代码:

package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lmc.ink.entity.seo.Email;
import com.lmc.ink.service.seo.EmailService;
public class _Email {
	public static void main(String[] args) throws Exception {
		ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmailService service = act.getBean("emailService",EmailService.class);
		//【按例查询与按例统计举例】
		Email en = new Email();
		//设置等价查询参数,以AND连接执行:id=1
		en.setId(1L);
		//设置模糊查询参数,%与_字符匹配,以AND链接执行:accout like '%1138789752@qq.com'
		en.setAccout("%1138789752@qq.com");
		//设置模糊查询参数,%与_字符匹配,以AND链接执行:accout like '%1138789752_'
		en.setAccout("%1138789752_");
		//设置范围查询参数,以AND连接执行:id = 1
		en.setXscope(true,en.setId(null),"=",1L);
		//设置范围查询参数,以AND连接执行:id != 1
		en.setXscope(true,en.setId(null),"!=",1L);
		//设置范围查询参数,以OR连接执行:accout like '%1138789752@qq.com'
		en.setXscope(false,en.setAccout(null),"like","%1138789752@qq.com");
		//设置范围查询参数,以OR连接执行:accout like '%1138789752_'
		en.setXscope(false,en.setAccout(null),"like","%1138789752_");
		//设置范围查询参数,以OR连接执行:id > 2
		en.setXscope(false,en.setId(null),">",2L);
		//设置范围查询参数,以OR连接执行:id >= 2
		en.setXscope(false,en.setId(null),">=",2L);
		//设置范围查询参数,以AND连接执行:id < 2
		en.setXscope(true,en.setId(null),"<",2L);
		//设置范围查询参数,以AND连接执行:id <= 2
		en.setXscope(true,en.setId(null),"<=",2L);
		//设置非空查询参数,以OR连接执行:accout is null
		en.setXsky(false,en.setAccout(null),true);
		//设置in查询参数,以AND连接执行:id in (1,2,3);
		en.setXchain(true,en.setId(null),new Long[]{1L,2L,3L});
		//设置分组字段,执行:group by state,group by id
		en.setXgroup(en.setState(null));
		en.setXgroup(en.setId(null));
		//获取按例查询总记录数
		long total = service.sum(en);
		//设置分页总记录数
		en.setXtotal(total);
		//设置分页总记录数
		en.setXtotal(10000L);
		//设置分页每页条数,此处设置为每页100条
		en.setXsize(100L);
		//设置分页当前页,此处设置取第2页
		en.setXnow(2L);
		//执行案例查询,返回值为List<Email>
		service.list(en);
		
		//【按键加载记录】
		en = new Email();
		en.setId(1L);
		en = service.get(en);
		System.out.println(en==null);
		
		//【添加记录】
		//实例化对象并设值
		en = new Email();
		en.setAccout("1138789752@qq.com");
		en.setState(2);
		//执行添加记录,返回值为boolean
		System.out.println(service.save(en));
		//执行添加记录后获取对象保存到数据库的主键值(主键为自增长时)
		System.err.println(en.getId());
		
		//【删除记录】
		//执行按主键删除,返回值为boolean
		en = new Email();
		en.setId(1L);
		System.out.println(service.drop(en));
		
		//【按例删除记录】
		//此处执行delete from s_email where account = '1138789752@qq.com' and state = 2
		en = new Email();
		en.setAccout("1138789752@qq.com");
		en.setState(2);
		System.out.println(service.recycle(en));
		//此处没有参数,相当于清空数据库表,因此应慎用按例删除记录
		System.out.println(service.recycle(new Email()));
		
		//【更新记录】
		en = new Email();
		en.setId(1L);
		en.setAccout("123456");
		//(局部更新)此处执行:update s_email set accout = '123456' where id = 1
		System.out.println(service.modify(en));
		en.setState(1);
		//(整体更新)此处执行:update s_email set accout = '123456',state = 1 where id = 1
		System.out.println(service.replace(en));
		
		//【输出调试信息】
		//此处被Service类内部调用
		service.debug(en,"错误信息");
		
		//以上以MyBatis+SpringMVC3.0实现的,借鉴了Hibernate设计思想,
		//采用封装、抽象、继承的设计思想,做到了数据与相应的操作的高内聚低耦合的实现
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值