一、步骤
1.创建一个普通maven工程
2.选择springboot版本
3.整合springmvc、mybatis用到的包、启动器
4.编写配置文件
5.编写实体类,配置文件中加入别名
6.定义mapper接口
7.定义mapper.xml映射文件
8.配置文件中加入映射文件位置
9.定义启动类,在启动类加入mapper的包扫描
10.编写service层
11.开发一个Controller
12.运行
二、实操
1.创建一个普通maven工程
2.选择springboot版本
<?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>
<groupId>com.zxs</groupId>
<artifactId>TestSpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- SpringBoot -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3.整合springmvc、mybatis用到的包、启动器
<?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>
<groupId>com.zxs</groupId>
<artifactId>TestSpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- SpringBoot -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.6</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
</project>
4.编写配置文件(application)
server:
port: 8888 #端口号
servlet:
context-path: /springboot
spring:
datasource:
url: jdbc:mysql://localhost:3306/book_store?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
5.编写实体类,配置文件中加入别名
package com.zxs.pojo;
public class Book {
private int id;
private String name;
private String author;
private double price;
public Book() {
}
public Book(int id, String name, String author, double price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
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 getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
mybatis:
type-aliases-package: com.zxs.pojo #实体类
6.定义mapper接口
package com.zxs.mapper;
import java.util.List;
public interface BookMapper {
List selectAll();
}
7.定义mapper.xml映射文件
在resource下新建mybatis文件夹,mapper.xml文件名没有要求了,不需要和接口名完全对应了,是根据namespace去找接口。但是最好还是和接口名保持一致
<?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.zxs.mapper.BookMapper">
<select id="selectAll" resultType="book">
select * from t_book
</select>
</mapper>
8.配置文件中加入映射文件位置
mybatis:
mapper-locations: classpath: mybatis/*.xml #映射文件
9.定义启动类,在启动类加入mapper的包扫描
package com.zxs;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.zxs.mapper")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
10.编写service层
package com.zxs.service;
import java.util.List;
public interface BookService {
List findAll();
}
package com.zxs.service.impl;
import com.zxs.mapper.BookMapper;
import com.zxs.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 BookMapper bookMapper;
public List findAll() {
return bookMapper.selectAll();
}
}
11.开发一个Controller
package com.zxs.controller;
import com.zxs.pojo.Book;
import com.zxs.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("findAll")
@ResponseBody //将java数据转为json类型
public String findAll(){
List books=bookService.findAll();
String str="";
for (int i = 0; i < books.size(); i++) {
Book book=(Book) books.get(i);
str=str+book.getName()+"\t";
str=str+book.getAuthor()+"\n";
}
return str;
}
}
12.运行