(1) pom.xml
1 <!-- -starter-parent --> 2 <parent> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-parent</artifactId> 5 <version>1.5.2.RELEASE</version> 6 <relativePath /> 7 </parent> 8 9 <properties> 10 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 11 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 12 <java.version>1.8</java.version> 13 </properties> 14 15 <!-- -starter-web --> 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-web</artifactId> 20 </dependency> 21 22 <!-- -starter-test --> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-test</artifactId> 26 <scope>test</scope> 27 </dependency> 28 29 <!-- -configuration-processor --> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-configuration-processor</artifactId> 33 <optional>true</optional> 34 </dependency> 35 36 <!-- -starter-jdbc --> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-jdbc</artifactId> 40 </dependency> 41 42 <!-- mysql-connector-java --> 43 <dependency> 44 <groupId>mysql</groupId> 45 <artifactId>mysql-connector-java</artifactId> 46 <scope>runtime</scope> 47 </dependency> 48 49 </dependencies>
(2) application.properties
### 數據庫參數(JdbcTemplate)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:///springboot_jdbctemplate spring.datasource.username=root spring.datasource.password=root
(3) 數據表
1 CREATE TABLE `account` (
2 `id` INT(11) NOT NULL AUTO_INCREMENT, 3 `name` VARCHAR(20) NOT NULL, 4 `money` DOUBLE DEFAULT NULL, 5 PRIMARY KEY (`id`) 6 ) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; 7 8 INSERT INTO `account` VALUES (NULL, 'aaa', '1000'); 9 INSERT INTO `account` VALUES (NULL, 'bbb', '2000'); 10 INSERT INTO `account` VALUES (NULL, 'ccc', '3000');
(4) Account.java
1 public class Account {
2
3 private int id; 4 private String name; 5 private double money; 6 7 // get / set 方法省略 ... ... 8 9 @Override 10 public String toString() { 11 return "ID: " + id + ", Name: " + name + ", Money: " + money; 12 } 13 14 }
IAccountDao.java
1 public interface IAccountDao {
2
3 List<Account> findAll(); 4 5 Account findById(Integer id); 6 7 int add(Account account); 8 9 int update(Account account); 10 11 int deleteById(Integer id); 12 13 }
AccountDaoImpl.java
1 @Repository
2 public class AccountDaoImpl implements IAccountDao { 3 4 @Autowired 5 private JdbcTemplate jdbcTemplate; 6 7 public List<Account> findAll() { 8 List<Account> list = jdbcTemplate.query("select * from account", new Object[]{}, new BeanPropertyRowMapper<Account>(Account.class)); 9 if (!list.isEmpty()) { 10 return list; 11 } 12 return null; 13 } 14 15 public Account findById(Integer id) { 16 List<Account> list = jdbcTemplate.query("select * from account where id = ?", new Object[] { id }, new BeanPropertyRowMapper<Account>(Account.class)); 17 if (!list.isEmpty()) { 18 Account account = list.get(0); 19 return account; 20 } 21 return null; 22 } 23 public int add(Account account) { 24 return jdbcTemplate.update("insert into account(id, name, money) values(null, ?, ?)", account.getName(), account.getMoney()); 25 } 26 27 public int update(Account account) { 28 return jdbcTemplate.update("update account set name = ?, money = ? where id = ?", account.getName(), account.getMoney(), account.getId()); 29 } 30 31 public int deleteById(Integer id) { 32 return jdbcTemplate.update("delete from account where id = ?", id); 33 } 34 35 }
IAccountService.java
1 public interface IAccountService {
2
3 List<Account> findAll(); 4 5 Account findById(Integer id); 6 7 int add(Account account); 8 9 int update(Account account); 10 11 int deleteById(Integer id); 12 13 }
AccountServiceImpl.java
1 @Service
2 @Transactional 3 public class AccountServiceImpl implements IAccountService { 4 5 @Autowired 6 private IAccountDao acountDao; 7 8 public List<Account> findAll() { 9 return acountDao.findAll(); 10 } 11 12 public Account findById(Integer id) { 13 return acountDao.findById(id); 14 } 15 16 public int add(Account account) { 17 return acountDao.add(account); 18 } 19 20 public int update(Account account) { 21 return acountDao.update(account); 22 } 23 24 public int deleteById(Integer id) { 25 return acountDao.deleteById(id); 26 } 27 28 }
AccountController.java
1 @RestController
2 @RequestMapping("/account") 3 public class AccountController { 4 5 @Autowired 6 private IAccountService accountService; 7 8 @RequestMapping(value = "/list", method = RequestMethod.GET) 9 public List<Account> selectList() { 10 return accountService.findAll(); 11 } 12 13 // localhost:8080/account/find/6 14 @RequestMapping(value = "/find/{id}", method = RequestMethod.GET) 15 public Account getById(@PathVariable("id") Integer id) { 16 return accountService.findById(id); 17 } 18 19 @RequestMapping(value = "/add", method = RequestMethod.GET) 20 public String add( // 21 @RequestParam(value = "name", required = true) String name, // 22 @RequestParam(value = "money", required = true) double money) { 23 24 Account account = new Account(); 25 account.setName(name); 26 account.setMoney(money); 27 28 int count = accountService.add(account); 29 if (count == 1) 30 return "success!"; 31 return "fail"; 32 } 33 34 // localhost:8080/account/update/6?name=666&money=666 35 @RequestMapping(value = "/update/{id}", method = RequestMethod.GET) 36 public String update(// 37 @PathVariable("id") Integer id, // 38 @RequestParam(value = "name", required = true) String name, // 39 @RequestParam(value = "money", required = true) double money) { 40 41 Account account = accountService.findById(id); 42 account.setName(name); 43 account.setMoney(money); 44 45 int count = accountService.update(account); 46 if (count == 1) { 47 return account.toString(); 48 } 49 return "fail"; 50 } 51 52 @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) 53 public String deleteById(@PathVariable("id") Integer id) { 54 accountService.deleteById(id); 55 return "success"; 56 } 57 58 }
AppBoot.java
@SpringBootApplication
public class AppBoot { public static void main(String[] args) { SpringApplication.run(AppBoot.class, args); } }
================== 最后總結: ===================
(1) pom.xml / application.properties / 數據表
Account.java
JdbcTemplate
JPA( @Entity / @Id, @GenereatedValue, @Column )
Mybatis
IAccountDao.java
JdbcTemplate(AccountDaoImpl.java / @Repository, jdbcTemplate)
JPA( IAccountDao extends JpaRepository<Account, Integer> () { } )
Mybatis( IAccountMapper.java / @Mapper, @Select/@Insert/@Update/@Delete(sql) )
(2) 注解總結:
@Component / @Repository / @Service / @Controller / @RestController
@ResponseBody / @RequestMapping
@PathVariable / @RequestParam / @RequestBody
@Configuration / @ComponentScan / @EnableAutoConfiguration / @SpringBootApplication
// JPA
@EntityScan / @EnableJpaRepository /
// Mybatis
@MapperScan
// JdbcTemplate @SpringBootApplication // @Configuration + @ComponentScan(當前包) + @EnableAutoConfiguration(當前包) // JPA @EntityScan(basePackages = {"cn.mayi.springboot.jpa.entity"}) @EnableJpaRepositories(basePackages = {"cn.mayi.springboot.jpa.dao"}) //Mybatis @MapperScan(basePackages = {"cn.mayi.springboot.mybatis.dao"}) public class AppBoot { public static void main(String[] args) { SpringApplication.run(AppBoot.class, args); } }