创建SpringBoot项目
上一章已经讲过如何创建一个
SpringBoot的项目,回顾请点击这里
三层架构介绍
三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:界面层(User Interface layer)、业务逻辑层(Business Logic Layer)、数据访问层(Data access layer)。
- 数据访问层:主要看数据层里面有没有包含逻辑处理,实际上它的各个函数主要完成各个对数据文件的操作。而不必管其他操作。
- 业务逻辑层:主要负责对数据层的操作。也就是说把一些数据层的操作进行组合。
- 表示层:主要对用户的请求接受,以及数据的返回,为客户端提供应用程序的访问。
依赖关系:ULL -> BLL -> DAL,ULL不可直接引用DAL层
代码结构
下图是上一章创建的
SpringBoot项目
我们先把
src删除,target是编译之后产生的目录,这边可以处理然后在项目上
右键,点击New Moudle,按照创建上一章SpringBoot项目一样即可
创建3个
Module:web=界面层(这边用来做启动)-biz=业务层-dal=数据层还需要建一个
Module:share=用于暴露接口每一层只要留下
src、*.iml、pom.xml即可因为是
Module所以每层里面自动会有一个Application.java的启动类,把Biz与Dal层的删除,只留下web层来启动就好了
然后我们需要去各层项目中的
pom.xml配置模块依赖首先是最外层的
pom.xml,这边添加的是模块属性,代表这个根项目中包含3个模块
然后是去
web层,这边添加的不是模块属性了,因为是在模块中配置了,此时应该配置依赖了,web层应该是依赖biz;然后同理biz去配置依赖dal就好了
然后将
biz与dal层中的application.properties重命名各层项目的名字,如application-biz.propertiesok,此时配置已经告一段落,接下来就是如何把三层访问连接起来…
三层访问
DAL层
前言:数据层我们是选用
Mybatis框架,在后面会使用到,不熟悉Mybatis的暂时可以先在
dal中的application-biz.properties配置数据库信息(MySQL的安装之前的文章也有提过),因为我们是用Mybatis框架,所以同时需要配置Mybatis//mysql spring.datasource.url=jdbc:mysql://localhost:3306/TestDB spring.datasource.username=root spring.datasource.password=@ASDasd123 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver //mybatis config mybatis.mapper-locations=classpath:/mappering/*mapper.xml mybatis.configuration.use-column-label=true mybatis.configuration.use-generated-keys=false mybatis.configuration.auto-mapping-behavior=FULL mybatis.configuration.default-fetch-size=100 mybatis.configuration.local-cache-scope=SESSION mybatis.configuration.lazy-loading-enabled=false mybatis.configuration.jdbc-type-for-null=OTHER mybatis.configuration.lazy-load-trigger-methods=equals,clone,hashCode,toString mybatis.configuration.safe-row-bounds-enabled=false mybatis.configuration.default-statement-timeout=30 mybatis.configuration.default-executor-type=SIMPLE mybatis.type-aliases-package=cn.jinxuebin.demodal.model mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.multiple-result-sets-enabled=true mybatis.configuration.cache-enabled=true在pom.xml中需要加入依赖
由于
datasource的配置是在dal层的application-biz.properties里,项目启动的时候不会读取这个配置,所以需要加一个配置类DalConfig.javapackage cn.jinxuebin.demodal.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; /** * @author Peter * @description DAL配置类 * @data 2019-02-20 */ @Configuration @MapperScan(basePackages = "cn.jinxuebin.demodal.dao") @PropertySource(value= {"classpath:application-dal.properties"}) public class DalConfig { }我已经在本地的数据库简单建了一张测试的
Products表
然后利用
Mybatis-generator工具生成相应的Dao/Model/Mapper关键文件就这么3个
- mybatis-generator-core-1.3.7.jar 它就是生成工具
jar包- mysql-connector-java-8.0.15.jar 它就是
mysql连接jar包,版本需一致- generatorConfig.xml 这个就是生成器的配置文件了
- src 这个是空的就可以,代码会自动生成到这边
generatorConfig.xml根据需求配置,注意namespace/type/parameterType检查是否正确执行命令
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite就生成成功了,然后把这些类放到工程里
DAL层的代码类如下图,具体代码里的注解请直接看源码
Share
前言:
Share不属于三层的范围,它只是BLL层对外开放的接口层,当其他应用需要调我们这个服务的时候就会用到Share这个Module;Share还需要提供BO实体类,也是用来提供给外部本章就简单一点,建一个
interface,里面就一个方法package cn.jinxuebin.demoshare; import cn.jinxuebin.demoshare.bo.ProductsBO; /** * @author Peter * @description Products业务接口类 * @data 2019-02-20 */ public interface IProductsService { ProductsBO getProductById(int id); }BLL
前言:
BLL是业务逻辑层,基本上应该都是业务代码,所以不会像DAL那么多配置,相对比较明朗首先当然也是
BLL的配置类:BizConfig.javapackage cn.jinxuebin.demodal.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.mybatis.spring.annotation.MapperScan; /** * @author Peter * @description DAL配置类 * @data 2019-02-20 */ @Configuration @MapperScan(basePackages = "cn.jinxuebin.demodal.dao") @PropertySource(value= {"classpath:application-dal.properties"}) public class DalConfig { }然后就是定义一个Imp实现类来实现IProductsService
package cn.jinxuebin.demobiz.imp; import cn.jinxuebin.demobiz.ProductsManager; import cn.jinxuebin.demoshare.IProductsService; import cn.jinxuebin.demoshare.bo.ProductsBO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author Peter * @description ProductsService实现类 * @data 2019-02-20 */ @Service public class ProductsImp implements IProductsService { @Autowired private ProductsManager productsManager; @Override public ProductsBO getProductById(int id) { return productsManager.getProductById(id); } }然后需要一个实现业务逻辑的Manager类
package cn.jinxuebin.demobiz; import cn.jinxuebin.demodal.dao.ProductsMapper; import cn.jinxuebin.demodal.model.Products; import cn.jinxuebin.demoshare.bo.ProductsBO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author Peter * @description 商品业务类 * @data 2019-02-20 */ @Service public class ProductsManager { @Autowired private ProductsMapper productsMapper; public ProductsBO getProductById(int id) { //将DO实体类转化成BO实体类,这边就不封装函数了,简单实现下 Products dao = productsMapper.selectByPrimaryKey(id); ProductsBO bo = new ProductsBO(); bo.setItemid(dao.getItemid()); bo.setDesc(dao.getItemdesc()); bo.setName(dao.getItemname()); bo.setPrice(dao.getPrice()); bo.setShopid(dao.getShopid()); return bo; } }ULL
前言:这边我们就是一个
Web的Module用来做启动创建
Application.javapackage cn.jinxuebin.demoweb; import cn.jinxuebin.demobiz.config.BizConfig; import cn.jinxuebin.demodal.config.DalConfig; import cn.jinxuebin.demoshare.IProductsService; import cn.jinxuebin.demoshare.bo.ProductsBO; import com.fasterxml.jackson.databind.util.JSONPObject; import com.mysql.cj.xdevapi.JsonArray; import net.minidev.json.JSONObject; import net.minidev.json.JSONValue; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @Import({BizConfig.class, DalConfig.class}) public class DemoWebApplication { public static void main(String[] args) { SpringApplication.run(DemoWebApplication.class, args); } @Autowired private IProductsService service; @RequestMapping("/book") public String home(@RequestParam("id") int id) { ProductsBO bo = service.getProductById(id); return JSONValue.toJSONString(bo); } }
类关系图

OK,这样就基本完成了,然后启动下看看,若没报错就成功了
访问地址:
http://127.0.0.1:8080/book?id=1,可以看到数据了
这边把建表的SQL语句提供下
create table Products
(
ItemId int auto_increment
primary key,
ItemName varchar(256) null,
ItemDesc varchar(256) null,
Price double not null,
ShopId int not null
);









