mysql template sql_SpringBoot-JdbcTemplates-MySQL

配置 application.yml

spring:

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3333/springboot

username: root

password: 123

配置实体类 类名为 Account

DAO层

接口 :

public interface IAccountDao {

int add(Account account);

int update(Account account);

int delete(int id);

Account findAccountById(int id);

List findAccountList();

}

实现类 :

@Repository

public class AccountDao implements IAccountDao {

@Autowired

private JdbcTemplate jdbcTemplate;

@Override

public int add(Account account) {

int add = jdbcTemplate.update("INSERT INTO account(NAME,money) values(?,?)", new Object[]{account.getName(), account.getMoney()});

return add;

}

@Override

public int update(Account account) {

String sql = " update account set name = ?,money = ? where id = ?";

int update = jdbcTemplate.update(sql, new Object[]{account.getName(),account.getMoney(),account.getId()});

return update;

}

@Override

public int delete(int id) {

String sql = "delete from account where id = ?";

return jdbcTemplate.update(sql,new Object[]{id});

}

@Override

public Account findAccountById(int id) {

String sql = "select id,name,money from account where id = ?";

List query = jdbcTemplate.query(sql, new Object[]{id}, new BeanPropertyRowMapper(Account.class));

if (query != null){

return query.get(0);

}

return null;

}

@Override

public List findAccountList() {

String sql = "select id,name,money from account";

List accounts = jdbcTemplate.query(sql,new Object[]{}, new BeanPropertyRowMapper(Account.class));

return accounts;

}

public List find() {

String sql = "select id,name,money from account";

List accounts = jdbcTemplate.query(sql, new Object[]{}, new RowMapper() {

List lists = new ArrayList();

@Override

public Account mapRow(ResultSet resultSet, int i) throws SQLException {

Account account = new Account();

account.setId(resultSet.getInt(1));

account.setName(resultSet.getString(2));

account.setMoney(resultSet.getDouble(3));

return account;

}

});

return accounts;

}

}

service层

接口 :

public interface IAccountService {

int add(Account account);

int update(Account account);

int delete(int id);

Account findAccountById(int id);

List findAccountList();

}

实现类 :

@Service

public class AccountService implements IAccountService {

@Autowired

private IAccountDao accountDao;

@Override

public int add(Account account) {

return accountDao.add(account);

}

@Override

public int update(Account account) {

return accountDao.update(account);

}

@Override

public int delete(int id) {

return accountDao.delete(id);

}

@Override

public Account findAccountById(int id) {

return accountDao.findAccountById(id);

}

@Override

public List findAccountList() {

return accountDao.find();

}

controller层

控制层

@RestController

@RequestMapping("account")

public class AccountController {

@Autowired

private IAccountService accountService;

@RequestMapping(value = "add",method = RequestMethod.POST)

public int insert(@RequestParam(value = "name")String name,@RequestParam(value = "money") Double money){

Account account = new Account();

account.setName(name);

account.setMoney(money);

System.out.println("add");

int add = accountService.add(account);

return add;

}

@RequestMapping(value = "delete",method = RequestMethod.GET)

public int delete(@RequestParam("id") int id){

System.out.println("delete");

return accountService.delete(id);

}

@RequestMapping("update")

public int modify(@RequestParam("id") int id,@RequestParam("name") String name,@RequestParam("money") Double money){

System.out.println("update");

Account account = new Account();

account.setId(id);

account.setName(name);

account.setMoney(money);

return accountService.update(account);

}

@RequestMapping(value = "/{id}",method = RequestMethod.GET)

public Account findOne(@PathVariable("id") int id){

System.out.println("queryById");

return accountService.findAccountById(id);

}

@RequestMapping(value = "findAll",method = RequestMethod.GET)

public List findAll(){

System.out.println("query");

return accountService.findAccountList();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值