SpringBoot构建微服务实战 之 整合Mybatis
SpringBoot整合 Mybatis 主要有两种主流的方法,一是基于注解的整合,二是基于配置的整合。本章我们学习一下SpringBoot 基于注解整合 Mybatis。
新建Maven项目
添加依赖和修改pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sstps.springBoot</groupId> <artifactId>Martket</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <Maven.compiler.source>1.8</Maven.compiler.source> <Maven.compiler.target>1.8</Maven.compiler.target> </properties> <dependencyManagement> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.8.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot starts --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- SpringBoot ends --> <!-- mysql starts --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- mysql ends --> <!-- mybatis starts --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- mybatis ends --> <!-- junit starts --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- junit ends --> </dependencies> </project>
配置数据源 application.properties
spring.datasource.url=jdbc:mysql:///sstps_product?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=bai5331359 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
建库建表 product.sql
create database sstps_product default charset uft8; create table products(pid int not null primary key auto_increment,pname varchar(200),type varchar(50),price double,createTime timestamp)
新建Product 实体 Product.java
package com.sstps.market.entity; import java.security.Timestamp; import org.springframework.stereotype.Component; @Component public class Product { private Integer pid; private String pname; private String type; private Double price; private Timestamp createTime; public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Timestamp getCreateTime() { return createTime; } public void setCreateTime(Timestamp createTime) { this.createTime = createTime; } @Override public String toString() { return "Product [pid=" + pid + ", pname=" + pname + ", type=" + type + ", price=" + price + ", createTime=" + createTime + "]"; } }
新建Mapper ProductMapper.java
package com.sstps.market.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.sstps.market.entity.Product; @Mapper public interface ProductMapper { @Insert("insert into products (pname,type,price) values(#{pname},#{type},#{price})") public Integer addProduct(Product product); @Delete("delete from products where pid =#{arg1}") public Integer deleteById(int pid); @Update("update products set pname=#{pname},type=#{type},price=#{price} where pid=#{pid}") public Integer updateProduct(Product product); @Select("select * from products where pid =#{arg1}") public Product getProductById(Integer id); @Select("select * from products oredr by pid desc") public List<Product> queryProductByList(); }
代码解读:
@Mapper MyBatis 集成SpringBoot 注解 使用该注解Spring 会为Mybaits生成相应实体类。@Insert/@update/@Delete/@Select:MyBatis Mapper接口注解。
App.java
package com.sstps.market; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.transaction.annotation.Transactional; import com.sstps.market.entity.Product; import com.sstps.market.mapper.ProductMapper; @SpringBootApplication public class App { @Transactional public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); ProductMapper productMapper = context.getBean(ProductMapper.class); Product product = context.getBean(Product.class); product.setPname("SpringBootDemo"); product.setType("LearningMatires"); product.setPrice(105.00); productMapper.addProduct(product); context.close(); } }
结果
小结
SpringBoot 整合Mybatis 需要相应的jar 包,具体请看 pom.xml 代码和注解。同时我们也许知道 SpringBoot 与 Mybatis 的关联jar 是由 Mybatis 方提供的(谁叫Spring 牛逼呢~~)。
基于注解整合Mybatis 时只需要引入@Mapper 注解并在相应的抽象方法上添加 @Insert/@update/@Delete/@Select 即可完成Mybatis 的实体类的注入和Mapper关系的建立(实际由mybatis-spring-boot-starter\1.3.0\mybatis-spring-boot-starter-1.3.0.jar 封装整个映射的实现和抽象)。
…