mybatis層編寫完畢後的項目目錄 1.右鍵SpringMVC2項目-》new-》Modual-》選擇maven項目(我的項目名為Study09_ssm),輸入模塊名,點擊Finish 2.第二部的操作就是將idea的基本運行環境搞定,包括:添加web支持,配置tomcat,配置project s ...
mybatis層編寫完畢後的項目目錄
1.右鍵SpringMVC2項目-》new-》Modual-》選擇maven項目(我的項目名為Study09_ssm),輸入模塊名,點擊Finish
2.第二部的操作就是將idea的基本運行環境搞定,包括:添加web支持,配置tomcat,配置project structure
2.1添加web支持:右鍵項目名為Study09_ssm,選擇Add Framework Support,勾選Web Application,點擊ok
2.2配置tomcat(註意:如果第一步沒有配置的話,tomcat裡邊不會出現這個項目的)
2.3配置project structure,選擇idea菜單欄File->project structure->Artifact->找到自己的這個項目點擊->在WEB-INF下新建一個lib文件夾(預設是沒有的),然後點擊加號,選擇所有的依賴,點擊ok
3.新建一個資料庫命名為ssm_crud,新建一個查詢視窗,執行如下的sql代碼
USE `ssm_crud`; DROP TABLE IF EXISTS `books`; CREATE TABLE `books` ( `bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '書id', `bookName` VARCHAR(100) NOT NULL COMMENT '書名', `bookCounts` INT(11) NOT NULL COMMENT '數量', `detail` VARCHAR(200) NOT NULL COMMENT '描述', KEY `bookID` (`bookID`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES (1,'Java',1,'從入門到放棄'), (2,'MySQL',10,'從刪庫到跑路'), (3,'Linux',5,'從進門到進牢');
4.讓idea連接上MySql資料庫,具體步驟如下:找到ida右側的database選項,點擊加號-》data source-》mysql,打開data source and driver視窗,也就是第二張圖,根據步驟做完即可,完成後會出現第四張圖的樣子。
5.建立項目目錄結構,如下圖所示:Java文件夾下的四個包,web模塊不用創建
6.resources文件夾下創建下麵的三個文件
7.database.properties文件為資料庫連接串,mybatis-config.xml文件為mybatis配置,這裡只寫別名和mappers即可,其他的讓spring來做,至此mybatis層的resources文件夾下的內容搞定,applicationContext.xml是用來整合各層的配置文件的,這個最後配置。
jdbc.driver=com.mysql.jdbc.Driver #mysql8.0+的話,要加一個時區設置&serverTimezone=Asia/Shanghai jdbc.url=jdbc:mysql://localhost:3306/ssm_crud?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=CST jdbc.username=root jdbc.password=123456
<?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.xiaoma1.pojo"/> </typeAliases> <mappers> <mapper class="com.xiaoma1.dao.BookMapper"></mapper> </mappers> </configuration>
8.下麵來做Java文件夾下的東西,包括:實體類,dao層的mapper介面和mapper.xml配置文件,service層的介面和實現類(我們這裡主要實現對書籍的增刪改查)
8.1編寫實體類:屬性名和資料庫欄位名一一對應,這裡的get、set方法、有參無參構造通過三個註解來實現,分別是@Data、@NoArgsConstructor、@AllArgsConstructor
package com.xiaoma1.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * Author:xiaoma * FileName:Books * Data:2022/5/3 10:21 */ @Data @NoArgsConstructor @AllArgsConstructor public class Books { private int bookID; private String bookName; private int bookCounts; private String detail; }
8.2編寫dao層的mapper介面和mapper對應的xml文件,xml文件中編寫sql語句實現和資料庫的交互,下麵為xml配置文件代碼和介面代碼
<?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"> <!--綁定介面BookMapper--> <mapper namespace="com.xiaoma1.dao.BookMapper"> <!--//增加一本書--> <insert id="addBook" parameterType="Books"> insert into books (bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail}); </insert> <!--/刪除一本書--> <delete id="deleteBookById" parameterType="int"> delete from books where bookID=#{id}; </delete> <!--//更新一本書--> <update id="updateBook" parameterType="Books"> update books set bookName = #{bookName},bookCounts = #{bookCounts},detail=#{detail} where bookID = #{bookID}; </update> <!--//查詢一本書--> <select id="queryBookById" resultType="Books" parameterType="int"> select * from books where bookID=#{id}; </select> <!--//查詢全部的書--> <select id="queryAllBook" resultType="Books"> select * from books; </select> </mapper>
package com.xiaoma1.dao; import com.xiaoma1.pojo.Books; import java.util.List; /** * Author:xiaoma * FileName:BookMapper * Data:2022/5/3 10:23 */ public interface BookMapper { //增加一本書 int addBook(Books books); //刪除一本書 int deleteBookById(int id); //更新一本書 int updateBook(Books books); //根據id查詢一本書 Books queryBookById(int id); //查詢全部的書 List<Books> queryAllBook(); }
8.3編寫service層的介面和實現類,這裡service'層的介面和dao層的mapper介面是一樣的
package com.xiaoma1.service; import com.xiaoma1.pojo.Books; import java.util.List; /** * Author:xiaoma * FileName:BookService * Data:2022/5/3 10:45 */ public interface BookService { //增加一本書 int addBook(Books books); //刪除一本書 int deleteBookById(int id); //更新一本書 int updateBook(Books books); //根據id查詢一本書 Books queryBookById(int id); //查詢全部的書 List<Books> queryAllBook(); }
package com.xiaoma1.service; import com.xiaoma1.dao.BookMapper; import com.xiaoma1.pojo.Books; import java.util.List; /** * Author:xiaoma * FileName:BookServiceImpl * Data:2022/5/3 10:45 */ public class BookServiceImpl implements BookService{ //業務層調用dao層,組合dao public BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public int addBook(Books books) { return bookMapper.addBook(books); } @Override public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } @Override public int updateBook(Books books) { return bookMapper.updateBook(books); } @Override public Books queryBookById(int id) { return bookMapper.queryBookById(id); } @Override public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } }
至此mybatis層實現完畢!!!!!!!!!