java dao层设计_DAO层设计的问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package cn.com.seegoo.autophone.DAO; import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.List; import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.PreparedStatementCreator;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.jdbc.core.support.JdbcDaoSupport; import cn.com.seegoo.autophone.Beans.Client;

import cn.com.seegoo.autophone.Beans.Information; public class InformationDAO extends JdbcDaoSupport { private final String insertSql = "insert into manager_information (type,content,entering_time) values (?, ?,now())";

// now()只用于mysql数据库

private final String updateSql = "update manager_information set type = ?, content = ? where infoid = ?";

// 时间不改

public InformationDAO() { } private Information formInformation(ResultSet rs) throws SQLException {

final Information information = new Information();

information.setInfoid(rs.getInt("infoid"));

information.setType(rs.getString("type"));

information.setContent(rs.getString("content"));

information.setEntering_time(rs.getDate("entering_time"));

return information;

} private final class InformationMapper implements RowMapper {

// RowMapper 将查询到的每行记录信息都封装到一个entity类里边,方便使用。

@Override

public Information mapRow(ResultSet rs, int rowNum) throws SQLException {

final Information information = formInformation(rs);

return information;

} } public boolean add(final Information information) {

if (information == null) {

return false;

}

int count = getJdbcTemplate().update(new PreparedStatementCreator() {

@Override

public PreparedStatement createPreparedStatement(

Connection connection) throws SQLException {

final PreparedStatement insertStatement = connection

.prepareStatement(insertSql);

insertStatement.setString(1, information.getType());

insertStatement.setString(2, information.getContent());

return insertStatement;

}

});

return count > 0;

} public boolean remove(int id) {

if (id > 0) {

int count = getJdbcTemplate().update(

"delete from manager_information where infoid=?", id);

return count > 0;

}

return false;

} public boolean update(Information information) {

if (information == null || information.getInfoid() <= 0) {

return false;

}

int count = getJdbcTemplate().update(updateSql, information.getType(),

information.getContent(), information.getInfoid());

return count > 0;

} public List queryInformation(String sql) {

return getJdbcTemplate().query(sql, new InformationMapper());

} public List getAllInformation() {

return queryInformation("select * from manager_information order by infoid desc");

} }

这是对消息的DAO层,本来设计上没有问题

但是有20多个实体,也就是说要写20多个dao层

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值