当前位置: 首页 > news >正文

网站建设调查的问卷通过网站提升公司形象

网站建设调查的问卷,通过网站提升公司形象,找人做网站做的很烂,开发公司成本管控四、Mapper 的动态代理1. 引入 在上面的 CURD 例子中#xff0c;我们发现#xff1a;Dao 层的实现类的每一个方法仅仅是通过 SqlSession 对象的相关 API 定位到映射文件 mapper 中的 SQL 语句#xff0c;真正对数据库操作的工作实际上是有 Mybatis 框架通过 mapper 中的 SQL…四、Mapper 的动态代理 1. 引入 在上面的 CURD 例子中我们发现Dao 层的实现类的每一个方法仅仅是通过 SqlSession 对象的相关 API 定位到映射文件 mapper 中的 SQL 语句真正对数据库操作的工作实际上是有 Mybatis 框架通过 mapper 中的 SQL 语句完成的所以 Dao 层的实现类并没有做什么实质性的工作所以Mybatis 就可以抛开 Dao 层实现类直接定位到 mapper 中的 SQL 语句来操作数据库这种 Dao 层实现类的实现方式称为 Mapper 动态代理即使用动态代理对象替换掉原来的 Dao 层实现类。 2. Mapper 动态代理特点  Mapper 动态代理方式不需要程序员去实现 Dao 层接口接口的实现由 Mybatis 结合映射文件自动生成动态代理对象来实现的。 3. 要实现 mapper 的动态代理需要满足如下条件 (1) 映射文件 mapper 需要和 Dao 层接口在同一个包下面 (2) 映射文件的名称需要和 Dao 层接口的名称相同 (3) 映射文件 mapper 中的 namespace 的名称必须是 Dao 层接口的全类名(4) 映射文件 mapper 中的 SQL 语句的 id 名称需要和 Dao 层接口的方法名相同(5) 删除 Dao 层的实现类 (6) 删除 Dao 层接口的 selectStudentMap() 方法以及映射文件配置因为 Mapper 动态代理不支持 map 查询 (7) 修改主配置文件 mybatis.xml 中引入 mapper 映射文件的方式 (8) 将 Student 的 tscore 属性改过来mapper 也要改 Student.javapackage com.edu.beans;public class Student {private long id;private String name;private int age;private double score;    public Student() {}    public Student(String name, int age, double score) {this.name name;this.age age;this.score score;}    public Student(long id, String name, int age, double score) {this.id id;this.name name;this.age age;this.score score;}    public long getId() {return id;}    public void setId(long id) {this.id id;}    public String getName() {return name;}    public void setName(String name) {this.name name;}    public int getAge() {return age;}    public void setAge(int age) {this.age age;}    public double getScore() {return score;}    public void setScore(double score) {this.score score;}    Overridepublic String toString() {return Student{ id id , name name \ , age age , score score };} }IStudentDao.xml 映射文件?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd !--mybatis-3-mapper.dtd是 mybatis 映射文件的约束文件约束文件的位置在 mybatis 的核心jar包(mybatis-3.3.0.jar)的这里org.apache.ibatis.builder.xml.mybatis-3-mapper.dtd -- mapper namespacecom.edu.dao.IStudentDao!--定义了一个插入的 SQL 语句其中 #{name} 为动态参数也就是 ? 占位符该动态参数的名称要和前面方法(void insertStudent(Student student))传过来的对象(student)的属性同名这样 student 对象的属性值就会给这些动态参数赋值--insert idinsertStudent parameterTypeStudentinsert into student(name,age,score) values(#{name},#{age},#{score})/insert    insert idinsertStudentCatchIdinsert into student(name,age,score) values(#{name},#{age},#{score})!--selectKey使用新插入记录的 id 值初始化被插入的对象即初始化方法参数 student它的属性有keyPropertyid要插入这个对象的属性名这里就是指给方法参数 Student 的 id 属性赋值resultTypelong这个 id 属性的数据类型orderAFTER指明主键 id 的值在 insert 语句执行时的生成时机MySQL 是 AFTER即在 insert 之后生成 id 值Oracle 是 BEFORE即在 insert 之前生成 id 值--selectKey keyPropertyid resultTypelong orderAFTERselect last_insert_id();/selectKey/insert    delete iddeleteStudentById!--注意如果动态参数只有一个可以使用任意名称不用和方法参数同名例如这里的 #{xxx} 而不是 #{id}--delete from student where id#{xxx}/delete    update idupdateStudent!--这里除了 #{id} 是旧值其他都是新值--update student set name#{name},age#{age},score#{score} where id#{id}/update    !--resultTypeStudent将返回结果集中的每一条记录封装成 Student 类型这里要给出全类名我们已经在主配置文件配置了别名就可以写别名--select idselectAllStudents resultTypeStudentselect * from student/select    !--resultMap完成表字段到 JavaBean 属性的映射从而可以将查询结果封装成 JavaBean 对象它的属性idstudentMapresultMap 的 id 名称typeStudent查询记录封装成的 JavaBean 类型--resultMap idstudentMap typeStudent!--完成表字段到 JavaBean 属性的映射--!--id表示主键字段columnid是表的字段名propertyid是 JavaBean 的属性名--id columnid propertyid/!--其他的非主键字段的映射使用 result 标签--result columnname propertyname/result columnage propertyage/result columnscore propertyscore//resultMapselect idselectStudentById resultMapstudentMapselect id,name,age,score from student where id#{xxx}/select    select idselectStudentsByMap resultTypeStudentselect * from student where name#{map.name} and age #{map.age}/select!--    select idselectStudentsByName resultTypeStudentselect * from student where name like concat(%,#{xxx},%)/select--    select idselectStudentsByName resultTypeStudent!--注意% 和 #{xxx} 之间有一个空格--select * from student where name like % #{xxx} %/select /mapper(9) 在测试类 StudentTest 中通过 SqlSession 的 getMapper() 方法获取 Dao 层接口的动态代理对象来测试package com.edu.test; import com.edu.beans.Student; import com.edu.dao.IStudentDao; import com.edu.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.util.HashMap; import java.util.List; import java.util.Map;//JUnit 的测试类 public class StudentTest {private SqlSession session;private IStudentDao studentDao;    Before //被 Before 注解的方法为初始化方法它会在每次 Test 修饰的方法执行之前执行一次完成初始化工作public void setUp(){session MybatisUtils.getSqlSession();//获取 Dao 层接口的动态代理对象(Mapper 的动态代理)studentDao session.getMapper(IStudentDao.class);}    After //被 After 注解的方法为销毁方法它会在 Test 修饰的方法执行之后执行一次完成资源的销毁工作public void tearDown(){if(session ! null){session.close();}}    Test //被 Test 注解的方法为测试方法可以有多个这样的方法public void testInsertStudent(){studentDao.insertStudent(new Student(张三,21,98.5));session.commit();//动态代理对象只会根据映射文件中的 SQL 标签去执行 SQL 语句并不会提交事务需要我们自己提交}    Testpublic void testInsertStudentCatchId(){Student student new Student(王五,21,98.5);System.out.println(插入数据前student student);studentDao.insertStudentCatchId(student);System.out.println(插入数据后student student);session.commit();//动态代理对象只会根据映射文件中的 SQL 标签去执行 SQL 语句并不会提交事务需要我们自己提交}    Testpublic void testDeleteStudentById(){studentDao.deleteStudentById(4L);session.commit();//动态代理对象只会根据映射文件中的 SQL 标签去执行 SQL 语句并不会提交事务需要我们自己提交}    Testpublic void testUpdateStudent(){Student student new Student(3,小王,21,98.5);studentDao.updateStudent(student);session.commit();//动态代理对象只会根据映射文件中的 SQL 标签去执行 SQL 语句并不会提交事务需要我们自己提交}    Testpublic void testSelectAllStudents(){ListStudent students studentDao.selectAllStudents();for (Student student :students) {System.out.println(student);}}    Testpublic void testSelectStudentById(){Student student studentDao.selectStudentById(3L);System.out.println(student);}    Testpublic void testSelectStudentsByMap(){MapString,Student map new HashMap();Student student new Student();student.setName(晓晓);student.setAge(21);map.put(map, student);ListStudent students studentDao.selectStudentsByMap(map);for (Student stu :students) {System.out.println(stu);}}    Testpublic void testSelectStudentsByName(){ListStudent students studentDao.selectStudentsByName(晓);for (Student student :students) {System.out.println(student);}} }然后我们在获取动态代理对象处打一个断点以调试方式执行 然后我们往下走一步查看可以看到生成的 studentDao 是一个代理对象。 然后我们重新来调试执行一次我们跟进到 getMapper() 方法中不断跟进去可以看到Mybatis 的 Mapper 动态代理使用的就是 JDK 的动态代理来创建代理对象的。 五、动态 SQL 1. 动态 SQL 主要用于解决查询条件不确定的情况在程序运行期间根据用户提交的查询条件进行查询提交的条件不同执行的 SQL 也不同若将每个不同条件逐一列出在进行排列组合将会出现大量的 SQL 语句我们可以使用动态 SQL 来解决这样的问题。 2. 动态 SQL 的示例3. 动态 SQL 概述 动态 SQL即通过 Mybatis 提供的各种标签对条件做出判断以实现动态拼接 SQL 语句, 常用的动态 SQL 标签有if、where、choose、foreach等等有意思的是这些动态 SQL 标签和 JSTL 语法非常相似。 4. 开发步骤 (1) 准备数据库表 (2) 定义实体类package com.edu.beans;public class Student {private long id;private String name;private int age;private double score;    public Student() {}    public Student(String name, int age, double score) {this.name name;this.age age;this.score score;}    public Student(long id, String name, int age, double score) {this.id id;this.name name;this.age age;this.score score;}    public long getId() {return id;}    public void setId(long id) {this.id id;}    public String getName() {return name;}    public void setName(String name) {this.name name;}    public int getAge() {return age;}    public void setAge(int age) {this.age age;}    public double getScore() {return score;}    public void setScore(double score) {this.score score;}    Overridepublic String toString() {return Student{ id id , name name \ , age age , score score };} }(3) 测试类package com.edu.test;import com.edu.dao.IStudentDao; import com.edu.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before;//JUnit 的测试类 public class DynamicSQLTest {private SqlSession session;private IStudentDao studentDao;    Before //被 Before 注解的方法为初始化方法它会在每次 Test 修饰的方法执行之前执行一次完成初始化工作public void setUp(){session MybatisUtils.getSqlSession();//获取 Dao 层接口的动态代理对象(Mapper 的动态代理)studentDao session.getMapper(IStudentDao.class);}    After //被 After 注解的方法为销毁方法它会在 Test 修饰的方法执行之后执行一次完成资源的销毁工作public void tearDown(){if(session ! null){session.close();}} }5. if 标签 该标签中的 test 属性为 true 时他会将它包含的 SQL 片段拼接在其所在 SQL 语句中 示例查询满足动态条件的学生信息 (1) 修改 Dao 层接口package com.edu.dao;import com.edu.beans.Student;import java.util.List;public interface IStudentDao {ListStudent selectStudentIf(Student student); }(2) 修改 IStudentDao.xml 映射文件 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd !--mybatis-3-mapper.dtd是 mybatis 映射文件的约束文件约束文件的位置在 mybatis 的核心jar包(mybatis-3.3.0.jar)的这里org.apache.ibatis.builder.xml.mybatis-3-mapper.dtd -- mapper namespacecom.edu.dao.IStudentDaoselect idselectStudentIf resultTypeStudent!--这里的 where 11 是为了保证如果后面的 if 语句不满足前面的 SQL 语句也是一条有效的语句if 条件满足也是一条有效的语句--select id,name,age,score from student where 11!--如果传过来的 name 属性不为 null且不是空字符串那么就拼接如下的 SQL 片段--if testname ! null and name ! and name like % #{name} %/if!--如果传过来的 age 0就拼接如下的 SQL 片段--if testage 0and age #{age}/if/select /mapper (3) 测试类 DynamicSQLTest6. where标签 if 标签有个比较麻烦的地方就是必须使用 where 11 子句因为若 where 条件后if 都不满足需要 where 11 来保证前面的 SQL 语句是一个有效的语句当然if满足时 where 11 也能保证拼接后的 SQL 语句是有效语句。使用where标签在有条件的时候会自动添加 where 子句没有条件的时候不会添加 where 子句  (1) 修改 IStudentDao.java package com.edu.dao;import com.edu.beans.Student;import java.util.List;public interface IStudentDao {ListStudent selectStudentIf(Student student);ListStudent selectStudentWhere(Student student); } (2) 修改 IStudentDao.xml 映射文件 select idselectStudentWhere resultTypeStudent!--这里的 where 11 是为了保证如果后面的 if 语句不满足前面的 SQL 语句也是一条有效的语句if 条件满足也是一条有效的语句--select id,name,age,score from student!--如果下面的 if 语句都不满足条件则不会条件 where 子句如果有一个 if 条件满足就会添加 where 子句--where!--如果传过来的 name 属性不为 null且不是空字符串那么就拼接如下的 SQL 片段--if testname ! null and name ! !--第一个 if 语句可以省略 and但是其他的 if 语句必须加上 and--and name like % #{name} %/if!--如果传过来的 age 0就拼接如下的 SQL 片段--if testage 0!--其他 if 语句的 and 不能省略--and age #{age}/if/where /select (3) 测试类 DynamicSQLTest 7. choose...when...otherwise类似于switch...case 对于 choose 标签会从第一个 when 开始逐个向后进行条件判断若某个 when 的 test 属性值为 true 的情况则拼接其 SQL 片段然后直接结束 choose 标签其他的不执行 when直到所有的 when 都不满足时执行 otherwise。 (1) 修改 IStudentDao.java(2) 修改StudentDao.xml(3) 测试类 从上可知“王”的姓名满足第一个 when 之后就不会去判断第一个 age 的 when。 8. foreach 标签遍历集合 (1) 遍历数组 1) 修改 IStudentDao.javapackage com.edu.dao;import com.edu.beans.Student;import java.util.List;public interface IStudentDao {ListStudent selectStudentIf(Student student);ListStudent selectStudentWhere(Student student);ListStudent selectStudentChoose(Student student);ListStudent selectStudentForEachArray(Object[] studentIds); }2) 修改 IStudentDao.xml select idselectStudentForEachArray resultTypeStudentselect id,name,age,score from student!--这里的 array 是指接口方法传过来的参数是一个数组而且这里只是使用 array 这个名字不能是其他名称array.length 表示数组的长度--if testarray ! null and array.length 0where id in!--collection待遍历的集合这里是数组统一写成 arrayopen遍历项放入的地方这里以(开始item每一个遍历项separator每个遍历项之间以,分割close遍历项放入的地方这里以)结束最终遍历出来得到的 SQL 语句形式如下select id,name,age,score from student where id in (2,3,4)--foreach collectionarray open( itemmyid separator, close)#{myid}/foreach/if /select 3) 测试类(2) 遍历泛型为基本类型的 List 1) 修改 IStudentDao.java package com.edu.dao;import com.edu.beans.Student;import java.util.List;public interface IStudentDao {ListStudent selectStudentIf(Student student);ListStudent selectStudentWhere(Student student);ListStudent selectStudentChoose(Student student);ListStudent selectStudentForEachArray(Object[] studentIds);ListStudent selectStudentForEachList(ListInteger studentIds); } 2) 修改 IStudentDao.xml select idselectStudentForEachList resultTypeStudentselect id,name,age,score from student!--这里的 list 是指接口方法传过来的参数是一个 List而且这里只是使用 list 这个名字不能是其他名称list.size 表示集合的大小--if testlist ! null and list.size 0where id in!--collection待遍历的集合这里是List统一写成 listopen遍历项放入的地方这里以(开始item每一个遍历项separator每个遍历项之间以,分割close遍历项放入的地方这里以)结束最终遍历出来得到的 SQL 语句形式如下select id,name,age,score from student where id in (2,3,5)--foreach collectionlist open( itemmyid separator, close)#{myid}/foreach/if /select 3) 测试类 (3) 遍历泛型为自定义类型的 List 1) 修改 IStudentDao.java package com.edu.dao;import com.edu.beans.Student;import java.util.List;public interface IStudentDao {ListStudent selectStudentIf(Student student);ListStudent selectStudentWhere(Student student);ListStudent selectStudentChoose(Student student);ListStudent selectStudentForEachArray(Object[] studentIds);ListStudent selectStudentForEachList(ListInteger studentIds);ListStudent selectStudentForEachList2(ListStudent students); } 2) 修改 IStudentDao.xml select idselectStudentForEachList2 resultTypeStudentselect id,name,age,score from student!--这里的 list 是指接口方法传过来的参数是一个 List而且这里只是使用 list 这个名字不能是其他名称list.size 表示集合的大小--if testlist ! null and list.size 0where id in!--collection待遍历的集合这里是List统一写成 listopen遍历项放入的地方这里以(开始item每一个遍历项separator每个遍历项之间以,分割close遍历项放入的地方这里以)结束最终遍历出来得到的 SQL 语句形式如下select id,name,age,score from student where id in (2,3,5)--foreach collectionlist open( itemstudent separator, close)#{student.id}/foreach/if /select 3) 测试类9. sql 标签 sql 标签可以用来定义一个 SQL 片段以便复用其他地方要用到的时候通过 include 标签引入即可 (1) 修改 IStudentDao.java package com.edu.dao;import com.edu.beans.Student;import java.util.List;public interface IStudentDao {ListStudent selectStudentIf(Student student);ListStudent selectStudentWhere(Student student);ListStudent selectStudentChoose(Student student);ListStudent selectStudentForEachArray(Object[] studentIds);ListStudent selectStudentForEachList(ListInteger studentIds);ListStudent selectStudentForEachList2(ListStudent students);ListStudent selectStudentBySQL(Student student); } (2) 修改 IStudentDao.xmlsql idselectHeadselect id,name,age,score from student/sqlselect idselectStudentBySQL resultTypeStudentinclude refidselectHead/!--如果下面的 if 语句都不满足条件则不会条件 where 子句如果有一个 if 条件满足就会添加 where 子句--where!--如果传过来的 name 属性不为 null且不是空字符串那么就拼接如下的 SQL 片段--if testname ! null and name ! !--第一个 if 语句可以省略 and但是其他的 if 语句必须加上 and--and name like % #{name} %/if!--如果传过来的 age 0就拼接如下的 SQL 片段--if testage 0!--其他 if 语句的 and 不能省略--and age #{age}/if/where/select (3) 测试类10. Param 注解 Mybatis 中的 Param 注解该注解指定的名称要作为 SQL 语句中动态参数的前缀例如 (1) 修改 IStudentDao.java package com.edu.dao;import com.edu.beans.Student; import org.apache.ibatis.annotations.Param;import java.util.List;public interface IStudentDao {ListStudent selectStudentIf(Student student);ListStudent selectStudentWhere(Student student);ListStudent selectStudentChoose(Student student);ListStudent selectStudentForEachArray(Object[] studentIds);ListStudent selectStudentForEachList(ListInteger studentIds);ListStudent selectStudentForEachList2(ListStudent students);ListStudent selectStudentBySQL(Student student);ListStudent selectStudentWhereParam(Param(stu) Student student, Param(myage) int age); } (2) 修改 IStudenDao.xml select idselectStudentWhereParam resultTypeStudentselect id,name,age,score from student!--如果下面的 if 语句都不满足条件则不会条件 where 子句如果有一个 if 条件满足就会添加 where 子句--where!--如果传过来的 name 属性不为 null且不是空字符串那么就拼接如下的 SQL 片段--!--如果接口方法参数是一个对象并且使用的 Param 注解那么再使用方法参数时要加上Param指定的前缀 stu.--if teststu.name ! null and stu.name ! !--第一个 if 语句可以省略 and但是其他的 if 语句必须加上 and--and name like % #{stu.name} %/if!--如果传过来的 age 0就拼接如下的 SQL 片段--!--如果接口方法参数是基本类型且被 Param 注解那么这里的动态参数名就是 Param 指定的名称--if testmyage 0!--其他 if 语句的 and 不能省略--and age #{myage}/if/where /select (3) 测试类
http://www.pierceye.com/news/810884/

相关文章:

  • 公众号注册入口官网seo排名的影响因素有哪些
  • 化妆品网站素材wordpress广告插件中文
  • 设计iphone手机网站网站开发怎么才能接到私活
  • 做网站美工排版提升学历英语翻译
  • 旅游网站建设服务对象微信第三方做网站需要费用吗
  • 能下载的网站soap公司网站
  • 肇庆网站推广排名花都网页设计
  • 网站后台素材wordpress适用linux
  • 开发一个app大概需要多少钱seo按照搜索引擎的什么对网站
  • 比较好的网站建设公司电话珠海开发网站公司
  • 响应式网站怎么做无缝轮播图网站域名在哪里
  • 大连网站建设设计公司哪家好临海市城乡建设规划局网站
  • 福州商城网站建设网站建设的域名和空间价位
  • 如何做外卖网站网页设计照片
  • 长沙河西做网站自己做的网站怎么设置文件下载
  • 计算机本科论文 网站建设wordpress如何添加关键词和描述
  • div嵌套影响网站收录唐山做网站多少钱
  • 做网站挂谷歌广告赚钱吗windows优化大师自动安装
  • 网站下一步工作怎么做网上最好购物网站
  • OA 公司网站 铁道建设报自驾游网站建设
  • wordpress建站网站根目录短视频怎么赚钱
  • 亳州网站开发公司wordpress 添加分享
  • 如何查询网站接入信息移动网站开发框架
  • 河南做网站的百度竞价推广收费标准
  • 深圳的深圳的网站建设公司校园网站建设方向
  • 电商网站建设 解决方案的设计营销策略都有哪些方面
  • 菏泽网站建设兼职凡科网制作网站教程
  • 实验一 电子商务网站建设与维护北京网站设计培训学校
  • 周到的网站建设合肥建筑网站大全
  • 国外互联网资讯网站南宁网站制作费用