1.新建maven项目
2.在pom.xml文件中引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
</parent>
<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>
<scope>test</scope>54
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
3.配置项目配置文件和包如图所示
项目结构如图:
4.配置配置文件
Main:
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
}
}
application.yml:
spring:
profiles:
active: dev
application-dev.yml:
server:
port: 8090
servlet:
context-path: /ssm
spring:
datasource:
druid:
username: root
password: 123
url: jdbc:mysql://192.168.0.199:3306/ssm_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.dyit.ssm.entity
mapper-locations: classpath:mapper/*.mapper.xml
5.写数据库表的实体类,mapper,dto
Book:
@Alias("Book")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
private Integer id;
private String title;
private String author;
private Double price;
private String category;
}
HttpResp:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HttpResp {
private int code;
private String msg;
private Object results;
private LocalDateTime time;
}
IBookMapper:
@Mapper
public interface IBookMapper {
List<Book> findAll();
}
6.配置mapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yang.ssm.mapper.IBookMapper">
<sql id="findAllBook">
SELECT * FROM book_tab
</sql>
<resultMap type="Book" id="BookMap">
<id property="id" column="book_id" />
<result property="title" column="book_title" />
<result property="price" column="book_price" />
<result property="category" column="book_category" />
<result property="author" column="book_author" />
</resultMap>
<select id="findAll" resultMap="BookMap">
<include refid="findAllBook" />
</select>
</mapper>
7.写service测试
IBookService:
public interface IBookService {
List<Book> findAll();
}
BookServiceImpl:
@Service
@Transactional
public class BookServiceImpl implements IBookService {
@Autowired
private IBookMapper mapper;
@Override
public List<Book> findAll() {
return mapper.findAll();
}
}
8.写controller进行接口测试
@CrossOrigin
@RestController
@RequestMapping("/api/book")
public class BookController {
@Autowired
private IBookService ibs;
@GetMapping("/findAll")
public HttpResp findAll(){
List<Book> list =ibs.findAll();
return new HttpResp(2001,"查询成功",list,null);
}
}
9.运行Main,进行接口测试
|