图书借阅管理系统数据持久层实现—MyBatis的增删改查预习内容
时间: 2024-06-13 15:07:04 浏览: 16
以下是使用MyBatis实现图书借阅管理系统数据持久层的增删改查预习内容:
1. 首先需要在pom.xml文件中添加MyBatis和MySQL的依赖:
```xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
```
2. 在application.properties文件中配置MySQL数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
3. 创建Book实体类和BookMapper接口:
```java
public class Book {
private Integer id;
private String name;
private String author;
private String publisher;
// 省略getter和setter方法
}
@Mapper
public interface BookMapper {
List<Book> findAll();
Book findById(Integer id);
void save(Book book);
void update(Book book);
void deleteById(Integer id);
}
```
4. 创建BookMapper.xml文件,编写SQL语句:
```xml
<mapper namespace="com.example.demo.mapper.BookMapper">
<select id="findAll" resultType="com.example.demo.entity.Book">
select * from book
</select>
<select id="findById" parameterType="int" resultType="com.example.demo.entity.Book">
select * from book where id = #{id}
</select>
<insert id="save" parameterType="com.example.demo.entity.Book">
insert into book(name, author, publisher) values(#{name}, #{author}, #{publisher})
</insert>
<update id="update" parameterType="com.example.demo.entity.Book">
update book set name=#{name}, author=#{author}, publisher=#{publisher} where id=#{id}
</update>
<delete id="deleteById" parameterType="int">
delete from book where id=#{id}
</delete>
</mapper>
```
5. 在BookService中注入BookMapper,并编写增删改查方法:
```java
@Service
public class BookService {
@Autowired
private BookMapper bookMapper;
public List<Book> findAll() {
return bookMapper.findAll();
}
public Book findById(Integer id) {
return bookMapper.findById(id);
}
public void save(Book book) {
bookMapper.save(book);
}
public void update(Book book) {
bookMapper.update(book);
}
public void deleteById(Integer id) {
bookMapper.deleteById(id);
}
}
```
以上就是使用MyBatis实现图书借阅管理系统数据持久层的增删改查预习内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)