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

高端做网站公司外链购买交易平台

高端做网站公司,外链购买交易平台,郑州疫情死了上万人,字体版权登记网站目录 一,查询 - select 1.1 全列查询 1.2 指定列查询 1.3 赋值问题 方法一:起别名 方法二:结果映射 方法三:添加配置 二,新增 - Insert 2.1 使用对象插入 2.2 获取主键 三,删除 - Delete 四&am…

目录

一,查询 - @select

1.1 全列查询

1.2 指定列查询

1.3 赋值问题

方法一:起别名

方法二:结果映射

方法三:添加配置

二,新增 - @Insert

2.1 使用对象插入

2.2 获取主键

三,删除 - @Delete

四,修改 - @Update 

补:MyBatis 的配置


一,查询 - @select

1.1 全列查询

@Mapper
public interface UserInfoMapper {@Select("select * from userInfo")public UserInfo queryAll();
}

1.2 指定列查询

@Mapper
public interface UserInfoMapper {//注意:MyBatis中方法名不允许重复!!!!//当只有一个参数时,sql参数与方法参数可以不同@Select("select * from userInfo where id = #{id}")public UserInfo queryUserInfoById(Integer id);//当需要对参数进行重命名时需要使用@Param注释//如果不使用@Param,那么sql参数与方法参数必须相同//注:如果使用阿里云创建的springboot项目,必须使用@Param注解@Select("select * from userInfo where id = #{id} and username = #{username}")public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);
}

 注:加了@Param注解后,sql语句中的参数名只能与@Param()的参数名匹配!!!

1.3 赋值问题

可以发现,在赋值时,deleteFlag,createTime,updateTime 并没有被赋值,这是为什么?原因分析:

  • 当⾃动映射查询结果时,MyBatis 会获取结果中返回的列名并在 Java 类中查找相同名字的属性(忽略⼤⼩写)。 这意味着如果发现了 ID 列和 id 属性,MyBatis 会将列 ID 的值赋给 id 属性
  • 所以没有赋值的原因就是MySQL中的字段名与Java对象中的属性名不相同

 

方法一:起别名

    @Select("select id, username, password, age, gender, phone," +"delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userInfo " +"where id = #{id} and username = #{username}")public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);

方法二:结果映射

    @Select("select * from userInfo where id = #{id}")@Results(id = "resultMap",value = {@Result(column = "delete_flag", property = "deleteFlag"),@Result(column = "create_time", property = "createTime"),@Result(column = "update_time", property = "updateTime")})public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);@Select("select * from userInfo where id = #{id}")@ResultMap(value = "resultMap")//使用过@Results注解后,其他方法可以直接使用@ResultMap注解获得相同效果public UserInfo queryUserInfoById(Integer id);

方法三:添加配置

mybatis:configuration:map-underscore-to-camel-case: true #驼峰自动转换

二,新增 - @Insert

2.1 使用对象插入

使用多个参数插入数据与上述查询的使用方法一样,这里不过多赘述,这里讲一下如何使用对象插入数据:

@Mapper
public interface UserInfoMapper {//这里演示一下使用对象插入@Insert("insert into userInfo (username, password, age, gender, phone) " +"values (#{username}, #{password}, #{age}, #{gender}, #{phone})")public Integer insert(UserInfo userInfo);//使用@Param注解@Insert("insert into userInfo (username, password, age, gender, phone) " +"values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone})")public Integer insertByParam(@Param("userInfo") UserInfo userInfo);//这里的返回值是成功修改了几行数据,也可以返回void
}

2.2 获取主键

在有些情景中,我们需要获取到新插入数据的 id,如果想要拿到自增 id,需要在Mapper接口方法上添加一个@Option注解:

    @Options(useGeneratedKeys = true, keyProperty = "id")@Insert("insert into userInfo (username, password, age, gender, phone) " +"values (#{username}, #{password}, #{age}, #{gender}, #{phone})")public Integer insert(UserInfo userInfo);
  • useGeneratedKeys = true:表示告诉MyBatis使用JDBC的getGeneratedKeys方法来检索数据库内部生成的主键(如果数据库支持的话)
  • keyProperty = ''id'':这个属性指定了userInfo对象中的 id 属性接收数据库生成的主键

三,删除 - @Delete

    @Delete("delete from userInfo where id = #{id}")public Integer deleteById(Integer id);

四,修改 - @Update 

    @Update("update userInfo set username = #{username} where id = #{id}")public Integer updateById(@Param("username") String username, @Param("id") Integer id);

补:MyBatis 的配置

mybatis:configuration: # 配置打印 MyBatis⽇志map-underscore-to-camel-case: true #驼峰自动转换log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句mapper-locations: classpath:mapper/**Mapper.xml #使用xml操作数据库时会用到

http://www.dt0577.cn/news/44172.html

相关文章:

  • 方案解决网站南昌seo优化
  • 那些网站可以上传自己做的视频站长之家seo工具包
  • 重庆品牌网站建设公司排名seo推广费用
  • unity 做网站百度服务商
  • 邯郸网站设计怎么开发泉州关键词快速排名
  • 做b2b需要建网站吗我也要投放广告
  • 少儿编程网站站长之家是什么
  • 模板网站与定制网站的定位网站seo检测
  • 大连招标信息网湖北网络推广seo
  • 网站制作的基本流程搜狗站长管理平台
  • 网站改版前端流程经验网络广告文案案例
  • 如何在阿里云建设网站培训心得体会1500字
  • 做平台的网站有哪些内容济南百度
  • 互动平台表示公司帮助国内客户进行新冠药物研发seo评测论坛
  • 怎么增加网站关键词库怎么做一个自己的网站
  • 广东官方移动网站建设哪家好网站监测
  • 公司网站换服务器怎么做论坛推广网站
  • wordpress 订阅天津seo外包
  • 三乡网站建设百度seo排名优化助手
  • 怎样做网站收录百度推广是什么工作
  • 旅游营销网站开发湖南长沙最新情况
  • 做商城网站需要备案什么域名百度官方客服
  • 做摄影和后期的兼职网站网络推广策划
  • 赣州58同城网长沙百度网站优化
  • 推广网上国网有什么好处网站seo外链平台
  • 海外短视频怎么下载辽宁好的百度seo公司
  • 企业开办网站拓客公司联系方式
  • 做网站绘制蓝图的步骤网络热词2022
  • 揭阳住房和城乡建设厅网站百度快速排名软件
  • 学生个人网页内容排版设计作品代做seo关键词排名