| 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 | 0 | public UsuarioDAO(JdbcTemplate jdbc) { |
| 12 | 0 | this.jdbc = jdbc; |
| 13 | 0 | } |
| 14 | |
|
| 15 | |
public void insereUsuario(String nome, String senha) { |
| 16 | 0 | int id = jdbc.queryForInt("select max(id) from usuario"); |
| 17 | 0 | id += 1; |
| 18 | 0 | Object[] args = new Object[] {id, nome, senha}; |
| 19 | 0 | jdbc.update("insert into usuario (id, nome, senha) values (?, ?, ?)", args); |
| 20 | 0 | } |
| 21 | |
|
| 22 | |
public void atualizaUsuario(Integer id, String nome, String senha) { |
| 23 | 0 | jdbc.update("update usuario set nome = ?, senha = ? where id = ?", new Object[] {nome, senha, id}); |
| 24 | 0 | } |
| 25 | |
|
| 26 | |
public void removeUsuario(String nome) { |
| 27 | 0 | jdbc.update("delete from usuario where nome = ?", new Object[]{nome}); |
| 28 | 0 | } |
| 29 | |
|
| 30 | |
@SuppressWarnings("unchecked") |
| 31 | |
public List<Map<String, Object>> buscaPorNome(String nome) { |
| 32 | |
List<Map<String, Object>> map; |
| 33 | 0 | map = jdbc.queryForList("select * from usuario where nome like ?", new Object[]{nome.replaceAll("[*]", "%")}); |
| 34 | 0 | return map; |
| 35 | |
} |
| 36 | |
} |