SSM框架整合-03-mybatis层配置
2022/1/7 23:35:22
本文主要是介绍SSM框架整合-03-mybatis层配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.配置mybatis-config.xml
在资源目录resources中创建mybatis-config.xml文件,并引入dtd约束
<?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> </configuration>
2.数据配置文件database.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm-test?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=123456
3.IDEA关联mysql数据库
IDEA关联MySQL数据库_w506667090的博客-CSDN博客
4.编写数据库对应的实体类
并使用lombok自动生成getter&setter方法和有参无参构造器,前提是IDEA中添加了lombok插件,如果没有可以下载lombok插件或手动生成相关方法。
package com.wanjl.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** @Data 注在类上,提供类的get、set、equals、hashCode、canEqual、toString方法 @AllArgsConstructor 注在类上,提供类的全参构造 @NoArgsConstructor 注在类上,提供类的无参构造 */ @Data @AllArgsConstructor @NoArgsConstructor public class Books { private int bookID; private String bookName; private int bookCounts; private String detail; }
5.编写dao层的mapper接口
package com.wanjl.dao; import com.wanjl.pojo.Books; import java.util.List; public interface BookMapper { //添加图书 int addBook(Books books); //修改图书 int updateBook(Books books); //删除图书 int deleteBookById(int id); //查询一本图书 Books queryBookById(int id); //查询所有图书 List<Books> queryAllBook(); }
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.wanjl.dao.BookMapper"> <!--添加图书--> <insert id="addBook" parameterType="Books"> insert into `ssm-test`.books(bookName, bookCounts, detail) values (${bookName},${bookCounts},${detail}); </insert> <!--修改图书--> <update id="updateBook" parameterType="Books"> update `ssm-test`.books set bookName=${bookName},bookCounts=${bookCounts},detail=${detail} where bookID=${bookID}; </update> <!--删除图书--> <delete id="deleteBookById" parameterType="int"> delete from `ssm-test`.books where bookID=${bookID}; </delete> <!--查询一本图书--> <select id="queryBookById" resultType="Books"> select * from `ssm-test`.books where bookID=${bookID}; </select> <!--查询所有图书信息--> <select id="queryAllBook" resultType="Books"> select * from `ssm-test`.books; </select> </mapper>
7.编写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.wanjl.pojo"/> </typeAliases> <mappers> <mapper class="com.wanjl.dao.BookMapper"/> </mappers> </configuration>
8.编写service层的接口和实现类
service接口
package com.wanjl.service; import com.wanjl.pojo.Books; import java.util.List; public interface BookService { //添加图书 int addBook(Books books); //修改图书 int updateBook(Books books); //删除图书 int deleteBookById(int id); //查询一本图书 Books queryBookById(int id); //查询所有图书 List<Books> queryAllBook(); }
service实现类
package com.wanjl.service; import com.wanjl.dao.BookMapper; import com.wanjl.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 updateBook(Books books) { return bookMapper.updateBook(books); } @Override public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } @Override public Books queryBookById(int id) { return bookMapper.queryBookById(id); } @Override public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } }
这篇关于SSM框架整合-03-mybatis层配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-26JavaScript入门教程:从零开始学习JavaScript编程
- 2024-12-26JavaScript入门教程:从零开始学习JavaScript
- 2024-12-26JS编程入门指南:从零开始学习JavaScript
- 2024-12-25Java编程面试题详解与解答
- 2024-12-25TS基础知识详解:初学者必看教程
- 2024-12-252024面试题解析与攻略:从零开始的面试准备指南
- 2024-12-25数据结构与算法学习:新手入门教程
- 2024-12-25初学者必备:订单系统资料详解与实操教程
- 2024-12-24内网穿透资料入门教程
- 2024-12-24微服务资料入门指南