MyBatis整合(SpringBoot)

  1. 导入依赖
              <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.2</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.22</version>
            </dependency>

     

  2. 创建数据库访问层(Mapper)
    @Mapper
    public interface BookMapper {
        int addBook(Book book);
        int deleteBookById(Integer id);
        int updateBookById(Book book);
        Book getBookById(Integer id);
        List<Book> getAllBooks();
    }

     

  3. 创建对应的Mapper.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.batac.summer.Mapper.BookMapper">
        <insert id="addBook" parameterType="com.batac.summer.bean.Book">
            insert into book (name, author) values (#{name},#{author})
        </insert>
    
        <delete id="deleteBookById" parameterType="int">
            delete from book where id=#{id}
        </delete>
        <update id="updateBookById" parameterType="com.batac.summer.bean.Book">
            update book set name=#{name},author=#{author} where id=#{id}
        </update>
    
        <select id="getBookById" parameterType="int" resultType="com.batac.summer.bean.Book">
            select * from book where id=#{id}
        </select>
        <select id="getAllBooks" resultType="com.batac.summer.bean.Book">
            select * from book
        </select>
    </mapper>

     

  4. service与controller测试
    @Service
    public class BookService {
    
    //    @Autowired
    //    BookDao bookDao;
    
        @Autowired
        private  BookMapper bookMapper;
    
        public int addBoot(Book book){
            return bookMapper.addBook(book);
        }
    
        public int updateBook(Book book){
            return bookMapper.updateBookById(book);
        }
    
        public int deleteBookById(Integer id){
            return bookMapper.deleteBookById(id);
        }
    
        public Book getBookByID(Integer id){
            return bookMapper.getBookById(id);
        }
    
        public List<Book>getAllBooks(){
            return bookMapper.getAllBooks();
        }
    
    }
    @RestController
    public class BookController {
        @Autowired
        BookService bookService;
    
       @GetMapping("/bookOps")
        public void  bookOps(){
            Book b1 = new Book();
            b1.setName("西厢记");
            b1.setAuthor("王师傅");
            int i = bookService.addBoot(b1);
           System.out.println("addBoot>>>"+i);
    
           Book b2 = new Book();
           b2.setId(1);
           b2.setName("朝花夕拾");
           b2.setAuthor("鲁迅");
           int i2=bookService.updateBook(b2);
           System.out.println("updateBook>>>"+i2);
    
           Book b3=bookService.getBookByID(1);
           System.out.println("getBookByID>>>"+b3);
    
           List<Book>list = bookService.getAllBooks();
           System.out.println("getAllBooks>>>"+list);
        }
        
    }

     

在Maven工程中, XML配置文件建议卸载resources目录下, 但是上文中的Mapper.xml文件写在了包下, Maven在运行时会忽略包下的XML文件, 因此需要在pom.xml文件中重新指明资源文件位置, 配置如下:

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

 注意: 有两种方式指明该类是一个Mapper: 第一种是如前面的代码所示, 在BookMapper上添加@Mapper注解, 表明该接口是一个MyBatis中的Mapper, 这种方式要在每一个Mapper上都添加注解, 还有一种简单的方式是在配置类上添加MapperScan("com.batac.summer.mapper")注解, 表示扫描com.batac.summer.mapper包下所有的接口作为Mapper, 这样就不用在每个接口上配置@Mapper注解了。

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值