| 1 | package br.org.agilcoop.cursos.testes.bancoDeDados; |
| 2 | |
| 3 | import java.util.List; |
| 4 | import java.util.Map; |
| 5 | |
| 6 | import org.springframework.jdbc.core.JdbcTemplate; |
| 7 | |
| 8 | public class UsuarioDAO { |
| 9 | JdbcTemplate jdbc; |
| 10 | |
| 11 | public UsuarioDAO(JdbcTemplate jdbc) { |
| 12 | this.jdbc = jdbc; |
| 13 | } |
| 14 | |
| 15 | public void insereUsuario(String nome, String senha) { |
| 16 | int id = jdbc.queryForInt("select max(id) from usuario"); |
| 17 | id += 1; |
| 18 | Object[] args = new Object[] {id, nome, senha}; |
| 19 | jdbc.update("insert into usuario (id, nome, senha) values (?, ?, ?)", args); |
| 20 | } |
| 21 | |
| 22 | public void atualizaUsuario(Integer id, String nome, String senha) { |
| 23 | jdbc.update("update usuario set nome = ?, senha = ? where id = ?", new Object[] {nome, senha, id}); |
| 24 | } |
| 25 | |
| 26 | public void removeUsuario(String nome) { |
| 27 | jdbc.update("delete from usuario where nome = ?", new Object[]{nome}); |
| 28 | } |
| 29 | |
| 30 | @SuppressWarnings("unchecked") |
| 31 | public List<Map<String, Object>> buscaPorNome(String nome) { |
| 32 | List<Map<String, Object>> map; |
| 33 | map = jdbc.queryForList("select * from usuario where nome like ?", new Object[]{nome.replaceAll("[*]", "%")}); |
| 34 | return map; |
| 35 | } |
| 36 | } |