目錄
??創(chuàng)建一個新的Spring Boot項目
??配置文件
??application.properties配置:
??創(chuàng)建實體類
??創(chuàng)建Mapper接口
??創(chuàng)建Mapper XML文件
??創(chuàng)建Service和Controller
??創(chuàng)建一個Controller類,用于處理HTTP請求和響應。在src/main/java目錄下創(chuàng)建一個名為“com.example.demo.controller”的包,并在其中創(chuàng)建一個名為“UserController”的Java類,代碼如下:
??運行應用程序
??創(chuàng)建一個新的Spring Boot項目
首先,我們需要在Maven中創(chuàng)建一個新的Spring Boot項目。在pom.xml中添加所需的依賴,包括Spring Boot Web、MyBatis、MySQL驅(qū)動等。具體的依賴項如下:
<dependencies>
<!-- Spring Boot Web依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- MySQL驅(qū)動依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
?
??配置文件
在application.properties或application.yml文件中配置應用程序,包括端口、數(shù)據(jù)庫連接信息等。
??application.properties配置:
# 服務端口號
server.port=8080
# MySQL相關(guān)配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MyBatis配置
mybatis.type-aliases-package=com.example.demo.entity
mybatis.mapper-locations=classpath:/mapper/*.xml
?
??創(chuàng)建實體類
我們需要定義一個實體類來映射數(shù)據(jù)庫中的表。在src/main/java目錄下創(chuàng)建一個名為“com.example.demo.entity”的包,并在其中創(chuàng)建一個名為“User”的Java類,代碼如下:
public class User {
private Long id;
private String name;
private int age;
// getter和setter方法
}
??創(chuàng)建Mapper接口
創(chuàng)建一個Mapper接口,用于定義訪問表的CRUD方法。在src/main/java目錄下創(chuàng)建一個名為“com.example.demo.mapper”的包,并在其中創(chuàng)建一個名為“UserMapper”的Java接口,代碼如下:
@Mapper
public interface UserMapper {
List<User> findAll();
User findById(Long id);
int addUser(User user);
int updateUser(User user);
int deleteUser(Long id);
}
在這里,我們使用了@Mapper注釋來標識這是一個Mapper接口。接口中定義了一些CRUD方法。
??創(chuàng)建Mapper XML文件
在src/main/resources目錄下創(chuàng)建一個名為“mapper”的文件夾,并在其中創(chuàng)建一個名為“UserMapper.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.example.demo.mapper.UserMapper">
<resultMap id="userResultMap" type="com.example.demo.entity.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</resultMap>
<select id="findAll" resultMap="userResultMap">
select id, name, age from user
</select>
<select id="findById" resultMap="userResultMap" parameterType="Long">
select id, name, age from user where id = #{id}
</select>
<insert id="addUser" parameterType="com.example.demo.entity.User" useGeneratedKeys="true" keyProperty="id">
insert into user(name, age) values(#{name}, #{age})
</insert>
<update id="updateUser" parameterType="com.example.demo.entity.User">
update user set name = #{name}, age = #{age} where id = #{id}
</update>
<delete id="deleteUser" parameterType="Long">
delete from user where id = #{id}
</delete>
</mapper>
?
??創(chuàng)建Service和Controller
創(chuàng)建一個Service類,用于實現(xiàn)業(yè)務邏輯。在src/main/java目錄下創(chuàng)建一個名為“com.example.demo.service”的包,并在其中創(chuàng)建一個名為“UserService”的Java類,代碼如下:
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> findAll() {
return userMapper.findAll();
}
public User findById(Long id) {
return userMapper.findById(id);
}
public int addUser(User user) {
return userMapper.addUser(user);
}
public int updateUser(User user) {
return userMapper.updateUser(user);
}
public int deleteUser(Long id) {
return userMapper.deleteUser(id);
}
}
在這里,我們使用了@Service注釋來標識這是一個Service類,使用@Autowired注釋來自動注入Mapper對象。
??創(chuàng)建一個Controller類,用于處理HTTP請求和響應。在src/main/java目錄下創(chuàng)建一個名為“com.example.demo.controller”的包,并在其中創(chuàng)建一個名為“UserController”的Java類,代碼如下:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/")
public List<User> findAll() {
return userService.findAll();
}
@GetMapping("/{id}")
public User findById(@PathVariable Long id) {
return userService.findById(id);
}
@PostMapping("/")
public void addUser(@RequestBody User user) {
userService.addUser(user);
}
@PutMapping("/")
public void updateUser(@RequestBody User user) {
userService.updateUser(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
}
}
在這里,我們使用了@RestController注釋來標識這是一個Controller類,使用@Autowired注釋來自動注入Service對象。使用@GetMapping、@PostMapping、@PutMapping和@DeleteMapping注釋來定義HTTP請求方法。
??運行應用程序
運行DemoApplication.java中的main方法,啟動應用程序。
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在這里,我們使用了@MapperScan注釋來掃描Mapper接口并將其注冊為MyBatis映射器。
在瀏覽器或Postman中輸入http://localhost:8080/user就是代碼訪問 列表接口文章來源:http://www.zghlxwxcb.cn/news/detail-502579.html
以上是一個簡單的Spring Boot + MyBatis + MySQL框架搭建的詳細過程示例,具體實現(xiàn)過程中還需要根據(jù)實際情況進行完善和調(diào)整文章來源地址http://www.zghlxwxcb.cn/news/detail-502579.html
到了這里,關(guān)于[Spring Boot + MyBatis + MySQL框架搭建]的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!