以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 '%[email protected]'
		en.setAccout("%[email protected]");
		//设置模糊查询参数,%与_字符匹配,以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 '%[email protected]'
		en.setXscope(false,en.setAccout(null),"like","%[email protected]");
		//设置范围查询参数,以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("[email protected]");
		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 = '[email protected]' and state = 2
		en = new Email();
		en.setAccout("[email protected]");
		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设计思想,
		//采用封装、抽象、继承的设计思想,做到了数据与相应的操作的高内聚低耦合的实现
	}
}
时间: 2024-08-05 03:25:50

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

MyBatis3.2.2+SpringMVC3.0 简单实现(增删改查,Web版实现)

MyBatis3.2.2+SpringMVC3.0 简单实现(增删改查,Web版实现) 首先,需要知道Eclipse如何创建Dynamic Web Project for Maven,我们首先需要知道如何用Eclipse创建动态部署的Maven Web-app 项目.参考以下链接的博客:http://blog.csdn.net/smilevt/article/details/8215558. 构建完之后:实现具体的增删改查,不去部署Web war的时候我们用Junit单元测试CRUD功能.代码如

SpringMVC3.0配置

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/s

Springmvc3.0 注解详解

相关关键词:  android教程 1,  为什么要用注解,注解有什么好处? Springmvc3.0 是基于注解进行编程,能大大提高开发效率,和维护成本.相比较SSH(struts2+spring+hibernate)   springmvc3.0去除了struts2.0 ,spring, hinernate 配置文件的编写和维护,从而可以更加快速的进行开发.所有这一切的实现就是基于注解进行编程. 2,  怎么去理解注解? 注解可以写在类上面,也可以写在方法上面,在目前我们的系统中,基于方法的

ENode 2.0 - 深入分析ENode的内部实现流程和关键地方的幂等设计

前言 ENode是一个基于消息的架构,使用ENode开发的系统,每个环节都是处理消息,处理完后产生新的消息.本篇文章我想详细分析一下ENode框架内部是如何实现整个消息处理流程的.为了更好的理解我后面的流程的描述,我觉得还是应该先把ENode的架构图贴出来,好让大家在看后面的分析时,可以对照这个架构图进行思考和理解. ENode架构图 ENode框架内部实现流程分析 Controller发送ICommand到消息队列(EQueue): [从这一步开始处理Command]ENode.EQueue中

SpringMVC3.0+MyIbatis3.0(分页示例)【转】

主要使用Oracle的三层sql实现分页! 一 环境:XP3+Oracle10g+MyEclipse6+(Tomcat)+JDK1.5 二 工程相关图片: 1 DEMO图片 2 工程代码图片  3 相关jar包图片  三 此示例是在: SSI:SpringMVC3+Mybatis3(登录及CRUD操作)基础上加的分页功能: 四 主要代码文件 1 BaseController.java用于子类调用方便 Java代码   package com.liuzd.ssm.web; import javax

SpringMVC3.0+MyIbatis3.0(分页示例)

参考资料 1 ibatis2.x与mybatis(ibatis3.x)的比较 http://zhaohe162.blog.163.com/blog/static/382167972011111114742371/2 MyBatis学习 之 三.动态SQL语句 http://limingnihao.iteye.com/blog/782190主要使用Oracle的三层sql实现分页! 一 环境:XP3+Oracle10g+MyEclipse6+(Tomcat)+JDK1.5 二 工程相关图片: 1

IBatis 2.x 和 MyBatis 3.0.x 的区别(从 iBatis 到 MyBatis)

从 iBatis 到 MyBatis,你准备好了吗? 对于从事 Java EE 的开发人员来说,iBatis 是一个再熟悉不过的持久层框架了,在 Hibernate.JPA 这样的一站式对象 / 关系映射(O/R Mapping)解决方案盛行之前,iBaits 基本是持久层框架的不二选择.即使在持久层框架层出不穷的今天,iBatis 凭借着易学易用.轻巧灵活等特点,也仍然拥有一席之地.尤其对于擅长 SQL 的开发人员来说,iBatis 对 SQL 和存储过程的直接支持能够让他们在获得 iBati

SpringMvc注解整合mybatis+ExtJS5.0框架

1先看整体结构 2,使用到的配置文件 2.1 jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8 username=root password=mayude #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最

SpringMVC+mybatis(oracle 和 mysql) HTML5 hibernate+ehcache二级缓存_shiro_druid_bootstrap

A 代码生成器(开发利器);  B 阿里数据库连接池druid; C 安全权限框架shiro ; D ehcache 自定义二级缓存  (后续会加入Activiti5 工作流 ) 系统为主流的 springmvc+mybaits 3.2 版本 ,提供maven的pom.xml文件,另免费赠送hibernate版本一套(垮数据库) 1. 有 oracle .msyql.spring3.0.spring4.0  一共 4 套版本全部提供没有打jar没有加密的源代码(最下面截图2.1版本) 2. 支持