MyBatis(二)-----註解方式crud

1、數據庫建表語句java

DROP TABLE IF EXISTS `m_book`;
CREATE TABLE `m_book` (
  `id` varchar(255) NOT NULL,
  `bname` varchar(255) DEFAULT NULL,
  `btime` datetime DEFAULT NULL,
  `price` double DEFAULT NULL,
  PRIMARY KEY (`id`)
)

2、sql

1dao層接口(注:mybatis默認返回int數據庫

package com.test.dao;

import com.test.model.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface IBookDao {

    @Insert("insert into m_book(id,bname,btime,price) values(#{book_id},#{book_name},#{book_time},#{book_price})")
    public Integer addBook(Book book);

    @Delete("delete from m_book where id=#{book_id}")
    public Integer delBook(String id);

    @Update("update m_book set bname=#book_name},btime=#{book_time},price=#{book_price} where id=#{book_id}")
    public Integer updateBook(Book Book);


    @Select("select * from m_book")
    public List getAllBooK();

}

2設置掃描你寫的接口與註解apache

<mappers>

    <mapper class="com.test.dao.IBookDao"/>
</mappers>

 

其餘省略mybatis

相關文章
相關標籤/搜索