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

给网站写文章怎么做的网站升级建设方案

给网站写文章怎么做的,网站升级建设方案,南昌建站价格,网页设计行业市场分析准备工作 ○ 创建module#xff08;Maven的普通Java模块#xff09;#xff1a;mybatis-002-crud ○ pom.xml ■ 打包方式jar ■ 依赖#xff1a; ● mybatis依赖 ● mysql驱动依赖 ● junit依赖 ● logback依赖 ○ mybatis-config.xml放在类的根路径下 ○ CarMapper.xml放…准备工作 ○ 创建moduleMaven的普通Java模块mybatis-002-crud ○ pom.xml ■ 打包方式jar ■ 依赖 ● mybatis依赖 ● mysql驱动依赖 ● junit依赖 ● logback依赖 ○ mybatis-config.xml放在类的根路径下 ○ CarMapper.xml放在类的根路径下一张表对应一个Mapper ○ logback.xml放在类的根路径下 ○ 提供com.sunsplanter.mybatis.utils.SqlSessionUtil工具类 ○ 创建测试用例com.sunsplanter.mybatis.CarMapperTest 1.insertCreate 1.1使用map传参完成insert 在第一个mybatis程序中的CarMapper.xml文件中,Sql语句是写死的 insert idinsertCar//插入的东西全部写死了但实际使用中我们不可能预知用户需要insert的数据//一定是通过某种方法留空然后前端的form表单传来数据再将数据与sql语句结合提交insert into t_car(id,car_num,brand,guide_price,produce_time,car_type)values(null,1003,toyota,30.00,2000-10-11,燃油车)/insert在JDBC中我们是这样留空的 // JDBC中使用 ? 作为占位符。 String sql insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(?,?,?,?,?);在MyBatis中我们这样做 在Java程序中将数据放到Map集合中 在sql语句中使用 #{map集合的key} 来完成传值#{} 等同于JDBC中的 ? #{}就是占位符 package com.sunsplanter.mybatis;import com.sunsplanter.mybatis.utils.SqlSessionUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.HashMap; import java.util.Map;public class CarMapperTest {Testpublic void testInsertCar() {//前端传来数据了用map将这些数据封装起来MapString, Object map new HashMap();map.put(k1, 103);map.put(k2, 奔驰E300L);map.put(k3, 50.3);map.put(k4, 2020-10-01);map.put(k5, 燃油车);//调用封装好的方法获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession(); // 执行SQL语句第一个参数是sql段的id第二参数是要传入的对象使用map集合给sql语句传递数据int count sqlSession.insert(insertCar, map);System.out.println(插入了几条记录 count);} }?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace先随便写-- mapper namespaceTBD insert idinsertCar !--t_car后接的参数指明要插入表中的哪个单元,而#{} 的里面必须填写map集合的key,以正确获取map的value--insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})/insert /mapper1.2使用POJO传参完成insert 如果采用POJO传参#{} 里写的是get方法的方法名去掉get之后将剩下的单词首字母变小写例如getAge对应的是#{age}getUserName对应的是#{userName}如果这样的get方法不存在会报错。 ● 第一步新建一个pojo包包下定义一个pojo类Car提供相关属性。 package com.sunsplanter.mybatis.pojo;public class Car { //long还是Long若使用包装类其提供了更多的方法可供调用并且支持值为null的情况。private Long id;private String carNum;private String brand;private Double guidePrice;private String produceTime;private String carType;Overridepublic String toString() {return Car{ id id , carNum carNum \ , brand brand \ , guidePrice guidePrice , produceTime produceTime \ , carType carType \ };}public Car() {}public Car(Long id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {this.id id;this.carNum carNum;this.brand brand;this.guidePrice guidePrice;this.produceTime produceTime;this.carType carType;}public Long getId() {return id;}public void setId(Long id) {this.id id;}public String getCarNum() {return carNum;}public void setCarNum(String carNum) {this.carNum carNum;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand brand;}public Double getGuidePrice() {return guidePrice;}public void setGuidePrice(Double guidePrice) {this.guidePrice guidePrice;}public String getProduceTime() {return produceTime;}public void setProduceTime(String produceTime) {this.produceTime produceTime;}public String getCarType() {return carType;}public void setCarType(String carType) {this.carType carType;} }Test public void testInsertCarByPOJO(){// 创建POJO封装数据Car car new Car();//前端传来数据了用set方法将这些数据传入POJO属性car.setCarNum(103);car.setBrand(奔驰C200);car.setGuidePrice(33.23);car.setProduceTime(2020-10-11);car.setCarType(燃油车);//调用封装好的方法获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession();// 执行SQL语句。第一个参数是sql段的id第二参数是要传入的对象使用POJO对象给sql语句传递数据int count sqlSession.insert(insertCarByPOJO, car);System.out.println(插入了几条记录 count); }insert idinsertCarByPOJO!--#{} 里写的是POJO的属性名--insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType}) /insert2.Delete 需求根据car_num进行删除。 Test public void testDeleteByCarNum(){//调用封装好的方法获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession();// 执行SQL语句。第一个参数是sql段的id第二个参数是要删除的“car_num”int count sqlSession.delete(deleteByCarNum, 102);System.out.println(删除了几条记录 count); }delete iddeleteByCarNum !--当占位符只有一个的时候${} 里面的内容可以随便写。--delete from t_car where car_num #{SuiBianXie} /delete2.1用POJO传参Update 需求修改id34的Car信息car_num为102brand为比亚迪汉guide_price为30.23produce_time为2018-09-10car_type为电车 update idupdateCarByPOJOupdate t_car set car_num #{carNum}, brand #{brand}, guide_price #{guidePrice}, produce_time #{produceTime}, car_type #{carType} where id #{id} /updateTestpublic void testUpdateCarById_POJO(){// 准备数据Car car new Car();car.setId(5L);car.setCarNum(102);car.setBrand(比亚迪汉);car.setGuidePrice(30.23);car.setProduceTime(2018-09-10);car.setCarType(电车);// 获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession();// 执行SQL语句int count sqlSession.update(updateCarById_Map, car);System.out.println(更新了几条记录 count);}2.2用Map传参Update 需求一致XML代码也一致仅仅是java代码改为用Map传参 Testpublic void testUpdateCarById_Map(){//前端传来数据了用map将这些数据封装起来MapString, Object map new HashMap();map.put(id, 5L);map.put(carNum, 104);map.put(brand, XiaomiSU7);map.put(guidePrice, 18.9);map.put(produceTime, 2024-04-01);map.put(carType, 电动车);//调用封装好的方法获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession();// 执行SQL语句int count sqlSession.update(updateCarById_Map,map);System.out.println(更新了几条记录 count);}3.1selectRetrieve一条 select语句和其它语句不同的是查询会返回一个结果集ResultSet。然而语句是 Object car sqlSession.selectOne(selectCarById, 1);因此必须将ResultSet转化成成一个Java对象。mybatis查询之后返回一个Java对象的话至少你要告诉mybatis返回一个什么类型的Java对象可以在标签中添加resultType属性用来指定查询要转换的类型 select idselectCarById resultTypecom.powernode.mybatis.pojo.Carselect * from t_car where id #{id} /selectTestpublic void testSelectCarById(){// 获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession();//查一条记录用selectOne方法Car car sqlSession.selectOne(selectCarById, 1);System.out.println(car);}然而这样输出的结果是 原因是例如对于汽车编号这个字段在POJO类和mapper文件中为carNum但在数据库表中为car_num这样当然查不到。 改为 !--虽然结果是List集合但是resultType属性需要指定的是List集合中元素的类型。-- select idselectCarById resultTypecom.sunsplanter.mybatis.pojo.Carselect !--记得使用as起别名让查询结果的字段名和java类的属性名对应上。方法是数据库中字段名 as Java中属性名--id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType from t_car where id #{id} /select3.1selectRetrieve多条 取一条时用selectOne方法返回ResultSet转化成一个指定对象这个指定对象只要定义个Car car接受即可。 取多条时用selectList方法返回多个ResultSet转换成多个指定对象这多个对象定义一个List来接受否则要定义Car car1 car2… !--虽然结果是List集合但是resultType属性需要指定的是List集合中元素的类型。-- select idselectCarAll resultTypecom.sunsplanter.mybatis.pojo.Car!--记得使用as起别名让查询结果的字段名和java类的属性名对应上。--selectid, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carTypefromt_car /selectTest public void testSelectCarAll(){// 获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession();// 执行SQL语句ListObject cars sqlSession.selectList(selectCarAll);// 输出结果cars.forEach(car - System.out.println(car)); }
http://www.pierceye.com/news/119355/

相关文章:

  • 张店网站制作设计公司自己做视频网站怎么让加载速度变快
  • 杭州有哪些做网站的公司好大连seo网站
  • 做网站优化公司wordpress电子书插件
  • 可以接单做3d网站东莞app制作公司
  • 请详细说明网站开发流程及原则网站图片代码
  • 网页设计基础学什么seochan是什么意思
  • 汽车网站网页设计设计师网站推荐家装
  • 游戏交易网站怎么做wordpress 分词
  • 网站难做吗建设企业网站的需求
  • 网站开发设计文档模板wordpress5.2下载
  • 网站备案后要做什么wordpress 黑色
  • 池州建设机械网站中国房地产100强名单
  • 怎么在虚拟主机上建网站wordpress 更新feed
  • 内设网站wordpress 注册验证
  • 全景网站建设做米业的企业网站
  • 珠海哪个建设网站建设好ui网站界面设计
  • 中国有名的模版网站电商建设网站
  • 网站改域名合肥网站建设企业
  • 建网站需要软件c 怎么做能让窗体访问网站
  • 呼家楼网站建设黄骅市官方网站
  • 空包网站分站怎么做手表网站哪个最好知乎
  • 手机商城建站系统网站建设有关建筑网站建设方案
  • 做网站需要具备的基础条件wordpress 修改后台
  • 网站做点击收费标准重庆建设工业集团
  • wordpress个人网站备案管理广州网站搭建费用
  • 英文网站建设的原因泰安房产信息网网签查询
  • 怎么将网站做成html如何寻找一批做网站的公司
  • 苏州网站建设孙峰南宁站建站时间
  • 海淀手机网站设计公司苏州建设工程有限公司
  • 兰州网站设计制作王妃貌美还狠凶