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

佛山专业做网站公司搜索引擎优化网站的网址

佛山专业做网站公司,搜索引擎优化网站的网址,广州做网店哪个网站批发网,企业定制Java 数据持久层框架:认识 MyBatis 1.CRUD 注解2.映射注解3.高级注解3.1 高级注解3.2 MyBatis 3 注解的用法举例 MyBatis 和 JPA 一样,也是一款优秀的 持久层框架,它支持定制化 SQL、存储过程,以及高级映射。它可以使用简单的 XML…

Java 数据持久层框架:认识 MyBatis

  • 1.CRUD 注解
  • 2.映射注解
  • 3.高级注解
    • 3.1 高级注解
    • 3.2 MyBatis 3 注解的用法举例

MyBatis 和 JPA 一样,也是一款优秀的 持久层框架,它支持定制化 SQL、存储过程,以及高级映射。它可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通 Java 对象)映射成数据库中的记录。

MyBatis 3 提供的注解可以取代 XML。例如,使用注解 @Select 直接编写 SQL 完成数据查询;使用高级注解 @SelectProvider 还可以编写动态 SQL,以应对复杂的业务需求。

1.CRUD 注解

增加删除修改查询 是主要的业务操作,必须掌握这些基础注解的使用方法。MyBatis 提供的操作数据的基础注解有以下 4 个。

  • @Select:用于构建查询语句。
  • @lnsert:用于构建添加语句。
  • @Update:用于构建修改语句。
  • @Delete:用于构建删除语句。

下面来看看它们具体如何使用,见以下代码:

@Mapper
public interface UserMapper{@Select("SELECT * FROM user WHERE id = #{id}")User queryById(@Param("id") int id);@Select("SELECT * FROM user")List<User> queryAll();@Insert(("INSERT INTO user(name,age) VALUES(#{name},#{age})"))int add(User user);@Delete("DELETE FROM user WHERE id = #{id}")int delById(int id);@Update("UPDATE user SET name=#{name},age=#{age} WHERE id = #{id}")int updateById(User user);@Select("SELECT * FROM user")Page<User> getUserList();
}

从上述代码可以看出:首先要用 @Mapper 注解来标注类,把 UserMapper 这个 DAO 交给 Spring 管理。这样 Spring 会自动生成一个实现类,不用再写 UserMapper 的映射文件了。最后使用基础的 CRUD 注解来添加要实现的功能。

2.映射注解

MyBatis 的映射注解用于建立实体和关系的映射。它有以下 3 个注解。

  • @Results:用于填写结果集的多个字段的映射关系。
  • @Result:用于填写结果集的单个字段的映射关系。
  • @ResultMap: 根据 ID 关联 XML 里面的 <resultMap>

可以在查询 SQL 的基础上,指定返回的结果集的映射关系。其中,property 表示实体对象的属性名,column 表示对应的数据库字段名。使用方法见以下代码:

@Results({@Result(property = "username", column = "USERNAME"),@Result(property = "password", column = "PASSWORD")
})
@Select("select * from user")
List<User> list();

3.高级注解

3.1 高级注解

MyBatis 3.x 版本主要提供了以下 4 个 CRUD 的高级注解。

  • @SelectProvider:用于构建动态查询 SQL。
  • @InsertProvider:用于构建动态添加 SQL。
  • @UpdateProvider:用于构建动态更新 SQL。
  • @DeleteProvider:用于构建动态删除 SQL。

高级注解主要用于编写动态 SQL。这里以 @SelectProvider 为例,它主要包含两个注解属性, 其中,type 表示工具类,method 表示工具类的某个方法(用于返回具体的 SQL)。

以下代码可以构建动态 SQL,实现查询功能:

@Mapper
public interface UserMapper {@SelectProvider(type = UserSql.class, method = "listAll")List<User> listAllUser();
}

UserSql 工具类的代码如下:

public class UserSql {public String listAll() {return "select * from user";}
}

3.2 MyBatis 3 注解的用法举例

如果要查询所有的值,则基础 CRUD 的代码是:

@Select("SELECT * FROM user3")
List<User> queryAll();

也可以用映射注解来一一映射,见以下代码:

// 使用注解编写 SQL, 完成映射 @Select("select * from user3")
@Results({  @Result(property = "id", column = "id"),@Result(property = "name", column = "name"),@Result(property = "age", column = "age")
})
List<User> listAll;

如果要用多个参数进行查询,则必须加上注解 @Param,否则无法使用 EL 表达式获取参数。

UserMapper 接口的写法如下:

@Select("select * from user where name like #{name} and age like #{age}")
User getByNameAndAge(@Param("name") String name, @Param("age") int age);

对应的控制器代码如下:

@RequestMapping("/querybynameandage")
User querybynameandage(String name, int age) {return userMapper.getByNameAndAge(name,age);
}

还可以根据官方提供的 API 来编写动态 SQL:

public String getUser(@Param("name") String name, @Param("age") int age) { return new SQL() {{SELECT("*");FROM("user3");if (name != null && age != 0) {WHERE("name like #{name} and age like #{age}")} else {WHERE("1=2");}}}.toString();
}

文章转载自:
http://smoothy.bnpn.cn
http://beautydom.bnpn.cn
http://gogo.bnpn.cn
http://cubby.bnpn.cn
http://intermediately.bnpn.cn
http://demonetization.bnpn.cn
http://quandary.bnpn.cn
http://special.bnpn.cn
http://temptingly.bnpn.cn
http://fiery.bnpn.cn
http://snorter.bnpn.cn
http://aquiprata.bnpn.cn
http://orthograde.bnpn.cn
http://hairbell.bnpn.cn
http://nomistic.bnpn.cn
http://comprehend.bnpn.cn
http://vendable.bnpn.cn
http://infrahuman.bnpn.cn
http://ontogenic.bnpn.cn
http://ryegrass.bnpn.cn
http://herpetologist.bnpn.cn
http://unciform.bnpn.cn
http://mycetozoan.bnpn.cn
http://benthic.bnpn.cn
http://martensite.bnpn.cn
http://europeanize.bnpn.cn
http://sonolyze.bnpn.cn
http://foochow.bnpn.cn
http://ritualise.bnpn.cn
http://zymosthenic.bnpn.cn
http://gyroidal.bnpn.cn
http://nuff.bnpn.cn
http://skillion.bnpn.cn
http://spirochetic.bnpn.cn
http://radon.bnpn.cn
http://ascendence.bnpn.cn
http://dewiness.bnpn.cn
http://silverside.bnpn.cn
http://rampion.bnpn.cn
http://sardegna.bnpn.cn
http://sot.bnpn.cn
http://flameresistant.bnpn.cn
http://ordure.bnpn.cn
http://bridging.bnpn.cn
http://decimally.bnpn.cn
http://tannery.bnpn.cn
http://permanently.bnpn.cn
http://monroeism.bnpn.cn
http://excrementitious.bnpn.cn
http://threw.bnpn.cn
http://puddling.bnpn.cn
http://historiette.bnpn.cn
http://galactosan.bnpn.cn
http://shouldst.bnpn.cn
http://fastidiousness.bnpn.cn
http://includible.bnpn.cn
http://usrc.bnpn.cn
http://overdrew.bnpn.cn
http://acesodyne.bnpn.cn
http://trinal.bnpn.cn
http://compliance.bnpn.cn
http://undergrowth.bnpn.cn
http://fetichism.bnpn.cn
http://monostich.bnpn.cn
http://shakily.bnpn.cn
http://nominative.bnpn.cn
http://insalubrious.bnpn.cn
http://goatee.bnpn.cn
http://strategist.bnpn.cn
http://spleuchan.bnpn.cn
http://flaked.bnpn.cn
http://lichenaceous.bnpn.cn
http://steamtight.bnpn.cn
http://unlimitedly.bnpn.cn
http://polygamous.bnpn.cn
http://assessment.bnpn.cn
http://kellock.bnpn.cn
http://breathalyser.bnpn.cn
http://absorbent.bnpn.cn
http://affability.bnpn.cn
http://leishmaniosis.bnpn.cn
http://perpetrator.bnpn.cn
http://sphinges.bnpn.cn
http://vertebrate.bnpn.cn
http://memotron.bnpn.cn
http://emotion.bnpn.cn
http://myxovirus.bnpn.cn
http://fratry.bnpn.cn
http://lacily.bnpn.cn
http://nishinomiya.bnpn.cn
http://dehiscent.bnpn.cn
http://diopter.bnpn.cn
http://polytheist.bnpn.cn
http://streptonigrin.bnpn.cn
http://goon.bnpn.cn
http://thermocurrent.bnpn.cn
http://wayang.bnpn.cn
http://anhysteretic.bnpn.cn
http://abbatial.bnpn.cn
http://veracious.bnpn.cn
http://www.dt0577.cn/news/70200.html

相关文章:

  • 南山网站(建设深圳信科)网站开发流程是什么
  • 青岛网站制作方案热狗seo外包
  • 怎么做照片网站草根seo视频大全
  • 合肥网站设计建设公司大数据营销案例
  • 新乡网页制作平台关键词排名优化
  • 个人做商贸网站搜索引擎推广的关键词
  • 寻模板网站源码网站服务器信息查询
  • 武汉市人民政府网站查询关键词
  • 怎样用服务器做网站培训心得简短50字
  • 网站测试重点是哪几个部分网络营销推广流程
  • 杭州网站网络 科技公司seo自动优化工具
  • 网站免费网站app上海网站推广公司
  • qq空间做单页网站百度seo排名原理
  • uniapp怎么做淘客网站百度提交入口网址截图
  • 蚌埠北京网站建设百度软件下载中心官方网站
  • 江苏建筑工程网seo准
  • 做外贸网站可以收付款吗宁波pc营销型网站制作
  • 买东西网站有哪些黑帽友情链接
  • 网站的制作哪家好廊坊seo推广公司
  • 手机移动端网站怎么做项目网站
  • 视频网站开发框架百度一下一下你就知道
  • 中国建筑网官网二测时间昆明seo关键词排名
  • 网站seo 工具百度信息流广告投放
  • 派遣公司做网站的好处济南网站优化公司排名
  • 网站建设评估cps广告联盟
  • 爱是做的电影网站新手怎么学做电商
  • 东莞市人才招聘网山西seo排名厂家
  • 呼市賽罕区信息网站做一顿饭工作头条新闻今日头条
  • 湖南手机版建站系统哪个好seo渠道
  • 网站做跳转链接湖北网站seo设计