Spring boot 入門三:SpringBoot用JdbcTemplates訪問Mysql 實現增刪改查



建表腳本

-- create table `account`
DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`money` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');


添加依賴


在pom文件引入spring-boot-starter-jdbc的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
引入mysql連接類和連接池:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>


數據庫配置


繼上一節新建了一個開發環境的配置文件 application-dev.properties 所以在這個配置文件中做配置mysql的驅動類,數據庫地址,數據庫賬號、密碼信息,然后將application.yml 的生效配置文件改成dev
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=123456
通過引入這些依賴和配置一些基本信息,springboot就可以訪問數據庫類


具體編碼

實體類
public class Account {
private int id ;
private String name ;
private double money;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getMoney() {
return money;
}

public void setMoney(double money) {
this.money = money;
}
}
dao層
public interface IAccountDAO {
int add(Account account);

int update(Account account);

int delete(int id);

Account findAccountById(int id);

List<Account> findAccountList();
}
實現類
@Repository
public class AccountDAOImpl implements IAccountDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int add(Account account) {
int id =jdbcTemplate.update("INSERT INTO account(name,money) VALUES (?,?)",account.getName(),account.getMoney());
return id;
}

@Override
public int update(Account account) {
int id =jdbcTemplate.update("UPDATE account set name=? ,money=? where id=?",account.getName(),account.getMoney(),account.getId());
return id;
}

@Override
public int delete(int id) {
return jdbcTemplate.update("DELETE FROM account where id=?",id);
}

@Override
public Account findAccountById(int id) {
List<Account> list = jdbcTemplate.query("select * from account where id = ?", new Object[]{id}, new BeanPropertyRowMapper(Account.class));
if(list!=null && list.size()>0){
Account account = list.get(0);
return account;
}else{
return null;
}
}

@Override
public List<Account> findAccountList() {
List<Account> list = jdbcTemplate.query("select * from account", new Object[]{}, new BeanPropertyRowMapper(Account.class));
if(list!=null && list.size()>0){
return list;
}else{
return null;
}
}
}
service層
public interface IAccountService {
int add(Account account);

int update(Account account);

int delete(int id);

Account findAccountById(int id);

List<Account> findAccountList();
}
實現類
@Service
public class AccountServiceImpl 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<Account> findAccountList() {
return accountDAO.findAccountList();
}
}
構建一組restful api來展示
@RestController
@RequestMapping("/account")
public class AccountController {
@Autowired
private IAccountService accountService;

@RequestMapping(value = "/getAccounts",method = RequestMethod.GET)
public List<Account> getAccounts() {
return accountService.findAccountList();
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Account getAccountById(@PathVariable("id") int id){
return accountService.findAccountById(id);
}
@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
public String updateAccount(@PathVariable("id")int id , @RequestParam(value = "name",required = true)String name,
@RequestParam(value = "money" ,required = true)double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
account.setId(id);
int t=accountService.update(account);
if(t==1){
return account.toString();
}else {
return "fail";
}
}

@RequestMapping(value = "",method = RequestMethod.POST)
public String postAccount( @RequestParam(value = "name")String name, @RequestParam(value = "money" )double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
int t= accountService.add(account);
if(t==1){
return account.toString();
}else {
return "fail";
}

}
}
通過postman進行測試,讀者自行測試,這里就不貼圖啦。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM