|
| 1 | +# 创建通用增删改查功能 |
| 2 | + |
| 3 | +## 1. 实体 |
| 4 | +目前hsweb只有一种实体:PO。统一继承 `GenericPo` |
| 5 | + |
| 6 | +新建实体`org.hsweb.demo.bean.test.MyTest`如下: |
| 7 | +```java |
| 8 | + public class MyTest extends GenericPo{ |
| 9 | + private String name; |
| 10 | + |
| 11 | + private int age; |
| 12 | + |
| 13 | + //由于查询使用动态参数,使用此方式定义属性名。方便统一维护 |
| 14 | + public interface Property extends GenericPo.Property{ |
| 15 | + String name = "name"; |
| 16 | + String age = "age"; |
| 17 | + } |
| 18 | + } |
| 19 | +``` |
| 20 | + |
| 21 | +建立数据库表(hsweb暂未使用jpa等方式自动建表): |
| 22 | + |
| 23 | +方式1: 编辑`resources/scripts/initialize.groovy` (此脚本在项目首次运行时执行) 并加入 |
| 24 | +```groovy |
| 25 | +database.createOrAlter("s_test") |
| 26 | + .addColumn().name("u_id").alias("id").comment("ID").jdbcType(JDBCType.VARCHAR).length(32).primaryKey().commit() |
| 27 | + .addColumn().name("age").alias("age").comment("年龄").jdbcType(JDBCType.DECIMAL).length(16,0).commit() |
| 28 | + .addColumn().name("name").alias("name").comment("姓名").jdbcType(JDBCType.VARCHAR).length(128).commit() |
| 29 | + .comment("测试").commit(); |
| 30 | +``` |
| 31 | + |
| 32 | +方式2: 如果系统已经初始化,则需要手动建立表结构,或者使用更新版本的方式进行初始化: |
| 33 | +假设当前版本为 1.0.0, 升级为 1.0.1,则新建文件 ``resources/scripts/upgrade/1.0.1.groovy`` 并加入方式1中的脚本内容. |
| 34 | +在启动后,更新版本时会自动执行此脚本. |
| 35 | + |
| 36 | +## 2. dao 接口 |
| 37 | + |
| 38 | +定义接口 ``org.hsweb.demo.dao.test.MyTestMapper`` |
| 39 | + |
| 40 | +(增删改查 继承GenericMapper即可) |
| 41 | +```java |
| 42 | + public interface MyTestMapper extends GenericMapper<MyTest, String> { |
| 43 | + } |
| 44 | +``` |
| 45 | +## 3.mybatis dao实现 |
| 46 | +mybatis 采用配置文件(xml)的方式 |
| 47 | + |
| 48 | +新建xml配置`resources/org/hsweb/demo/mappers/test/MyTestMapper`如下: |
| 49 | +```xml |
| 50 | +<?xml version="1.0" encoding="UTF-8" ?> |
| 51 | + <!DOCTYPE mapper |
| 52 | + PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
| 53 | + "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 54 | + <mapper namespace="org.hsweb.demo.dao.test.MyTestMapper"> |
| 55 | + <resultMap id="TestResultMap" type="org.hsweb.demo.bean.test.MyTest"> |
| 56 | + <id property="id" column="u_id" javaType="string" jdbcType="VARCHAR"/> |
| 57 | + <result property="name" column="name" javaType="String" jdbcType="VARCHAR"/> |
| 58 | + <result property="age" column="age" javaType="int" jdbcType="INTEGER"/> |
| 59 | + </resultMap> |
| 60 | + |
| 61 | + <!--用于动态生成sql所需的配置--> |
| 62 | + <sql id="config"> |
| 63 | + <!--动态sql使用resultMapId对应的配置,来生成sql--> |
| 64 | + <bind name="resultMapId" value="'TestResultMap'"/> |
| 65 | + <bind name="tableName" value="'s_test'"/> |
| 66 | + </sql> |
| 67 | + <insert id="insert" parameterType="org.hsweb.web.bean.common.InsertParam"> |
| 68 | + <include refid="config"/> |
| 69 | + <include refid="BasicMapper.buildInsertSql"/> |
| 70 | + </insert> |
| 71 | + |
| 72 | + <delete id="delete" parameterType="org.hsweb.web.bean.common.DeleteParam"> |
| 73 | + <include refid="config"/> |
| 74 | + <include refid="BasicMapper.buildDeleteSql"/> |
| 75 | + </delete> |
| 76 | + |
| 77 | + <update id="update" parameterType="org.hsweb.web.bean.common.UpdateParam"> |
| 78 | + <include refid="config"/> |
| 79 | + <include refid="BasicMapper.buildUpdateSql"/> |
| 80 | + </update> |
| 81 | + |
| 82 | + <select id="selectByPk" parameterType="string" resultMap="TestResultMap"> |
| 83 | + select * from s_test where u_id=#{id} |
| 84 | + </select> |
| 85 | + |
| 86 | + <select id="select" parameterType="org.hsweb.web.bean.common.QueryParam" resultMap="TestResultMap"> |
| 87 | + <include refid="config"/> |
| 88 | + <include refid="BasicMapper.buildSelectSql"/> |
| 89 | + </select> |
| 90 | + |
| 91 | + <select id="total" parameterType="org.hsweb.web.bean.common.QueryParam" resultType="int"> |
| 92 | + <include refid="config"/> |
| 93 | + <include refid="BasicMapper.buildTotalSql"/> |
| 94 | + </select> |
| 95 | + </mapper> |
| 96 | +``` |
| 97 | + |
| 98 | +## 4. service 接口 |
| 99 | + |
| 100 | +定义service接口 ``org.hsweb.demo.service.test.MyTestService`` |
| 101 | + |
| 102 | +(增删改查 GenericService) |
| 103 | +```java |
| 104 | +public interface MyTestService extends GenericService<MyTest, String> { |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## 5. service 实现 |
| 109 | + |
| 110 | +定义service实现类 ``org.hsweb.demo.service.test.impl.SimpleMyTestService`` |
| 111 | + |
| 112 | +继承 AbstractServiceImpl |
| 113 | +```java |
| 114 | +@Service("testService") |
| 115 | +public class SimpleMyTestService extends AbstractServiceImpl<MyTest, String> implements MyTestService { |
| 116 | + @Autowired |
| 117 | + private MyTestMapper myTestMapper; |
| 118 | + |
| 119 | + //AbstractServiceImpl 使用GenericMapper的实现类进行CRUD操作 |
| 120 | + @Override |
| 121 | + protected MyTestMapper getMapper() { |
| 122 | + return myTestMapper; |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## 6. controller |
| 128 | + |
| 129 | +定义Controller ``org.hsweb.demo.controller.test.MyTestController`` |
| 130 | + |
| 131 | +```java |
| 132 | +@RestController |
| 133 | +@RequestMapping("/myTest") |
| 134 | +@Authorize(module = "myTest") //权限验证 |
| 135 | +@AccessLogger("测试模块") //访问日志描述 |
| 136 | +public class MyTestController extends GenericController<MyTest, String> { |
| 137 | + |
| 138 | + @Autowired |
| 139 | + MyTestService myTestService; |
| 140 | + |
| 141 | + @Override |
| 142 | + protected MyTestService getService() { |
| 143 | + return myTestService; |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +## 7. 添加权限 |
| 149 | + |
| 150 | +1、启动并登录系统,进入系统管理-权限管理模块加入对应的权限,重新登录即可使用了。 |
| 151 | +2、或者参照初始化表的方式,以脚本的方式进行初始化,如: |
| 152 | + |
| 153 | +```groovy |
| 154 | +def module= [u_id: 'myTest', name: '测试', uri: 'admin/myTest/list.html', icon: '', parent_id: '-1', remark: '', status: 1, optional: '[{"id":"M","text":"菜单可见","checked":true},{"id":"import","text":"导入excel","checked":true},{"id":"export","text":"导出excel","checked":true},{"id":"R","text":"查询","checked":true},{"id":"C","text":"新增","checked":true},{"id":"U","text":"修改","checked":true},{"id":"D","text":"删除","checked":false}]', sort_index: 1]; |
| 155 | +database.getTable("s_modules").createInsert().value(module).exec(); |
| 156 | +``` |
| 157 | + |
| 158 | +## 8. 感觉太麻烦? |
| 159 | + |
| 160 | +**使用在线代码生成器,一键生成上诉全部代码!** |
0 commit comments