注意:本章不需要重新创建项目,依赖前面章节module-oauth-api、module-oauth-resources项目即可。
请预先搭建oracle数据库,这里就不讲解oracle搭建了
步骤一:在module-oauth-resources项目下pom.xm引入以下配置:
org.springframework.boot
spring-boot-starter-data-jpa
com.oracle
ojdbc6
11.2.3.0
注意:
引入oracle jar的时候,可能会报错,因为没有ojdbc6这个jar包,需要先到官网下载ojdbc6.jar
下载之后,把ojdbc6.jar放到一个固定目录下,我的目录是E:\dev\jar\oracle\ojdbc6.jar,然后执行以下命令:
mvn install:install-file -Dfile=E:\dev\jar\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.3.0 -Dpackaging=jar -DgeneratePom=true
--属性说明
-Dfile : jar地址
cmd窗口中显示build success即表示安装成功,刷新一下项目就不报错了。
ojdbc6.jar和源码放置在一起,可以一起到码云上下载,最底下有下载链接信息。
步骤二:在module-oauth-resources项目下application.properties中新增以下配置:
#oracle
spring.datasource.driver-class-name = oracle.jdbc.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username = xm
spring.datasource.password = xm
#jpa
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.show-sql = true
步骤三:以下几个文件都是在module-oauth-resources项目中新增
新创建一个UserEntity类,内容如下:
package com.maven.xm.oauth.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* 用户实体类
* @Table中的name属性,表示oracle数据库的表名
* @Column中的name属性,表示oracle数据库表中的字段名
* @author ouyangjun
*
*/
@Entity
@Table(name = "xm_user")
public class UserEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "userid")
private String userid;
@Column(name = "username")
private String username;
@Column(name = "passwrod")
private String passwrod;
@Column(name = "phone")
private String phone;
@Column(name = "sex")
private String sex;
@Column(name = "age")
private Integer age;
@Column(name = "weight")
private Integer weight;
@Column(name = "height")
private Integer height;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswrod() {
return passwrod;
}
public void setPasswrod(String passwrod) {
this.passwrod = passwrod;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
}
新创建一个UserRepository接口,该接口和数据库交互,内容如下:
package com.maven.xm.oauth.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.maven.xm.oauth.entity.UserEntity;
/**
* 用户数据交互接口
* JpaRepository<用户实体类,用户实体类中的主键类型>
* 如果是自定义接口,一定要在接口上添加@Repository注解
* @author ouyangjun
*
*/
public interface UserRepository extends JpaRepository{
/**
* 根据用户ID查询一条数据,jpa有一定的查询规则,以一些通用前缀开头,比如findBy、find、get等
* 如果不想用这种默认规则,需要在接口上添加@Query主键,自定义实现数据查询,如下面一个接口
* @param userid
* @return
*/
public UserEntity findByUserid(String userid);
/**
* jpa支持对象查询,简称HQL,也支持原生sql查询
* @return
*/
@Query("select u from UserEntity u")
public List listUserEntity();
}
新建一个UserController类,暴露给api的接口,内容如下(可能会报没有UserDTO,先不要急,接着往下走):
package com.maven.xm.oauth.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.maven.xm.oauth.dto.UserDTO;
import com.maven.xm.oauth.entity.UserEntity;
import com.maven.xm.oauth.repository.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(tags = "module-oauth模块,用户User Controller")
@RestController
@RequestMapping(value = "/oauth/user")
public class UserController {
@Autowired
private UserRepository userRepository;
@ApiOperation(value="module模块,获取所有用户信息", notes="Test")
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List listUser(){
List dtoList = new ArrayList();
List list = userRepository.listUserEntity();
// 除了这种迭代之外,还可以用jdk1.8自带的lambda表达式迭代
UserDTO dto = null;
for(UserEntity entity : list){
dto = new UserDTO();
BeanUtils.copyProperties(entity, dto);
dtoList.add(dto);
}
return dtoList;
}
}
步骤四:以下几个文件都是在module-oauth-api项目中新增
新创建一个UserDTO数据转换类,作用于DAO层数据转换使用,内容如下:
package com.maven.xm.oauth.dto;
import java.io.Serializable;
/**
* 用户DTO,由于oauth模块只暴露调用接口,需要创建一个和用户实体类属性一致的DTO,作用于DAO层数据转换,暴露给其它模块调用
* @author Administrator
*
*/
public class UserDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String userid;
private String username;
private String passwrod;
private String phone;
private String sex;
private Integer age;
private Integer weight;
private Integer height;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswrod() {
return passwrod;
}
public void setPasswrod(String passwrod) {
this.passwrod = passwrod;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
}
新创建一个IUserApi接口,该接口注意是暴露给其它模块调用,内容如下:
package com.maven.xm.oauth.api;
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.maven.xm.oauth.api.fallback.UserApiFallback;
import com.maven.xm.oauth.dto.UserDTO;
@FeignClient(
value = "module-oauth-server",
fallback = UserApiFallback.class
)
public interface IUserApi {
@RequestMapping(value = "/oauth/user/list", method = RequestMethod.GET)
public List listUser();
}
新创建一个UserApiFallback回调函数实现类,继承IUserApi接口,内容如下:
package com.maven.xm.oauth.api.fallback;
import java.util.List;
import org.springframework.stereotype.Component;
import com.maven.xm.oauth.api.IUserApi;
import com.maven.xm.oauth.dto.UserDTO;
@Component
public class UserApiFallback implements IUserApi {
@Override
public List listUser() {
return null;
}
}
步骤五:文件都已经创建成功,现在开始启动
第一步:先启动EurekaServerApplication注册中心
第二步:启动OAuthApplication模块
第三步:在浏览器地址中输入:http://localhost:8763/oauth/user/list,界面如下,数据为json格式
本章未实现在xm-web-controller模块调用IUserApi接口,留给大家尝试一下。
如果只有mysql数据库,需要在pom.xml引入jar,然后替换application.properties中oracle配置即可。
本章功能已实现完,待续!
源码下载地址: https://gitee.com/ouyangjun_xm/springcloud/attach_files下chapter-six.rar压缩包
码云账户: [email protected] 密码: [email protected]
请勿恶意操作,谢谢!
本文说明:该文章属于原创,如需转载,请标明文章转载来源