SpringBoot整合ssm項目實例
·1. 模塊創建
創建一個項目,創建時勾選springweb和mysql driver,
項目創建好后導入mybatis-plus和druid
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
配置好端口等
2. 實體類開發
本處會使用lombok(一個java類庫,提供一組注解,簡化pojo實體類開發)
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
實體類:
@Data//lombok提供的其中包括get set toString方法等,lombok還提供其他方法具體自行查看
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
3. 數據層開發
基礎功能
技術方案:
- MyBatisPlus
- Druid
配置:
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/jdbcstudy?serverTimezone=GMT
username: root
password: 123456
dao層:
@Repository
@Mapper
public interface BookDao extends BaseMapper<Book> {
}
測試:
@SpringBootTest
class DemoApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetById() {
System.out.println(bookDao.selectById(1));
}
@Test
void testUpDate() {
Book book = new Book();
book.setId(4);
book.setType("測試數據aaa");
book.setDescription("測試數據123");
book.setName("測試數據123");
bookDao.updateById(book);
}
@Test
void testSave() {
Book book = new Book();
book.setType("測試數據123");
book.setDescription("測試數據123");
book.setName("測試數據123");
bookDao.insert(book);
}
@Test
void testDelete() {
bookDao.deleteById(4);
}
@Test
void testGetAll() {
System.out.println(bookDao.selectList(null));
}
}
如果想看到mybait-plus的運行日志不要每次只有結果:
在springboot的配置文件中的mybait-plus配置項中添加:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
分頁查詢功能
// 分頁查詢
@Test
void testGetPage() {
// 參數一:當前數據,參數二:每一頁大小
IPage iPage = new Page(1,2);
bookDao.selectPage(iPage,null);
}
除此之外還需要一個MybatisPlus分頁攔截器:
//表示是一個配置需要加載
@Configuration
public class MPConfig {
// 實例化攔截器
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// PaginationInnerInterceptor()是用來分頁的攔截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}

按條件查詢
@Test
void testGetBy() {
// 方法一:
String name = "Spring";
QueryWrapper<Book> qw = new QueryWrapper<>();
// 查詢name like %spring%的值,第一個參數是條件相當于if
qw.like(name != null,"name",name);
bookDao.selectList(qw);
// 方法二:(推薦)
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
lqw.like(name != null,Book::getName,"Spring");
bookDao.selectList(lqw);
}
4. 業務層開發
普通業務層開發
業務層接口:
public interface BookService {
Boolean save(Book book);
Boolean update(Book book);
Boolean delete(Integer id);
Book getByID(Integer id);
List<Book> getAll();
}
業務層實現類:
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public Boolean save(Book book) {
// bookDao.insert(book)返回值為影響的行數,
return bookDao.insert(book) >0;
}
@Override
public Boolean update(Book book) {
return bookDao.updateById(book)>0;
}
@Override
public Boolean delete(Integer id) {
return bookDao.deleteById(id)>0;
}
@Override
public Book getByID(Integer id) {
return bookDao.selectById(id);
}
@Override
public List<Book> getAll() {
return bookDao.selectList(null);
}
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
MyBatisPlus實現業務層的快速開發

業務層接口:
public interface IBookService extends IService<Book> {
}
業務層實現類:
//mybait-plus沒提供的方法也可以自己編輯添加
@Service
public class IBookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
}
測試:
//這些方法如getById(1)都是自帶的,其他沒列出來自行查看
@Autowired
private IBookService bookService;
@Test
void testGerById(){
System.out.println(bookService.getById(1));
}
@Test
void testUpDate() {
Book book = new Book();
bookService.updateById(book)
}
@Test
void testDelete() {
bookService.removeById(1);
}
5. 表現層開發
- 基于Restful進行表現層開發
- 使用Postman測試表現層接口功能
表現層開發
表現層功能類:
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
// 下面的方法都是mybaitplus提供的
//@RequestBody表示參數在請求體
//@PathVariable表示參數在url路徑
@GetMapping
public List<Book> getAll(){
return bookService.list();
}
@PostMapping
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book){
return bookService.updateById(book);
}
@DeleteMapping("{id}")
public Boolean update(@PathVariable Integer id){
return bookService.removeById(id);
}
@GetMapping("{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
//分頁 由于mybaitplus中的分頁方法不太方便,所以本處我們是自己寫的方法具體見下
@GetMapping("{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return bookService.getPage(currentPage,pageSize);
}
}
//分頁方法
@Service
public class IBookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
表現層消息格式統一
因為之前我們每個消息傳遞數據的格式都不一致很麻煩,所以要對其進行統一
所以我們要改成這種統一的格式:

具體方法就是設計一個表現層的模型類:
@Data //包含get、set tostring等方法
public class R {
private Boolean flag;
private Object data;
protected R(){}
public R(Boolean flag, Object data) {
this.flag = flag;
this.data = data;
}
public R(Boolean flag ){
this.flag=flag;
}
}
之后修改我們之前的表現層功能類:
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
// 下面的方法都是mybaitplus提供的
//@RequestBody表示參數在請求體
//@PathVariable表示參數在url路徑
@GetMapping
public R getAll(){
return new R(true,bookService.list());
}
@PostMapping
public R save(@RequestBody Book book){
return new R(bookService.save(book));
}
@PutMapping
public R update(@RequestBody Book book){
return new R(bookService.updateById(book));
}
@DeleteMapping("{id}")
public R update(@PathVariable Integer id){
return new R(bookService.removeById(id));
}
@GetMapping("{id}")
public R getById(@PathVariable Integer id){
return new R(true,bookService.getById(id));
}
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return new R(true,bookService.getPage(currentPage,pageSize));
}
}
之后就是一些前端的操作,就不再多寫了

浙公網安備 33010602011771號