回顾
我的环境
环境:
- IDEA
- MySQL 5.7.34
- Tomcat 8
- Maven 3.8.3
要求:
需要熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识;
Mybatis层编写
1、数据库配置文件 database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=****
jdbc.password=********
2、IDEA关联数据库
3、编写MyBatis的核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.shan.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/shan/dao/BookMapper.xml"/>
</mappers>
</configuration>
4、编写数据库对应的实体类 com.shan.pojo.Books
使用lombok插件!
package com.shan.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data @NoArgsConstructor @AllArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
5、编写Dao层的 Mapper接口!
package com.shan.dao;
import com.shan.pojo.Books;
import java.util.List;
public interface BookMapper {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBook(int bookID);
//更新一本书
int updateBook(Books books);
//通过id查询一本书
Books queryBookById(int bookID);
//查询所有书
List<Books> queryAllBook();
//通过书籍名称查询书籍
List<Books> queryBookByName(String bookName);
}
6、编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shan.dao.BookMapper">
<insert id="addBook" parameterType="books">
insert into ssmbuild.books (bookID, bookName, bookCounts, detail) VALUES (#{bookID}, #{bookName}, #{bookCounts}, #{detail})
</insert>
<delete id="deleteBook" parameterType="int">
delete from ssmbuild.books where bookID = #{bookID}
</delete>
<update id="updateBook" parameterType="books">
update ssmbuild.books
set bookName=#{bookName}, bookCounts=#{bookCounts}, detail=#{detail}
where bookID=#{bookID}
</update>
<select id="queryBookById" resultType="books">
select * from ssmbuild.books where bookID = #{bookID}
</select>
<select id="queryAllBook" resultType="books">
select * from ssmbuild.books
</select>
<select id="queryBookByName" resultType="books">
select * from ssmbuild.books where bookName = #{bookName}
</select>
</mapper>
7、编写Service层的接口和实现类
接口:
package com.shan.service;
import com.shan.pojo.Books;
import java.util.List;
//BookService:底下需要去实现,调用dao层
public interface BookService {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBook(int bookID);
//更新一本书
int updateBook(Books books);
//通过id查询一本书
Books queryBookById(int bookID);
//查询所有书
List<Books> queryAllBook();
//通过书籍名称查询书籍
List<Books> queryBookByName(String bookName);
}
实现类:
package com.shan.service;
import com.shan.dao.BookMapper;
import com.shan.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService{
//调用dao层的操作,设置一个set接口,方便Spring管理
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
@Override
public int addBook(Books books) {
return bookMapper.addBook(books);
}
@Override
public int deleteBook(int bookID) {
return bookMapper.deleteBook(bookID);
}
@Override
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
@Override
public Books queryBookById(int bookID) {
return bookMapper.queryBookById(bookID);
}
@Override
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
@Override
public List<Books> queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}
}
到此Mybatis层编写完成!
作者有话说
博客创作不易,希望看到这里的读者动动你的小手点个赞,如果喜欢的小伙伴可以一键三连,作者大大在这里给大家谢谢了。