Desenvolvi baseado na necessidade do projeto. É bem simples. To lendo aqui sobre o MyBatis e é muita coisa.
Vou mostrar aqui como esse meu projeto ajudou no meu sistema:
Classe Original
/**
*
* @author krismorte
*/
public class AutorizaHoraExtraDao {
public boolean insert(AutorizaHoraExtra model) {
try {
Connection con = ConnectionFactory.connectBase();
String insert = "insert into hep_autoriza_hora_extra values(?,?,?,?,?)";
PreparedStatement cmd = con.prepareStatement(insert);
cmd.setString(1, model.getHoraExtra().getUsuario().getMatricula());
cmd.setDate(2, new Date(model.getHoraExtra().getData().getMillis()));
cmd.setDate(3, new Date(model.getDataAprovacao().getMillis()));
cmd.setString(4, model.getJustificativa());
cmd.setString(5, model.getAprovador().getMatricula());
cmd.executeUpdate();
cmd.close();
con.close();
return true;
} catch (Exception e) {
e.printStackTrace();
Main.addMsgLog(AutorizaHoraExtraDao.class.getName(), e.getMessage());
return false;
}
}
public boolean delete(String usuario, Date data) {
try {
Connection con = ConnectionFactory.connectBase();
String insert = "delete from hep_autoriza_hora_extra where usuario=? and data=?";
PreparedStatement cmd = con.prepareStatement(insert);
cmd.setString(1, usuario);
cmd.setDate(2, data);
cmd.executeUpdate();
cmd.close();
con.close();
return true;
} catch (Exception e) {
e.printStackTrace();
Main.addMsgLog(AutorizaHoraExtraDao.class.getName(), e.getMessage());
return false;
}
}
public AutorizaHoraExtra select(String usuarioMatricula, Date data) {
try {
Connection con = ConnectionFactory.connectBase();
AutorizaHoraExtra lst = null;
String select = "select hah.*,hu.matricula,hu.nome,hu.email,ha.matricula,ha.nome,ha.email,hhe.motivo,hhe.hora_entrada,"
+ " hhe.hora_saida,hhe.hora_total , case when hhe.motivo is null then 'NÃO LANÇADA' else 'LANÇADA' end"
+ " from hep_autoriza_hora_extra hah(nolock)"
+ " left join hep_hora_extra hhe on hah.usuario=hhe.usuario and hah.data=hhe.data"
+ " inner join hep_usuario hu on hah.usuario=hu.matricula"
+ " inner join hep_usuario ha on hah.aprovador=ha.matricula"
+ " where hah.usuario=? and hah.data=?";
PreparedStatement cmd = con.prepareStatement(select);
cmd.setString(1, usuarioMatricula);
cmd.setDate(2, data);
ResultSet set = cmd.executeQuery();
if (set.next()) {
Date d = set.getDate(2);
Date d2 = set.getDate(3);
UsuarioInterno usuario = new UsuarioInterno();
usuario.setMatricula(set.getString(6));
usuario.setNome(set.getString(7));
usuario.setEmail(set.getString(8));
UsuarioInterno aprovador = new UsuarioInterno();
aprovador.setMatricula(set.getString(9));
aprovador.setNome(set.getString(10));
aprovador.setEmail(set.getString(11));
lst = new AutorizaHoraExtra(usuario, new DateTime(d.getTime()), set.getString(12), set.getString(13), set.getString(14), set.getString(15), new DateTime(d2.getTime()), set.getString(4), aprovador, set.getString(16));
}
con.close();
cmd.close();
return lst;
} catch (Exception e) {
Main.addMsgLog(AutorizaHoraExtraDao.class.getName(), e.getMessage());
return null;
}
}
public List<AutorizaHoraExtra> list(int ano,int mes) {
try {
Connection con = ConnectionFactory.connectBase();
List<AutorizaHoraExtra> lst = new ArrayList<AutorizaHoraExtra>();
String select = "select hah.*,hu.matricula,hu.nome,hu.email,ha.matricula,ha.nome,ha.email,hhe.motivo,hhe.hora_entrada,"
+ "hhe.hora_saida,hhe.hora_total , case when hhe.motivo is null then 'NÃO LANÇADA' else 'LANÇADA' end"
+ " from hep_autoriza_hora_extra hah(nolock)"
+ " left join hep_hora_extra hhe on hah.usuario=hhe.usuario and hah.data=hhe.data"
+ " inner join hep_usuario hu on hah.usuario=hu.matricula"
+ " inner join hep_usuario ha on hah.aprovador=ha.matricula"
+ " where DATEPART(YEAR,hah.data)=? and DATEPART(MONTH,hah.data)=?";
PreparedStatement cmd = con.prepareStatement(select);
cmd.setInt(1, ano);
cmd.setInt(2, mes);
ResultSet set = cmd.executeQuery();
while (set.next()) {
Date d = set.getDate(2);
Date d2 = set.getDate(3);
UsuarioInterno usuario = new UsuarioInterno();
usuario.setMatricula(set.getString(6));
usuario.setNome(set.getString(7));
usuario.setEmail(set.getString(8));
UsuarioInterno aprovador = new UsuarioInterno();
aprovador.setMatricula(set.getString(9));
aprovador.setNome(set.getString(10));
aprovador.setEmail(set.getString(11));
lst.add(new AutorizaHoraExtra(usuario, new DateTime(d.getTime()), set.getString(12), set.getString(13), set.getString(14), set.getString(15), new DateTime(d2.getTime()), set.getString(4), aprovador, set.getString(16)));
}
con.close();
cmd.close();
return lst;
} catch (Exception e) {
e.printStackTrace();
Main.addMsgLog(AutorizaHoraExtraDao.class.getName(), e.getMessage());
return null;
}
}
public List<AutorizaHoraExtra> list(Date ini, Date fim) {
try {
Connection con = ConnectionFactory.connectBase();
List<AutorizaHoraExtra> lst = new ArrayList<AutorizaHoraExtra>();
String select = "select hah.*,hu.matricula,hu.nome,hu.email,ha.matricula,ha.nome,ha.email,hhe.motivo,hhe.hora_entrada,"
+ "hhe.hora_saida,hhe.hora_total , case when hhe.motivo is null then 'NÃO LANÇADA' else 'LANÇADA' end"
+ " from hep_autoriza_hora_extra hah(nolock)"
+ " left join hep_hora_extra hhe on hah.usuario=hhe.usuario and hah.data=hhe.data"
+ " inner join hep_usuario hu on hah.usuario=hu.matricula"
+ " inner join hep_usuario ha on hah.aprovador=ha.matricula"
+ " where hah.data between ? and ?";
PreparedStatement cmd = con.prepareStatement(select);
cmd.setDate(1, ini);
cmd.setDate(2, fim);
ResultSet set = cmd.executeQuery();
while (set.next()) {
Date d = set.getDate(2);
Date d2 = set.getDate(3);
UsuarioInterno usuario = new UsuarioInterno();
usuario.setMatricula(set.getString(6));
usuario.setNome(set.getString(7));
usuario.setEmail(set.getString(8));
UsuarioInterno aprovador = new UsuarioInterno();
aprovador.setMatricula(set.getString(9));
aprovador.setNome(set.getString(10));
aprovador.setEmail(set.getString(11));
lst.add(new AutorizaHoraExtra(usuario, new DateTime(d.getTime()), set.getString(12), set.getString(13), set.getString(14), set.getString(15), new DateTime(d2.getTime()), set.getString(4), aprovador, set.getString(16)));
}
con.close();
cmd.close();
return lst;
} catch (Exception e) {
Main.addMsgLog(AutorizaHoraExtraDao.class.getName(), e.getMessage());
return null;
}
}
}
Classe alterada
/**
*
* @author krismorte
*/
public class AutorizaHoraExtraDao extends EntidadeJDBC<AutorizaHoraExtra> {
public static final Integer CONSULTA_POR_MES = 0;
public static final Integer CONSULTA_POR_PERIODO = 1;
public static final Integer INCLUIR_REGISTRO = 2;
public static final Integer REMOVER_REGISTRO = 3;
public static final Integer CONSULTA_POR_USUARIO_DATA = 4;
private Connection conexao;
public AutorizaHoraExtraDao() throws ExcecaoDao {
super();
adicionaComandoSQL(CONSULTA_POR_MES, "select hah.*,hu.matricula,hu.nome,hu.email,ha.matricula,ha.nome,ha.email,hhe.motivo,hhe.hora_entrada,"
+ "hhe.hora_saida,hhe.hora_total , case when hhe.motivo is null then 'NÃO LANÇADA' else 'LANÇADA' end"
+ " from hep_autoriza_hora_extra hah(nolock)"
+ " left join hep_hora_extra hhe on hah.usuario=hhe.usuario and hah.data=hhe.data"
+ " inner join hep_usuario hu on hah.usuario=hu.matricula"
+ " inner join hep_usuario ha on hah.aprovador=ha.matricula"
+ " where DATEPART(YEAR,hah.data)=? and DATEPART(MONTH,hah.data)=?");
adicionaComandoSQL(CONSULTA_POR_PERIODO, "select hah.*,hu.matricula,hu.nome,hu.email,ha.matricula,ha.nome,ha.email,hhe.motivo,hhe.hora_entrada,"
+ "hhe.hora_saida,hhe.hora_total , case when hhe.motivo is null then 'NÃO LANÇADA' else 'LANÇADA' end"
+ " from hep_autoriza_hora_extra hah(nolock)"
+ " left join hep_hora_extra hhe on hah.usuario=hhe.usuario and hah.data=hhe.data"
+ " inner join hep_usuario hu on hah.usuario=hu.matricula"
+ " inner join hep_usuario ha on hah.aprovador=ha.matricula"
+ " where hah.data between ? and ?");
adicionaComandoSQL(INCLUIR_REGISTRO, "insert into hep_autoriza_hora_extra values(?,?,?,?,?)");
adicionaComandoSQL(REMOVER_REGISTRO, "delete from hep_autoriza_hora_extra where usuario=? and data=?");
adicionaComandoSQL(CONSULTA_POR_USUARIO_DATA, "select hah.*,hu.matricula,hu.nome,hu.email,ha.matricula,ha.nome,ha.email,hhe.motivo,hhe.hora_entrada,"
+ " hhe.hora_saida,hhe.hora_total , case when hhe.motivo is null then 'NÃO LANÇADA' else 'LANÇADA' end"
+ " from hep_autoriza_hora_extra hah(nolock)"
+ " left join hep_hora_extra hhe on hah.usuario=hhe.usuario and hah.data=hhe.data"
+ " inner join hep_usuario hu on hah.usuario=hu.matricula"
+ " inner join hep_usuario ha on hah.aprovador=ha.matricula"
+ " where hah.usuario=? and hah.data=?");
setExcecao(new ExceptionDao(AutorizaHoraExtraDao.class.getName()));
}
@Override
public Connection getConexao() {
return ConnectionFactory.connectBase();
}
@Override
public AutorizaHoraExtra consultar() throws ExcecaoDao {
try {
conexao = getConexao();
AutorizaHoraExtra lst = null;
ResultSet set = executaConsulta();
if (set.next()) {
Date d = set.getDate(2);
Date d2 = set.getDate(3);
UsuarioInterno usuario = new UsuarioInterno();
usuario.setMatricula(set.getString(6));
usuario.setNome(set.getString(7));
usuario.setEmail(set.getString(8));
UsuarioInterno aprovador = new UsuarioInterno();
aprovador.setMatricula(set.getString(9));
aprovador.setNome(set.getString(10));
aprovador.setEmail(set.getString(11));
lst = new AutorizaHoraExtra(usuario, new DateTime(d.getTime()), set.getString(12), set.getString(13), set.getString(14), set.getString(15), new DateTime(d2.getTime()), set.getString(4), aprovador, set.getString(16));
}
return lst;
} catch (Exception e) {
getExcecao().lancaExcecao(e);
return null;
} finally {
finalizaConexaoComSeguranca(conexao);
}
}
@Override
public List listar() throws ExcecaoDao {
try {
List<AutorizaHoraExtra> lst = new ArrayList();
conexao = getConexao();
ResultSet set = executaConsulta();
while (set.next()) {
Date d = set.getDate(2);
Date d2 = set.getDate(3);
UsuarioInterno usuario = new UsuarioInterno();
usuario.setMatricula(set.getString(6));
usuario.setNome(set.getString(7));
usuario.setEmail(set.getString(8));
UsuarioInterno aprovador = new UsuarioInterno();
aprovador.setMatricula(set.getString(9));
aprovador.setNome(set.getString(10));
aprovador.setEmail(set.getString(11));
lst.add(new AutorizaHoraExtra(usuario, new DateTime(d.getTime()), set.getString(12), set.getString(13), set.getString(14), set.getString(15), new DateTime(d2.getTime()), set.getString(4), aprovador, set.getString(16)));
}
return lst;
} catch (Exception e) {
getExcecao().lancaExcecao(e);
return null;
} finally {
finalizaConexaoComSeguranca(conexao);
}
}
}
Exemplo de Chamada
public boolean add() throws ExcecaoDao {
EntidadeJDBC dao = new AutorizaHoraExtraDao();
dao.escolherComandoSQL(AutorizaHoraExtraDao.INCLUIR_REGISTRO);
dao.adicionaCampos(getHoraExtra().getUsuario().getMatricula(), new Date(getHoraExtra().getData().getMillis()),
new Date(getDataAprovacao().getMillis()), getJustificativa(), getAprovador().getMatricula());
dao.incluir();
return true;
}