代码实现对数据库新增查询操作。
编写对MySQL数据库新增、查询操作的接口定义。
public interface EchoDao{
public List findAll();
public Account findById(int id);
public int addAccount(Account account);
MySQL数据库新增、查询操作接口的实现类及查询数据库的sql语句。
@Component
@Qualifier("echoDao")
public class EchoDaoImpl implements EchoDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public List findAll() {
RowMapper rowMapper = new BeanPropertyRowMapper(Account.class);
return jdbcTemplate.query("select id, user_name, user_age from account", rowMapper);
}
@Override
public Account findById(int id) {
RowMapper rowMapper = new BeanPropertyRowMapper(Account.class);
return jdbcTemplate.queryForObject("select id, user_name, user_age from account where id = ?", rowMapper, id);
}
@Override
public int addAccount(Account account) {
return jdbcTemplate.update("INSERT INTO account
(user_name
,user_age
) VALUES(?,?)", account.getUserName(),
account.getUserAge());
}
}