springboot学习

一、快速开始

首先从一个最基本的 REST 接口开始:

@RestController
@RequestMapping("/books")
public class BookController {
    
    @GetMapping
    public String getById() {
        return "spring boot is running...";
    }
}

启动类配置:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

二、配置文件使用

SpringBoot支持 yml 和 properties 两种格式的配置文件。以下是一个典型的配置示例:

server:
  port: 8080 

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root //数据库
    password: 123456

# 自定义配置项
book:
  name: SpringBoot实战
  price: 100

读取配置的方式:

@RestController
@RequestMapping("/books")
public class BookController {
    
    @Value("${book.name}")
    private String name;
    
    @GetMapping
    public String getBook() {
        return "book name is " + name;
    }
}

三、数据库访问

使用 JPA 进行数据库操作:

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private Double price;
    // getter setter 省略
}

数据访问层:

@Repository
public interface BookRepository extends JpaRepository<Book, Integer> {
    List<Book> findByNameLike(String name);
}

四、单元测试

服务层测试示例:

@SpringBootTest
class BookServiceTest {
    
    @Autowired
    private BookService bookService;
    
    @Test
    void testGetById() {
        Book book = bookService.getById(1);
        assertNotNull(book);
        assertEquals("SpringBoot实战", book.getName());
    }
}

五、SSM整合案例

6.1 数据访问层(DAO)

使用 MyBatis 注解方式实现数据库操作:

@Mapper
public interface BookDao {
    // 新增图书
    @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
    public int save(Book book);

    // 更新图书信息
    @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
    public int update(Book book);

    // 删除图书
    @Delete("delete from tbl_book where id = #{id}")
    public int delete(Integer id);

    // 根据ID查询
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);

    // 查询所有
    @Select("select * from tbl_book")
    public List<Book> getAll();
}

6.2 服务层实现

基于 DAO 层封装业务逻辑:

@Service
public class BookServiceImpl implements BookService {
    
    @Autowired
    private BookDao bookDao;
    
    @Override
    public boolean save(Book book) {
        return bookDao.save(book) > 0;
    }
    
    @Override
    public Book getById(Integer id) {
        return bookDao.getById(id);
    }
    
    @Override
    public List<Book> getAll() {
        return bookDao.getAll();
    }
    
    @Override
    public boolean update(Book book) {
        return bookDao.update(book) > 0;
    }
    
    @Override
    public boolean delete(Integer id) {
        return bookDao.delete(id) > 0;
    }
}

6.3 控制层实现

@RestController
@RequestMapping("/books")
public class BookController {
    
    @Autowired
    private BookService bookService;
    
    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean flag = bookService.save(book);
        return new Result(flag, flag ? "添加成功" : "添加失败");
    }
    
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
        return new Result(true, book);
    }
    
    @GetMapping
    public Result getAll() {
        List<Book> books = bookService.getAll();
        return new Result(true, books);
    }
}


springboot学习
http://localhost:8090//archives/springbootxue-xi
作者
潘显镇
发布于
2025年03月03日
许可协议