目录
4.在pom.xml中添加Mybatis、MySQL、FreeMarker等的starter
5.在application.properties中添加数据库、Mybatis、FreeMarker等的相关配置
6.在SpringBoot启动类MallApplication配置扫描mapper接口类
1.新建一个SpringBoot项目
(1)File--->New--->Project
(2)选择项目工具和JDK版本
(3)填写项目信息
(4)选择Web依赖包
(5)填写项目名称和项目保存地址
(6)搭建好的目录结构
2.数据库设计
3.项目整体目录结构
4.在pom.xml中添加Mybatis、MySQL、FreeMarker等的starter
<!-- spring boot mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- mysql连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- spring boot freemarker依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
5.在application.properties中添加数据库、Mybatis、FreeMarker等的相关配置
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/testproject?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis配置
## 实体类包路径
mybatis.typeAliasesPackage=org.mall.domain
## mapper.xml路径
mybatis.mapperLocations=classpath:mapper/*.xml
## Freemarker 配置
## 文件配置路径
spring.freemarker.template-loader-path=classpath:/web/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
## 获取contextPath
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
6.在SpringBoot启动类MallApplication配置扫描mapper接口类
package org.mall;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//Spring Boot应用的标识
@SpringBootApplication
//mapper接口类扫描包配置,用这个注解可以注册mybatis mapper接口类
@MapperScan("org.mall.dao")
public class MallApplication {
public static void main(String[] args) {
// 程序启动入口,启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(MallApplication.class,args);
}
}
7.实现增删改查
(1)Commodity 实体类
package org.mall.domain;
/*
* 商品实体类
* */
public class Commodity {
//主键id
private Integer id;
//商品名称
private String name;
//商品规格
private String standard;
//商品温度
private String temperature;
//商品价格
private int price;
//商品描述
private String description;
//省略getter、setter方法
......
}
(2)CommodityDao接口类
package org.mall.dao;
import org.mall.domain.Commodity;
import java.util.List;
/*
* 商品DAO
* */
public interface CommodityDao {
//获取全部商品
List<Commodity> findAll();
//新增商品
void insertCommodity(Commodity commodity);
//修改商品
void updateCommodity(Commodity commodity);
//根据id获取商品
Commodity findById(Integer id);
//根据id删除商品
void deleteById(Integer id);
}
(3)CommodityService接口
package org.mall.service;
import org.mall.domain.Commodity;
import java.util.List;
/*
* 商品Service接口
* */
public interface CommodityService {
//获取全部商品信息
List<Commodity> findAll();
//新增商品信息
void insertCommodity(Commodity commodity);
//修改商品信息
void updateCommodity(Commodity commodity);
//根据id查询获取商品信息
Commodity findById(Integer id);
//根据id删除商品信息
void deleteById(Integer id);
}
(4)CommodityService接口实现类
package org.mall.service.impl;
import org.mall.dao.CommodityDao;
import org.mall.domain.Commodity;
import org.mall.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/*
* 商品Service实现类
* */
@Service
public class CommodityServiceImpl implements CommodityService {
@Autowired
private CommodityDao commodityDao;
//获取全部商品
public List<Commodity> findAll(){
return commodityDao.findAll();
}
//新增商品
public void insertCommodity(Commodity commodity){
commodityDao.insertCommodity(commodity);
}
//修改商品
public void updateCommodity(Commodity commodity){
commodityDao.updateCommodity(commodity);
}
//根据id获取商品
public Commodity findById(Integer id){
return commodityDao.findById(id);
}
//根据id删除商品
public void deleteById(Integer id){
commodityDao.deleteById(id);
}
}
(5)Commodity控制层
注:在控制层使用了Swagger实现接口说明,具体使用看上篇博