Springboot-JPA(增删改查)


(1) pom.xml

    <!-- -starter-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <!-- -starter-web -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- -starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- -configuration-processor -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

 

(2) application.properties

### 数据库参数(JdbcTemplate)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:///springboot_jpa spring.datasource.username=root spring.datasource.password=root

 

(3) 数据表

DROP TABLE IF EXISTS `account`;

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 (NULL, 'aaa', '1000');
INSERT INTO `account` VALUES (NULL, 'bbb', '2000');
INSERT INTO `account` VALUES (NULL, 'ccc', '3000');

SELECT * FROM Account;

 

(4)  Account.java

@Entity
public class Account { @Id @GeneratedValue private int id; private String name; private double money; // get / set 方法省略 ... ...  @Override public String toString() { return "ID: " + id + ", Name: " + name + ", Money: " + money; } 
}

 

     IAccountDao.java

public interface IAccountDao extends JpaRepository<Account, Integer> {
}

 

     IAccountService.java

public interface IAccountService {
    
    List<Account> findAll(); Account findById(Integer id); void deleteById(Integer id); void update(Account account); void add(Account account); }

 

      AccountServiceImpl.java

@Service
@Transactional
public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; public List<Account> findAll() { return accountDao.findAll(); } public Account findById(Integer id) { return accountDao.findOne(id); } public void deleteById(Integer id) { accountDao.delete(id); } public void update(Account account) { accountDao.saveAndFlush(account); } public void add(Account account) { accountDao.saveAndFlush(account); } }

 

    AccountController.java

@RestController
@RequestMapping("/account") public class AccountController { @Autowired private IAccountService accountService; @RequestMapping(value = "/list", method = RequestMethod.GET) public List<Account> findAccounts() { return accountService.findAll(); } @RequestMapping(value = "/findone/{idd}", method = RequestMethod.GET) public Account findById(@PathVariable("idd") Integer id) { return accountService.findById(id); } @RequestMapping(value = "/delete/{id}") public String deleteById(@PathVariable("id") Integer id) { accountService.deleteById(id); return "SUCCESS!"; } @RequestMapping(value = "/update") public String update(// @RequestParam(value = "id") Integer id, // @RequestParam(value = "name", required = true) String name, // @RequestParam(value = "money", required = true) double money) { Account account = accountService.findById(id); account.setName(name); account.setMoney(money); accountService.update(account); return "SUCCESS!"; } @RequestMapping(value = "/add") public String add(// @RequestParam(value = "name", required = true) String name, // @RequestParam(value = "money", required = true) double money) { Account account = new Account(); account.setName(name); account.setMoney(money); accountService.update(account); return "SUCCESS!"; } }

  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);
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM