MyBatis 是一款優秀的、開源的持久層框架,它支持定製化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎全部的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可使用簡單的 XML 或註解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。
這就代表,MyBatis是SQL映射框架(SQL mapping framework),和Hibernate工做原理並不相同,由於Hibernate是ORM 框架。
MyBatis社區已經爲Spring Boot提供了starter依賴,由於咱們能夠在Spring Boot中愉快地使用MyBatis.
本次介紹將不使用MyBatis XML來執行數據庫查詢,而是使用基於註釋的mappers(annotation-based mappers).java
由於本次演示的是如何在Spring Boot中使用MyBatis操做MySQL,進行數據庫的增刪改查。因此,咱們須要對MySQL數據庫作一些準備工做。具體說來,就說在MySQL的test數據庫下新建表格person,其中的字段爲id,name,age,city,id爲自增加的主鍵。mysql
use test create table person( id int primary key auto_increment, name varchar(20), age int, city varchar(20) );
接着在 http://start.spring.io/ 中建立Spring Boot項目,加入Web, MyBatis, MySQL起始依賴,以下圖:web
整個項目的結構以下圖:spring
畫紅線的框內的文件是咱們須要新增或修改的文件。
先是實體類Person.java,其代碼以下:sql
package com.hello.MyBatisDemo.domain; public class Person{ private Integer id; private String name; private Integer age; private String city; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
接着是基於註釋的mappers文件PersonMapper.java,其代碼以下:數據庫
package com.hello.MyBatisDemo.DAO; import com.hello.MyBatisDemo.domain.Person; import org.apache.ibatis.annotations.*; import java.util.List; @Mapper public interface PersonMapper { /** * 添加操做,返回新增元素的 ID * @param person */ @Insert("insert into person(id,name,age,city) values(#{id},#{name},#{age},#{city})") @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id") void insert(Person person); /** * 更新操做 * @param person * @return 受影響的行數 */ @Update("update person set name=#{name},age=#{age},city=#{city} where id=#{id}") Integer update(Person person); /** * 刪除操做 * @param id * @return 受影響的行數 */ @Delete("delete from person where id=#{id}") Integer delete(@Param("id") Integer id); /** * 查詢全部 * @return */ @Select("select id,name,age,city from person") List<Person> selectAll(); /** * 根據主鍵查詢單個 * @param id * @return */ @Select("select id,name,age,city from person where id=#{id}") Person selectById(@Param("id") Integer id); }
下一步是控制文件PersonController.java,其代碼以下:apache
package com.hello.MyBatisDemo.Controller; import com.hello.MyBatisDemo.DAO.PersonMapper; import com.hello.MyBatisDemo.domain.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class PersonController { @Autowired private PersonMapper personMapper; @RequestMapping("/insert") public String insert(@RequestParam String name, @RequestParam String city, @RequestParam Integer age) { Person person = new Person(); person.setName(name); person.setAge(age); person.setCity(city); personMapper.insert(person); return "第"+person.getId()+"條記錄插入成功!"; } @RequestMapping("/update") public String update(@RequestParam Integer id, @RequestParam String name, @RequestParam String city, @RequestParam Integer age) { Person person = new Person(); person.setId(id); person.setName(name); person.setAge(age); person.setCity(city); return personMapper.update(person)+"條記錄更新成功!"; } @RequestMapping("/delete") public String delete(@RequestParam Integer id) { return personMapper.delete(id)+"條記錄刪除成功!"; } @RequestMapping("/selectById") public Person selectById(@RequestParam Integer id) { return personMapper.selectById(id); } @RequestMapping("/selectAll") public List<Person> selectAll() { return personMapper.selectAll(); } }
最後一步是整個項目的配置文件application.properies,主要是MySQL數據庫的鏈接設置,其代碼以下:瀏覽器
spring.datasource.url=jdbc:mysql://localhost:33061/test?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=147369 spring.datasource.driver-class-name=com.mysql.jdbc.Driver server.port=8100
整個項目的結構及代碼介紹完畢。app
啓動Spring Boot項目,在瀏覽器中輸入: http://localhost:8100/insert?name=bob&age=14&city=shanghai 便可插入一條新的記錄。如此相似地插入如下三條記錄:框架
在瀏覽器中輸入: http://localhost:8100/update?id=2&name=tian&age=27&city=shanghai 便可更新id=2的記錄
在瀏覽器中輸入: http://localhost:8100/delete?id=3 便可刪除id=3的記錄。
在瀏覽器中輸入: http://localhost:8100/selectById?id=2 便可查詢id=2的記錄。
在瀏覽器中輸入: http://localhost:8100/selectAll 便可查詢全部的記錄。 本次分享到此結束,接下來還會繼續分享更多的關於Spring Boot的內容,歡迎你們交流~~