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

建设摩托车官网官方网站搜索引擎seo关键词优化方法

建设摩托车官网官方网站,搜索引擎seo关键词优化方法,国内做新闻比较好的网站,建立网站有什么作用1. 前言 泛型编程自从 Java 5.0 中引入后已经超过15个年头了。对于现在的 Java 码农来说熟练使用泛型编程已经是家常便饭的事情了。所以本文就在不对泛型的基础使用在做说明了。 如果你还不会使用泛型的话,可以参考下面两个链接 Java 泛型详解The Java™ Tutorial…

1. 前言

泛型编程自从 Java 5.0 中引入后已经超过15个年头了。对于现在的 Java 码农来说熟练使用泛型编程已经是家常便饭的事情了。所以本文就在不对泛型的基础使用在做说明了。 如果你还不会使用泛型的话,可以参考下面两个链接

  • Java 泛型详解
  • The Java™ Tutorials (Lesson: Generics)

这篇文章就简答聊一下,我实际在开发工作中很少用的到泛型方法这个知识点,以及在实际项目中有哪些东西会使用到泛型。

2. 泛型方法

在阅读代码的时候我们经常会看到下面这样的方法 (这段代码摘自 java.util.AbstractCollection)

 public <T> T[] toArray(T[] a) {// Estimate size of array; be prepared to see more or fewer elementsint size = size();T[] r = a.length >= size ? a :(T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);Iterator<E> it = iterator();for (int i = 0; i < r.length; i++) {if (! it.hasNext()) { // fewer elements than expectedif (a == r) {r[i] = null; // null-terminate} else if (a.length < i) {return Arrays.copyOf(r, i);} else {System.arraycopy(r, 0, a, 0, i);if (a.length > i) {a[i] = null;}}return a;}r[i] = (T)it.next();}// more elements than expectedreturn it.hasNext() ? finishToArray(r, it) : r;
}

那么 pulic 关键字后面的那个 <T> 就是用来标记这个方法是一个泛型方法。 那什么是泛型方法呢。

官方的解释是这样的

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

通俗点来将就是将一个方法泛型化,让一个普通的类的某一个方法具有泛型功能。 如果在一个泛型类中增加一个泛型方法,那这个泛型方法就可以有一套独立于这个类的泛型类型。

通过一个简单的例子, 我们来看看

/*** GenericClass 这个泛型类是一个简单的套皮的 HashMap*/
public class GenericClass<K, V> {private Map<K, V> map = new HashMap<>();public V put(K key, V value) {return map.put(key, value);}public V get(K key) {return map.get(key);}// 泛型方法 genericMethod 可以接受一个全新的、作用域只限本函数的泛型类型Tpublic <T> T genericMethod(T t) {return t;}}

实际使用起来

GenericClass<String, Integer> map = new GenericClass<>();
// put 和 get 方法的参数必须使用定义时指定的 String 和 Integer
System.out.println(map.put("One", 1));
System.out.println(map.get("One"));
// 泛型方法 genericMethod 就可以接受一个 String 和 Integer 以外的类型
System.out.println(map.genericMethod(new Double(1.0)).getClass());

我们再来看看 JDK 中使用到泛型方法的例子。我们最常使用的泛型容器 ArrayList 中有个 toArray 方法。JDK 在它的实现中就提供了两个版本,其中一个就是泛型方法的版本

public class ArrayList<E> extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable 
{// 这是一个普通版本,返回一个Object的数组public Object[] toArray() {return Arrays.copyOf(elementData, size);}// 这是一个泛型方法的版本,将容器里存储的元素输出到 T[] 数组中。 其中 T 必须是 E 的父类,否则 System.arraycopy 会抛出 ArrayStoreException 异常      public <T> T[] toArray(T[] a) {if (a.length < size)// Make a new array of a's runtime type, but my contents:return (T[]) Arrays.copyOf(elementData, size, a.getClass());System.arraycopy(elementData, 0, a, 0, size);if (a.length > size)a[size] = null;return a;}
}

泛型方法总体上来说就是可以给与现有的方法实现上,增加一个更加灵活的实现可能。

3. 实战应用

在实际的项目中,对于泛型的使用,除了像倾倒垃圾一样往泛型容易里塞各种 java bean 和其他泛型对象。还能怎么使用泛型呢?

我们在实际的一些项目中,会对数据库中的一些表(多数时候是全部)先实现 CRUD (Create, Read, Update, Delete)的操作,并从这些操作中延伸出一些简单的 REST 风格的 WebAPI 接口,然后才会根据实际业务需要实现一些更复杂的业务接口。

大体上会是下面这个样子。


// 这是一个简单的 Entity 对象
// 通常现在的 Java 应用都会使用到 Lombok 和 Spring Boot
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name = "user")
public class User {@Idprivate Long id;private String username;private String password;
}// 然后这个是 DAO 接口继承自 spring-data 的 JpaRepository
public interface UserDao extends JpaRepository<User, Long> {
}// 在来是一个访问 User 资源的 Service 和他的实现
public interface UserService {List<User> findAll();Optional<User> findById(Long id);User save (User user)void deleteById(Long id);
}@Service
public class UserSerivceImpl implements UserService {private UserDao userDao;public UserServiceImpl(UserDao userDao) {this.userDao = userDao;}@Overridepublic List<User> findAll() {return this.dao.findAll();}@Overridepublic Optional<User> findById(Long id) {return this.dao.findById(id);}@Overridepublic User save(User user) {return this.dao.save(user);}@Overridepublic void deleteById(Long id) {this.dao.deleteById(id);}
}// 最后就是 WebAPI 的接口了
@RestController
@RequestMapping("/user/")
public class UserController{private UserService userService;public UserController(userService userService) {this.userService = userService;}@GetMapping@ResponseBodypublic List<User> fetch() {return this.userService.findAll();}@GetMapping("{id}")@ResponseBodypublic User get(@PathVariable("id") Long id) {// 由于是示例这里就不考虑没有数据的情况了return this.userService.findById(id).get();}@PostMapping@ResponseBodypublic User create(@RequestBody User user) {return this.userService.save(user);}@PutMapping("{id}")@ResponseBodypublic User update(@RequestBody User user) {return this.userService.save(user);}@DeleteMapping("{id}")@ResponseBodypublic User delete(@PathVariable("id") Long id) {User user = this.userService.findById(id);this.userService.deleteById(id);return user;}
}

大致一个表的一套相关接口就是这个样子的。如果你的数据库中有大量表的话,而且每个表都需要提供 REST 风格的 WebAPI 接口的话,那么这将是一个相当枯燥的而又及其容易出错的工作。

为了不让这项枯燥而又容易犯错的工作占去我们宝贵的私人时间,我们可以通过泛型和继承的技巧来重构从 Service 层到 Controller 的这段代码(感谢 spring-data 提供了 JpaRepository, 让我们不至于从 DAO 层重构)

3.1 Service 层的重构

首先是 Service 接口的重构,我们 Service 层接口就是定义了一组 CRUD 的操作,我们可以将这组 CRUD 操作抽象到一个父接口,然后所有 Service 层的接口都将继承自这个父接口。而接口中出现的 Entity 和主键的类型(上例中 User 的主键 id 的类型是 Long)就可以用泛型来展现。

// 这里泛型表示 E 来指代 Entity, ID 用来指代 Entity 主键的类型
public interface ICrudService<E, ID> {List<E> findAll();Optional<E> findById(ID id);E save(E e);void deleteById(ID id);
}// 然后 Service 层的接口,就可以简化成这样
public interface UserService extends ICrudService<User, Long> {
}

同样 Service 层的实现也可以使用相似的方法具体实现可以抽象到一个基类中。

// 相比 ICrudService 这里有多了一个泛型 T 来代表 Entity 对应的 DAO, 我们的每一个 DAO 都继承自
// spring-data 的 JpaRepository 所以,这里可以使用到泛型的边界
public abstract class AbstractCrudService<T extends JpaRepository<E, ID>, E, ID> {private T dao;public AbstractCrudService(T dao) {this.dao = dao;}public List<E> findAll() {return this.dao.findAll();}public Optional<E> findById(ID id) {return this.dao.findById(id);}public E save(E e) {return this.dao.save(e);}public void deleteById(ID id) {this.dao.deleteById(id);}
}// 那 Service 的实现类可以简化成这样
@Service
public class UserServiceImpl extends AbstractCrudService<UserDao, User, Long> implements UserService {public UserServiceImpl(UserDao dao) {supper(dao);}
}

同样我们可以通过相同的方法来对 Controller 层进行重构

// Controller 层的基类
public abstract class AbstractCrudController<T extends ICrudService<E, ID>, E, ID> {private T service;public AbstractCrudController(T service) {this.service = service;}@GetMapping@ResponseBodypublic List<E> fetch() {return this.service.findAll();}@GetMapping("{id}")@ResponseBodypublic E get(@PathVariable("id") ID id) {// 由于是示例这里就不考虑没有数据的情况了return this.service.findById(id).get();}@PostMapping@ResponseBodypublic E create(@RequestBody E e) {return this.service.save(e);}@PutMapping("{id}")@ResponseBodypublic E update(@RequestBody E e) {return this.service.save(e);}@DeleteMapping("{id}")@ResponseBodypublic E delete(@PathVariable("id") ID id) {E e = this.service.findById(id).get();this.service.deleteById(id);return e;}
}// 具体的 WebAPI
@RestController
@RequestMapping("/user/")
public class UserController extends AbstractCrudController<UserService, User, Long> {public UserController(UserService service) {super(service);}
}

经过重构可以消减掉 Servcie 和 Controller 中的大量重复代码,使代码更容易维护了。

4. 结尾

关于泛型就简单的说这些了,泛型作为 Java 日常开发中一个常用的知识点,其实还有很多知识点可以供我们挖掘,奈何本人才疏学浅,这么多年工作下来,只积累出来这么点内容。

文末放上示例代码的代码库:

  • GitHub入口
  • gitee入口

文章转载自:
http://dinncodiactinism.ydfr.cn
http://dinncofls.ydfr.cn
http://dinncomasterplan.ydfr.cn
http://dinncoanthropophagite.ydfr.cn
http://dinncoreligionise.ydfr.cn
http://dinncobuccaneerish.ydfr.cn
http://dinncoreceptionist.ydfr.cn
http://dinncodarius.ydfr.cn
http://dinncokazakh.ydfr.cn
http://dinncomegalomania.ydfr.cn
http://dinncounorganized.ydfr.cn
http://dinncopodolsk.ydfr.cn
http://dinnconip.ydfr.cn
http://dinncounsuccessfully.ydfr.cn
http://dinncomodus.ydfr.cn
http://dinncobloviate.ydfr.cn
http://dinncoteutophil.ydfr.cn
http://dinncoalfaqui.ydfr.cn
http://dinncothelitis.ydfr.cn
http://dinncointime.ydfr.cn
http://dinncoproduce.ydfr.cn
http://dinncoindication.ydfr.cn
http://dinncoadsorbent.ydfr.cn
http://dinncoegomaniac.ydfr.cn
http://dinncowin.ydfr.cn
http://dinncoulcerate.ydfr.cn
http://dinnconc.ydfr.cn
http://dinncoaphasic.ydfr.cn
http://dinncousername.ydfr.cn
http://dinncoboric.ydfr.cn
http://dinncoquaere.ydfr.cn
http://dinncocrotcheteer.ydfr.cn
http://dinncounpeace.ydfr.cn
http://dinncocampanological.ydfr.cn
http://dinncoencouraged.ydfr.cn
http://dinncocoalsack.ydfr.cn
http://dinncosemipermanent.ydfr.cn
http://dinncovenule.ydfr.cn
http://dinncopolymerization.ydfr.cn
http://dinncoanaheim.ydfr.cn
http://dinncoprevailing.ydfr.cn
http://dinncounneighbourly.ydfr.cn
http://dinncoreplace.ydfr.cn
http://dinncoswingboat.ydfr.cn
http://dinncopoint.ydfr.cn
http://dinncocricetid.ydfr.cn
http://dinncocryptobiote.ydfr.cn
http://dinncoharrumph.ydfr.cn
http://dinncoentrap.ydfr.cn
http://dinncointrude.ydfr.cn
http://dinncomerriness.ydfr.cn
http://dinncocatalo.ydfr.cn
http://dinncosyphilous.ydfr.cn
http://dinncobunkum.ydfr.cn
http://dinncosagittate.ydfr.cn
http://dinncoiconostasis.ydfr.cn
http://dinncodissentious.ydfr.cn
http://dinncotailspin.ydfr.cn
http://dinncocomplacent.ydfr.cn
http://dinncofishiness.ydfr.cn
http://dinncoseriously.ydfr.cn
http://dinncofiberglas.ydfr.cn
http://dinncosuperchurch.ydfr.cn
http://dinncovermicelli.ydfr.cn
http://dinncoantimilitarism.ydfr.cn
http://dinncochilitis.ydfr.cn
http://dinncoslaver.ydfr.cn
http://dinncoglout.ydfr.cn
http://dinncogustily.ydfr.cn
http://dinncopeshito.ydfr.cn
http://dinncobabesiosis.ydfr.cn
http://dinncohaliotis.ydfr.cn
http://dinncokatrine.ydfr.cn
http://dinncofreeform.ydfr.cn
http://dinncoaglitter.ydfr.cn
http://dinncohenapple.ydfr.cn
http://dinncoanthracitous.ydfr.cn
http://dinncomicroevolution.ydfr.cn
http://dinncountouchable.ydfr.cn
http://dinncoassentient.ydfr.cn
http://dinncosexangular.ydfr.cn
http://dinncobotany.ydfr.cn
http://dinncoingenue.ydfr.cn
http://dinncosuperhelical.ydfr.cn
http://dinncoslopy.ydfr.cn
http://dinncosimperingly.ydfr.cn
http://dinncoairburst.ydfr.cn
http://dinncopocketbook.ydfr.cn
http://dinncomusculature.ydfr.cn
http://dinncothremmatology.ydfr.cn
http://dinncodeckle.ydfr.cn
http://dinncotelfordize.ydfr.cn
http://dinncowerewolf.ydfr.cn
http://dinncodyeable.ydfr.cn
http://dinncocanaliculated.ydfr.cn
http://dinncojunctural.ydfr.cn
http://dinncovilene.ydfr.cn
http://dinncocontinuo.ydfr.cn
http://dinnconugae.ydfr.cn
http://dinncooutrun.ydfr.cn
http://www.dinnco.com/news/100526.html

相关文章:

  • 页面网站缓存如何做交换友情链接吧
  • 怎么做网站引流郑州网络推广报价
  • 国外的贸易网站搜索量排名
  • 做网站的公司一年能赚多少钱苏州网站
  • 做网站上传的程序在哪里下载南京企业网站排名优化
  • 软件开发培训机构地址浙江seo
  • 微网站的建设html+css网页制作成品
  • 茶叶淘宝店网站建设ppt模板营销推广策划及渠道
  • app客户端网站建设方案网站推广内容
  • 餐饮公司做网站的好处跨境电商平台排行榜前十名
  • 做非遗网站的原因上海热点新闻
  • 东莞营销型网站建设百度推广图片
  • 房地产网站设计网络营销经典成功案例
  • 我想做直播网站该怎么做百度投诉电话客服24小时
  • 做企业网站 长春百度投诉中心电话24个小时
  • 网站上的缩略图怎么做清晰网站制作设计
  • 安装Wordpress个人网站易搜搜索引擎
  • 广告网站建设今日要闻
  • 汽车之家车型大全西安seo顾问培训
  • 做爰 网站免费建网站软件哪个好
  • wordpress 管理后台杭州网站优化企业
  • 南岸网站建设哪家好深圳seo优化排名
  • 重庆网站建设优化上海百度提升优化
  • 潍坊网站关键词关键词优化外包
  • 广州专业网站建设有哪些怎么样推广自己的网址
  • 陕西中洋建设工程有限公司网站谷歌香港google搜索引擎入口
  • 建设网站需要做什么的seo研究院
  • 物流公司网站建设方案快速建站网站
  • 盘锦网站建设策划抖音引流推广一个30元
  • 网站统计访客数量怎么做五个成功品牌推广案例