1. 创建一个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.windy.mall.product</groupId> <artifactId>ms-mall</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <name>ms-mall</name> <url>http://maven.apache.org</url> <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> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <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.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> </dependencies> </project>
2. 创建数据库,sql如下:
create database db_products default charset utf8; create table products (pid int not null primary key auto_increment, pname varchar(200), type varchar(50), price double, createTime timestamp)
3. 创建resouces folder: src/main/resources,并创建application.properties文件
spring.datasource.url=jdbc:mysql:///db_products?useSSL=false spring.datasource.username=*** spring.datasource.password=***
4. 创建实体类package: bean,并创建实体类Product.java
package com.windy.mall.product.bean; import java.sql.Timestamp; 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; } public String toString() { return "Product [pid=" + pid + ", pname=" + pname + ", type=" + type + ", price=" + price + ", createTime=" + createTime + "]"; } }
5. 创建Mapper package:mapper,并创建一个Mapper接口:ProductMapper(整合Mybatis,基于注解的形式)
Mybatis会帮助我们自动创建实现类, 我们只需要声明接口即可.
问题: 如何使用Mybatis的逆向工程,自动生成实体类,并且Mapper接口都是以注解的形式存在,而并非XML形式?
package com.windy.mall.product.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.windy.mall.product.bean.Product; @Mapper public interface ProductMapper { @Insert("insert into products(pname,type,price)values(#{pname},#{type},#{price})") public Integer add(Product product); @Delete("delete from products where pid=#{arg1}") public Integer deleteById(Integer id); @Update("update products set pname=#{pname},type=#{type},price=#{price} where pid=#{id} ") public Integer update(Integer id); @Select("select * from products where pid=#{arg1}") public Product getById(Integer id); @Select("select * from products order by pid desc") public List<Product> queryByLists(); }
6. 针对Mapper的单元测试(待补充)
7. 创建一个响应类Response,把所有返回结果信息,封装在这个类中.
package com.windy.mall.product.utils; public class Response { /* * code: 标志状态码 * 200:表示成功 * 404: 表示资源不存在 * 500:表示失败 */ private String code; private String msg; private Object data; public Response() { super(); } public Response(String code, String msg) { super(); this.code = code; this.msg = msg; } public Response(String code, String msg, Object data) { super(); this.code = code; this.msg = msg; this.data = data; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
8. 创建Controller类(由于只是简单的Demo,所以没有实现Service层)
package com.windy.mall.product.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RestController; import com.windy.mall.product.bean.Product; import com.windy.mall.product.mapper.ProductMapper; import com.windy.mall.product.utils.Response; @RestController public class ProductController { @Autowired private ProductMapper productMapper; @PostMapping("/ms/product/add") public Object add(Product product){ Integer res = productMapper.add(product); if(res == null){ return new Response("200", "Success!"); } return new Response("500","Fail!"); } @PutMapping("/ms/product/update") public Object update(Product product ){ return new Response("200", "Success!", productMapper.update(product)); } @GetMapping("/ms/product/{id}") public Object get(@PathVariable("id") Integer id ){ return new Response("200", "Success!", productMapper.getById(id)); } @GetMapping("/ms/products") public Object list(){ return new Response("200","Success!", productMapper.queryByLists()); } @DeleteMapping("/ms/product/{id}") public Object delete(@PathVariable("id") Integer id ){ Integer res = productMapper.deleteById(id); return res==1? new Response("200","Success!") : new Response("500", "Fail!"); } }