cz_health_day06
项目一第六天
项目需求
了解移动端(微信公众号)开发方式
独立搭建移动端工程
独立开发套餐列表、套餐详情
掌握阿里云短信服务发送方式
1、移动端的开发(移动端开发方式)
- 基于手机API开发(原生APP) client / server 客户端、服务端
- 苹果 Objective-C
- 安卓 java
- windows phone C#
- symbian C++
注意C/S方式,需要安装客户端,多平台语言不通
- 基于浏览器开发 browser / server
B/S方式,browser浏览器 手机直接访问页面(微信公众号)即可,但需要适配不同手机屏幕大小,无需安装
- 混合开发
原生APP在外面套个壳,访问的还是Web网页,需要安装客户端
建议:移动端开发对新人不友好
移动端开发以微信公众号开发为例:
账号分为 服务号、订阅号、小程序、企业微信 ,最多定义三个一级菜单,最多定义五个子菜单,跳转到开发好的页面
2、查询套餐详情
- 用户点击套餐信息,跳转至套餐详情,这里是页面跳转,将查询的套餐id拼在url路径上,在通过特定的js获取请求的参数,再将这个参数,通过页面加载事件发送异步请求至后端
- 后端接收到数据,查询所有的套餐,数据包括套餐的基本信息,检查组信息,检查项信息,由于彼此之间通过关联表进行关联的,且是多对多的关系,所以采用关联查询操作,将查询得到的数据返回至前端回显
3、短信发送
- 阿里云进行注册,进行短信设置
- 设置短信发送的签名
![]()
- 设置access keys
- 将ak设置权限 将权限进行细分,每个人拥有他所拥有的最大权限
- 通过工具类进行短信的发送
后端的关联查询代码
1.关联查询 ---懒加载模式下 节省资源 fetchType="lazy"
必须在提供方进行数据的使用,因为出了当前事务对数据进行使用时是不会产生效果的
1 1、套餐 2 <resultMap id="baseResultMap" type="com.itheima.pojo.Setmeal"> 3 <!--column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性--> 4 <id column="id" property="id"/> 5 <!--<result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>--> 6 <result column="name" property="name"/> 7 <result column="code" property="code"/> 8 <result column="helpCode" property="helpCode"/> 9 <result column="sex" property="sex"/> 10 <result column="age" property="age"/> 11 <result column="price" property="price"/> 12 <result column="remark" property="remark"/> 13 <result column="attention" property="attention"/> 14 <result column="img" property="img"/> 15 </resultMap> 16 17 <resultMap id="findByIdResultMap" type="com.itheima.pojo.Setmeal" extends="baseResultMap"> 18 <!--多对多映射--> 19 <!--<collection column="传递给嵌套查询语句的字段参数" property="pojo对象中集合属性" ofType="集合属性中的pojo对象" select="嵌套的查询语句" >--> 20 <!--</collection>--> 21 <collection 22 property="checkGroups" 23 ofType="com.itheima.pojo.CheckGroup" 24 select="com.itheima.dao.CheckGroupDao.findCheckGroupById" 25 column="id" 26 > 27 </collection> 28 </resultMap> 29 30 <!--根据套餐ID查询套餐详情(包含套餐基本信息、检查组信息、检查项信息)--> 31 <!--resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。--> 32 <select id="findById" parameterType="int" resultMap="findByIdResultMap"> 33 select * from t_setmeal where id = #{id} 34 </select> 35 36 2、检查组 37 <resultMap id="baseResultMap" type="com.itheima.pojo.CheckGroup"> 38 <id column="id" property="id"/> 39 <result column="name" property="name"/> 40 <result column="code" property="code"/> 41 <result column="helpCode" property="helpCode"/> 42 <result column="sex" property="sex"/> 43 <result column="remark" property="remark"/> 44 <result column="attention" property="attention"/> 45 </resultMap> 46 47 <resultMap id="findByIdResultMap" type="com.itheima.pojo.CheckGroup" extends="baseResultMap"> 48 <!--检查组和检查项多对多关联查询--> 49 <collection property="checkItems" 50 ofType="com.itheima.pojo.CheckItem" 51 column="id" 52 select="com.itheima.dao.CheckItemDao.findCheckItemById" 53 ></collection> 54 </resultMap> 55 56 <!--根据套餐ID查询关联的检查组详情--> 57 <select id="findCheckGroupById" parameterType="int" resultMap="findByIdResultMap"> 58 select * from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id = #{setmeal_id}) 59 </select> 60 61 3、检查项 62 <!--根据检查组ID查询关联的检查项--> 63 <select id="findCheckItemById" parameterType="int" resultType="com.itheima.pojo.CheckItem"> 64 select * from t_checkitem 65 where id 66 in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{id}) 67 </select>
2.java代码实现---优点 sql 语句简单
1 public Setmeal getById(int id) { 2 3 Setmeal setmeal = setmealDao.getById(id);//只获取setmeal对象 4 5 int[] ids = new int[]{id} 6 7 List<CheckGroup> checkgroups = new ArrayList(); 8 9 for(int setmealId :ids) { 10 11 List<CheckGroup> checkgroups = checkgroupDaoz.getById(setmealId ) 12 13 for( ) {//通过检查组获取每个检查项 14 15 } 16 17 checkgroups.add(checkgroup); 18 19 } 20 21 }
发送短信的工具类
1 /** 2 * 短信发送工具类 3 */ 4 public class SMSUtils { 5 public static final String VALIDATE_CODE = "SMS_179606470";//发送短信验证码 6 public static final String ORDER_NOTICE = "SMS_180058300";//体检预约成功通知 7 /** 8 * 发送短信 9 * @param phoneNumbers 10 * @param param 11 * @throws ClientException 12 */ 13 public static void sendShortMessage(String templateCode,String phoneNumbers,String param) throws ClientException{ 14 // 设置超时时间-可自行调整 15 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); 16 System.setProperty("sun.net.client.defaultReadTimeout", "10000"); 17 // 初始化ascClient需要的几个参数 18 final String product = "Dysmsapi";// 短信API产品名称(短信产品名固定,无需修改) 19 final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名(接口地址固定,无需修改) 20 // 替换成你的AK 21 final String accessKeyId = "LTAI4Fu8SGxcqMeCpwr6Znva";// 你的accessKeyId,参考本文档步骤2 22 final String accessKeySecret = "JARg13AOni2GUapEM5W3TmtNEWHYY0";// 你的accessKeySecret,参考本文档步骤2 23 // 初始化ascClient,暂时不支持多region(请勿修改) 24 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); 25 DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); 26 IAcsClient acsClient = new DefaultAcsClient(profile); 27 // 组装请求对象 28 SendSmsRequest request = new SendSmsRequest(); 29 // 使用post提交 30 request.setMethod(MethodType.POST); 31 // 必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 32 request.setPhoneNumbers(phoneNumbers); 33 // 必填:短信签名-可在短信控制台中找到 34 request.setSignName("传智健康短信"); 35 // 必填:短信模板-可在短信控制台中找到 36 request.setTemplateCode(templateCode); 37 // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 38 // 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 39 request.setTemplateParam("{\"code\":\""+param+"\"}"); 40 // 可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) 41 // request.setSmsUpExtendCode("90997"); 42 // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 43 // request.setOutId("yourOutId"); 44 // 请求失败这里会抛ClientException异常 45 SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); 46 if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { 47 // 请求成功 48 System.out.println("请求成功"); 49 } else { 50 System.out.println(sendSmsResponse.getMessage()); 51 } 52 } 53 54 }


浙公网安备 33010602011771号