数据操作逻辑

结合上一篇笔记,这一篇是关于数据操作的各种逻辑实现,以Message类为例

 

包图
数据操作逻辑

Message类,已简化

 

package com.ccit.bean;

public class Message {
	private Integer id;
	private String title;
	private String content;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
}

 

dao.inter包 MessageDao接口

 

package com.ccit.dao.inter;

import com.ccit.bean.Message;
import com.ccit.tools.PageDiv;

public interface MessageDao {
	public boolean add(Message m);
	public boolean update(Message m);
	public boolean delete(int id);
	public Message getMsg(int id);
	public PageDiv<Message> getAll(int pageNo, int pageSize);
.。。。。。
}

 

dao.impl包

 

DaoBase类

package com.ccit.dao.impl;

import com.ccit.jdbc.template.JDBCTemplate;
public class DaoBase {
	protected JDBCTemplate template = JDBCTemplate.getJDBCTemplate();
}

 MessageDaoImpl类

package com.ccit.dao.impl;

import java.sql.Timestamp;
import java.util.List;

import com.ccit.bean.Message;
import com.ccit.dao.inter.MessageDao;
import com.ccit.rowmap.MsgRowMapper;
import com.ccit.tools.PageDiv;

public class MessageDaoImpl extends DaoBase implements MessageDao {

	@Override
	public boolean add(Message m) {
		boolean flag = false;
		String sql="insert into t_msg(m_title ,m_content)values(?,?)";
		if(this.template.ExecuteUpdate(sql, m.getTitle(),m.getContent(),) > 0) {
			flag = true;
		}
		return flag;
	}
	@Override
	public boolean update(Message m) {
		boolean flag = false;
		String sql = "update t_msg set m_title=?,m_content=? where id=?";
		if(this.template.ExecuteUpdate(sql, m.getTitle(),m.getContent(),m.getId()) > 0) {
			flag = true;
		}
		return flag;
	}
	@Override
	public boolean delete(int id) {
		boolean flag = false;
		String sql="delete from t_msg where id = (?)";
		if(this.template.ExecuteUpdate(sql,id)>0){
			flag = true;
		}
		return flag;
	}
	@Override
	public Message getMsg(int id) {
		Message m = null;
		String sql="select * from t_msg where id="+id;
		m = (Message) this.template.getUniquObject(sql, new MsgRowMapper());
		return m;
	}

。。。。。。
}

 

dao.factory包 DaoFactory类

package com.ccit.dao.factory;

import java.io.IOException;
import java.util.Properties;

import com.ccit.dao.impl.DaoBase;

public class DaoFactory {

	public static DaoBase getDao(String daoName){
		DaoBase db = null;
		Properties p = new Properties();
		try {
			p.load(DaoFactory.class.getResourceAsStream("/dao.properties"));
			db=(DaoBase)Class.forName(p.getProperty(daoName)).newInstance();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		return db;
	}
}

 

JDBCTemplate类

请参照另一篇笔记http://ericblog.iteye.com/blog/1014356

 

MsgRowMapper类

实现RowMapper接口

package com.ccit.rowmap;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.ccit.bean.Message;
import com.ccit.dao.inter.RowMapper;

public class MsgRowMapper implements RowMapper {
	@Override
	public Object RowMap(ResultSet rs) {
		Message m = new Message();
		try {
			m.setContent(rs.getString("m_content"));
			m.setId(rs.getInt("id"));
			m.setTitle(rs.getString("m_title"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return m;
	}
}

 

RowMapper接口

package com.ccit.dao.inter;

import java.sql.ResultSet;

public interface RowMapper {

	/**
	 * 把RS中的当前光标的数据转换为一个对象
	 * @param rs
	 * @return
	 */
	public Object RowMap(ResultSet rs);
}

 

dao.properties配置信息

MessageDao=com.ccit.dao.impl.MessageDaoImpl

 

代码挺多,跑起来之后更有助于理解。

对Message操作全放在MessageDaoImpl这个类中实现,在调用时通过DaoFactory获得。impl类将写好的sql语句及其他参数交给JDBCTemplate类执行。

你可能感兴趣的:(DAO,sql,bean,jdbc,ITeye)