diff --git a/quick-start/README.md b/quick-start/README.md index 0a94e52c5..e71b26eea 100644 --- a/quick-start/README.md +++ b/quick-start/README.md @@ -3,7 +3,7 @@ ## 目录 1. [前言](#前言) 2. [创建项目](#创建项目) -3. [引入hsweb](#引入hsweb) +3. [增删改查](#增删改查) ## 前言 `hsweb`是基于`java8`,`spring-boot`,`mybatis`开发.所以在开始使用`hsweb`的前,你至少应该掌握以下技术:`java`,`maven`. @@ -13,7 +13,7 @@ hsweb 目前未提供前端支持,仅有一个[demo](https://github.com/hs-web/hsweb3-demo)可供参考. -本入门教程以一个最传统的单模块项目为例子. IDE为:`Intellij IDEA` +本入门教程以一个最传统的单模块项目为例子. IDE为:`Intellij IDEA`(需安装lombok插件) ## 创建项目 1. 新建maven项目: @@ -103,6 +103,12 @@ hsweb 目前未提供前端支持,仅有一个[demo](https://github.com/hs-web/h spring-boot-starter-test test + + org.hswebframework.web + hsweb-tests + ${hsweb.framework.version} + test + @@ -287,30 +293,260 @@ public class MyProjectApplication { 到此,项目建立完成,和普通的spring-boot项目没有区别. -## 引入hsweb +## 增删改查 -以权限管理模块(`hsweb-system/hsweb-system-authorization`)为例. - -在pom.xml中引入模块: +使用通用CURD,添加一个增删改查功能: + +在`pom.xml`中引入模块: ```xml - + org.hswebframework.web - hsweb-authorization-basic + hsweb-commons-dao-mybatis ${hsweb.framework.version} - - org.hswebframework.web - hsweb-system-authorization-starter + hsweb-commons-service-simple + ${hsweb.framework.version} + + + org.hswebframework.web + hsweb-commons-controller ${hsweb.framework.version} ``` -![import-authorization-module](./img/import-authorization-module.gif "import-authorization-module") +![引入curd依赖](./img/import-authorization-module.gif "引入curd依赖") +1. 创建数据库表: + +```sql + create table tb_test( + id varchar(32) primary key, + name varchar(32) not null, + status tinyint, + comment text + ) +``` + +2. 创建实体类 `com.mycompany.entity.TestEntity` + +实体类可通过继承:`org.hswebframework.web.commons.entity.SimpleGenericEntity<主键类型>`.来使用通用的crud功能. + +```java +@Getter +@Setter +public class TestEntity extends SimpleGenericEntity { + private String name; + + private Byte status; + + private String comment; +} + +``` + +![创建实体](./img/create-entity.gif "创建实体") + +3. 创建Dao + +dao接口可通过继承:`org.hswebframework.web.dao.CrudDao<实体类,主键类型>`.来使用通用的crud功能. + +创建Dao接口 `com.mycompany.dao.TestDao` + +```java +public interface TestDao extends CrudDao { + +} +``` +![创建Dao](./img/create-dao.gif "创建实体Dao") + + +创建myabtis mapper,在`resources`目录上创建:`com/mycompany/dao/mybatis/TestMapper.xml` + +```xml + + + + + + + + + + + + + + + + + + + + + + + + delete from tb_test where id =#{id} + + + + + + + + + + + + + + + + + +``` +![创建Mybatis](./img/create-mybaits-mapper.gif "创建实体Mybatis") + + + +在`application.yml`中添加配置: + +```yaml +mybatis: + mapper-locations: classpath:com/mycompany/dao/mybatis/**/*.xml +``` + +在`MyProjectApplication`上添加注解:`@MapperScan(basePackages = "com.mycompany.dao", markerInterface = org.hswebframework.web.dao.Dao.class)` + + + +4. 创建Service + +service接口可通过继承:`org.hswebframework.web.service.CrudService<实体类,主键类型>`.来使用通用的crud功能. + +创建接口类: `com.mycompany.service.TestService` + +```java +public interface TestService extends CrudService { +} + +``` + +实现类可通过继承: ` org.hswebframework.web.service.GenericEntityService<实体类,主键类型>`.来使用通用crud功能. + +创建实现类 `com.mycompany.service.impl.TestServiceImpl` + +```java +@Service +public class TestServiceImpl extends GenericEntityService + implements TestService { + + @Autowired + private TestDao testDao; + + @Override + protected IDGenerator getIDGenerator() { + return IDGenerator.MD5; + } + + @Override + public TestDao getDao() { + return testDao; + } +} +``` + +![创建Service](./img/create-service.gif "创建Serivce") + + +5. 创建Controller + +controller 可通过实现接口: `org.hswebframework.web.controller.SimpleGenericEntityController<实体类,主键类型,org.hswebframework.web.commons.entity.param.QueryParamEntity>` + +创建controller类: `com.mycompany.controller.TestController` + +```java + +@RestController +@RequestMapping("/test") +public class TestController implements SimpleGenericEntityController { + + @Autowired + TestService testService; + + @Override + public CrudService getService() { + return testService; + } +} + +``` + +6. 测试 + +方式一:编写单元测试 + +在test目录创建`com.mycompany.TestApplication`和`com.mycompany.controller.TestControllerTest` + +```java +@SpringBootApplication +@WebAppConfiguration +public class TestApplication { + +} +``` + +```groovy +@WebAppConfiguration +@ContextConfiguration +@SpringBootTest(classes = [TestApplication.class], properties = ["classpath:application.yml"]) +class TestControllerTest extends Specification { + @Autowired + private ConfigurableApplicationContext context; + + @Shared + private MockMvc mockMvc; + + void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); + } + + def "Test Create Data"() { + setup: + def testData = """ + {"name":"测试数据","status":1,"comment":"说明"} + """ + and: + mockMvc.perform( + post("/test") + .contentType(MediaType.APPLICATION_JSON) + .content(testData) + ).andExpect(status().is(201)) + + } +} +``` +执行单元测试,通过则说明新增功能测试通过. + +方式二: `postman` 或者idea的`Test Restful Web Servcice`,以Idea工具为例: + +执行启动类:`com.mycompany.MyProjectApplication`启动服务,然后调用`/test`服务 + +![测试](./img/idea-test.gif "测试") + +一个最简单的通用crud例子完成了!! diff --git a/quick-start/img/create-controller.gif b/quick-start/img/create-controller.gif new file mode 100644 index 000000000..0abddc8e1 Binary files /dev/null and b/quick-start/img/create-controller.gif differ diff --git a/quick-start/img/create-dao.gif b/quick-start/img/create-dao.gif new file mode 100644 index 000000000..76bf1070c Binary files /dev/null and b/quick-start/img/create-dao.gif differ diff --git a/quick-start/img/create-entity.gif b/quick-start/img/create-entity.gif new file mode 100644 index 000000000..b5219f01b Binary files /dev/null and b/quick-start/img/create-entity.gif differ diff --git a/quick-start/img/create-mybatis-mapper.gif b/quick-start/img/create-mybatis-mapper.gif new file mode 100644 index 000000000..0fc240687 Binary files /dev/null and b/quick-start/img/create-mybatis-mapper.gif differ diff --git a/quick-start/img/create-service.gif b/quick-start/img/create-service.gif new file mode 100644 index 000000000..7468292f1 Binary files /dev/null and b/quick-start/img/create-service.gif differ diff --git a/quick-start/img/idea-test.gif b/quick-start/img/idea-test.gif new file mode 100644 index 000000000..9dda5a532 Binary files /dev/null and b/quick-start/img/idea-test.gif differ diff --git a/quick-start/img/import-authorization-module.gif b/quick-start/img/import-authorization-module.gif new file mode 100644 index 000000000..4f9fb2150 Binary files /dev/null and b/quick-start/img/import-authorization-module.gif differ diff --git a/quick-start/img/import-commons-module.gif b/quick-start/img/import-commons-module.gif new file mode 100644 index 000000000..31ea0ead6 Binary files /dev/null and b/quick-start/img/import-commons-module.gif differ