转载

Spring Boot 入门之数据库篇

由于授权问题,Maven3不提供Oracle JDBC driver, 为了在Maven项目中应用Oracle JDBC driver,必须手动添加到本地仓库。

步骤如下:

(1)下载Oracle驱动程序ojdbc7.jar

(2)获取Oracle版本,SELECT * FROM v$version

Spring Boot 入门之数据库篇

(3)加载到本地maven库中

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=ojdbc7.jar 

(4)安装成功后,在pom.xml文件中添加引用

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>11.2.0.1.0</version>
</dependency>

(5)编写VO类

public class Phones {   
   private String phoneid;

   private String phone;   
   private String name;   
   private String phonelevel;   
   public String getPhoneid() {
      return phoneid;
   }
   public void setPhoneid(String phoneid) {
      this.phoneid = phoneid;
   }
   public String getPhone() {
      return phone;
   }
   public void setPhone(String phone) {
      this.phone = phone;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getPhonelevel() {
      return phonelevel;
   }
   public void setPhonelevel(String phonelevel) {
      this.phonelevel = phonelevel;
   }
}

(6)编写service

public class UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取联系人信息
     * @return
     */
    public List<Phones> getPhoneList() {
        List<Phones> phonelist = new ArrayList<Phones>();
        String sql = "select phoneid,phone,name,phonelevel from PHONELIST";
        phonelist = jdbcTemplate.query(sql, new RowMapper<Phones>() {
            public Phones mapRow(ResultSet rs, int index) throws SQLException {
                Phones msg = new Phones();
                msg.setPhoneid(rs.getString("phoneid"));
                msg.setPhone(rs.getString("phone"));
                msg.setName(rs.getString("name"));
                msg.setPhonelevel(rs.getString("phonelevel"));
                return msg;
            }
        });
        return phonelist;
    }

(7)编写Controller

@RestController
public class HelloController {
    @Autowired
    private UserService userService;
    @RequestMapping("/users")
    public List getUserList(){

        return userService.getPhoneList();
    }
}

(8) 访问地址 http://127.0.0.1:8080/users ,即可看到结果。

原文  https://blog.csdn.net/cwb1128/article/details/81065372
正文到此结束
Loading...