ssm基本结构图示
ssm——springboot、springMVC、mybaits
首先创建一个基本的springboot项目:
编辑pom.xml
导入mybaits依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
编辑application.yml
例如:
server:
port: 8888
spring:
datasource:
url: jdbc:mysql://localhost:3306/shopping?useSSL=false&useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
编辑启动类BootdemoApplication
package com.neu.tech.bootdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages={"com.neu.tech.bootdemo.mapper"})
public class BootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(BootdemoApplication.class, args);
}
}
MapperScan是扫描器,注意加上这一句不然会报错
编辑controller
package com.neu.tech.bootdemo.controller;
import com.neu.tech.bootdemo.entity.Product;
import com.neu.tech.bootdemo.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
//作用是让类里面全都返回json
@CrossOrigin
public class TestController {
@Autowired
private ProductMapper productMapper;
@GetMapping("/hello")
public String hello(){
return "hello root";
}
@GetMapping("/listProduct")
public List<Product> listProduct(){
return productMapper.listProduct();
}
}
其中@RestController作用是让类里面全都返回json格式
@CrossOrigin作用是解决跨域问题
编辑实体类Product
package com.neu.tech.bootdemo.entity;
import java.math.BigDecimal;
import java.util.Date;
public class Product {
private Integer id;
private Integer categoryId;
private String name;
private String subtitle;
private String mainImage;
private String subImages;
private String detail;
private BigDecimal price;
private Integer stock;
private Integer status;
private Date createTime;
private Date updateTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getMainImage() {
return mainImage;
}
public void setMainImage(String mainImage) {
this.mainImage = mainImage;
}
public String getSubImages() {
return subImages;
}
public void setSubImages(String subImages) {
this.subImages = subImages;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
其中getter和setter方法没有用lombok生成。
编辑ProductMapper
注意文件位置需要与resources中的一致,因为打包成jar包的时候事实上是两者合在一起的。
package com.neu.tech.bootdemo.mapper;
import com.neu.tech.bootdemo.entity.Product;
import java.util.List;
public interface ProductMapper {
List<Product> listProduct();
}
这里的listProduct与xml文件中的select语句中的id一致。
编辑ProductMapper.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.neu.tech.bootdemo.mapper.ProductMapper">
<resultMap id="product" type="com.neu.tech.bootdemo.entity.Product">
<id property="id" column="id"></id>
<result property="categoryId" column="category_id"></result>
<result property="name" column="name"></result>
<result property="subtitle" column="subtitle"></result>
<result property="mainImage" column="main_image"></result>
<result property="subImages" column="sub_images"></result>
<result property="detail" column="detail"></result>
<result property="price" column="price"></result>
<result property="stock" column="stock"></result>
<result property="status" column="status"></result>
<result property="createTime" column="create_time"></result>
<result property="updateTime" column="update_time"></result>
</resultMap>
<select id="listProduct" resultMap="product">
select * from pmt_product limit 0,10
</select>
</mapper>
其中mapper中namespace与java类mapper位置一致。
resultMap中的id是映射名称可以自取,namespace是实体类位置。
select语句中,id为实体类中的方法名,resultMap对应映射名称