- 准备一个叫 springboot-test 的原始的 maven 项目,(我用的maven3.6http://链接:https://pan.baidu.com/s/1iH6QLvpGnYX4g8LZSHmC7Q?pwd=1234 提取码:1234+jdk18+IDEA2023)
- 准备一个ssm数据库,里面创建一个book表用来增删改查。
- 第一步,配置pom.xml文件,引入依赖的包。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.0</version>
</parent>
<groupId>com.itheima</groupId>
<artifactId>springboot-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 第二步,在resources写application.yml配置文件,连接数据库
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&
username: root //这里写自己的mysql的账号和密码
password: admin123
type: com.alibaba.druid.pool.DruidDataSource
- 第三步,在com.itheima包下创建一个 Application 启动类。
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- 第四步,在com.itheima.bean包下创建一个 Book 实体类
package com.itheima.bean;
public class Book {
/**
* id INT ,-- 主键
* NAME VARCHAR(100) NOT NULL, -- 名字
* TYPE VARCHAR(100) NOT NULL, -- 类别
* description VARCHAR(100) NOT NULL -- 描述
*/
private int id;
private String name;
private String type;
private String description;
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", type='" + type + '\'' +
", description='" + description + '\'' +
'}';
}
}
- 第五步,在com.itheima.dao包下创建一个 BookDao 接口
package com.itheima.dao;
import com.itheima.bean.Book;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface BookDao {
//查询所有书籍信息
@Select("select * from book")
public List<Book> getAll();
//根据id 查询书籍信息
@Select("select * from book where id=#{id}")
public Book getById(int id);
//添加图书信息
@Insert("insert into book(name,type,description) values(#{name},#{type},#{description})")
public int addBook(Book book);
//更新图书信息
@Update("update book set name=#{name},type=#{type},description=#{description} where id=#{id}")
public int updateBook(Book book);
//根据id删除图书信息
@Delete("delete from book where id=#{id}")
public int deleteById(int id);
}
- 第六步,在com.itheima.service包下创建一个 BookService 接口
package com.itheima.service;
import com.itheima.bean.Book;
public interface BookService {
public void findAll();
Book getById(int id);
Boolean addBook(Book book);
Boolean updateBook(Book book);
public boolean deleteById(int id);
}
- 第七步,在com.itheima.service.impl包下创建一个 BookServiceImpl 实现类
package com.itheima.service.impl;
import com.itheima.bean.Book;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public void findAll() {
List<Book> all = bookDao.getAll();
System.out.println(all);
System.out.println("你查询了所有的图书信息");
}
@Override
public Book getById(int id){
return bookDao.getById(id);
}
@Override
public Boolean addBook(Book book){
if(bookDao.addBook(book)>0){
return true;
}
return false;
}
@Override
public Boolean updateBook(Book book){
if(bookDao.updateBook(book)>0){
return true;
}
return false;
}
@Override
public boolean deleteById(int id){
if(bookDao.deleteById(id)>0){
return true;
}else {
return false;
}
}
}
- 第八步,在test->java->com.itheima包下创建 BookTest 测试类
import com.itheima.Application;
import com.itheima.bean.Book;
import com.itheima.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = Application.class)
public class BookTest {
@Autowired
private BookService bookService;
@Test
public void testFindAll(){
bookService.findAll();
}
@Test
public void testGetById(){
Book book=bookService.getById(1);
System.out.println(book);
}
@Test
public void testAddBook(){
Book book=new Book();
book.setName("西游记");
book.setType("魔幻");
book.setDescription("1111");
Boolean aBoolean = bookService.addBook(book);
if(aBoolean){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
}
@Test
public void testUpdateBook(){
Book book=new Book();
book.setId(2);
book.setName("水浒传");
book.setType("魔幻");
book.setDescription("3333");
Boolean aBoolean = bookService.updateBook(book);
if(aBoolean){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
}
@Test
public void testDel(){
Boolean aBoolean = bookService.deleteById(3);
if(aBoolean){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
}
- 执行结果