SpringBoot整合持久层技术--(二)MyBatis
2020-02-20 16:01:34来源:博客园 阅读 ()
SpringBoot整合持久层技术--(二)MyBatis
简介:
原名iBatis,SpringBoot中使用MyBatis:
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2.数据库表,实体类以及配置文件与上一章JdbcTemplate那没有区别,
3.从下往上写,dao层:还是一样的,增删改查查询所有方法
@Mapper public interface BookMapper { int addBook(Book book); int deleteBookById(Integer id); int updateBookById(Book book); Book getBookById(Integer id); List<Book> getAllBooks(); }
<?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="org.sang.mapper.BookMapper">
<insert id="addBook" parameterType="org.sang.model.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="org.sang.model.Book"> UPDATE book set name=#{name},author=#{author} WHERE id=#{id} </update>
<select id="getBookById" parameterType="int" resultType="org.sang.model.Book"> SELECT * FROM book WHERE id=#{id} </select>
<select id="getAllBooks" resultType="org.sang.model.Book"> SELECT * FROM book </select> </mapper>
4.service:
@Service public class BookService { @Autowired BookMapper bookMapper; public int addBook(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(); } }
5.controller:
@RestController public class BookController { @Autowired BookService bookService; @GetMapping("/bookOps") public void bookOps() { Book b1 = new Book(); b1.setName("西厢记"); b1.setAuthor("王实甫"); int i = bookService.addBook(b1); System.out.println("addBook>>>" + i); Book b2 = new Book(); b2.setId(1); b2.setName("朝花夕拾"); b2.setAuthor("鲁迅"); int updateBook = bookService.updateBook(b2); System.out.println("updateBook>>>"+updateBook); Book b3 = bookService.getBookById(1); System.out.println("getBookById>>>"+b3); int delete = bookService.deleteBookById(2); System.out.println("deleteBookById>>>"+delete); List<Book> allBooks = bookService.getAllBooks(); System.out.println("getAllBooks>>>"+allBooks); } }
http://localhost:8080/bookOps
原文链接:https://www.cnblogs.com/crazy-lc/p/12336710.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Java虚拟机——JVM
下一篇:集合与容器类
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
- nacos~配置中心功能~springboot的支持 2020-06-10
- SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash