码迷,mamicode.com
首页 > 数据库 > 详细

SpringBoot数据库访问工具(JdbcTemplate、MyBatis、JPA、Hibernate)

时间:2018-09-22 23:22:56      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:host   data-   ora   dal   and   tor   nat   rom   映射   

SpringBoot数据库访问

关系型数据库访问(RDBMS)

采用JdbcTemplate、MyBatis、JPA、Hibernate等技术。

一、JdbcTemplate工具

1.在pom.xml添加boot-starter-jdbc定义<dependencies> 数据库驱动

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>2.0.1.RELEASE</version>
  </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>

</dependencies>

2.在application.properties添加链接参数定义

spring.datasource.username=SCOTT
spring.datasource.password=TIGER
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.driverClassName=oracle.jdbc.OracleDriver

3.根据DEPT表编写Dept实体类

public class Dept implements Serializable{
    private Integer deptno;
    private String dname;
    private String loc;
    //set和get省略
}

4.定义DeptDao接口

public interface DeptDao {
    public List<Dept> findAll();
}

5.定义JdbcDeptDao实现类

@Repository("deptDao")
public class JdbcDeptDao implements DeptDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Dept> findAll() {
        String sql = "select * from DEPT";
        //BeanPropertyRowMapper功能同名加载。查询结果集列名和实体类属性名一致
        RowMapper<Dept> rowMapper = new BeanPropertyRowMapper<Dept>(Dept.class);
        List<Dept> list = jdbcTemplate.query(sql, rowMapper);
        return list;
    }

}

6.定义主启动类,开启自动配置和扫描等功能

@SpringBootApplication
public class MyBootApplication {

}

7.测试程序

public static void main(String[] args) {
    ApplicationContext ac = 
        SpringApplication.run(MyBootApplication.class);
    DeptDao deptDao = ac.getBean("deptDao",DeptDao.class);
    List<Dept> list = deptDao.findAll();
    for(Dept dept:list){
        System.out.println(dept.getDeptno()
            +" "+dept.getDname()+" "+dept.getLoc());
    }
}

四、JPA工具

1.在pom.xml追加boot-jdbc、boot-data-jpa包定义 、数据库驱动

<parent>
   <!-- 设置boot版本控制 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <properties>
  <!-- 设置字符集utf-8 jre版本1.8--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency>
    <!-- jpa基于jdbc --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency>
    <!-- 数据库驱动 --> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> </dependency> <dependency>
    <!-- jpa --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies>

2.在application.properties文件追加数据库连接参数定义

spring.datasource.username=SCOTT
spring.datasource.password=TIGER
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.driverClassName=oracle.jdbc.OracleDriver

3.根据DEPT表编写Dept实体类,并追加映射注解标记(可用jpa逆向生成工程)

@Entity
@Table(name="DEPT")
public class Dept implements Serializable{

    @Id
    @Column(name="DEPTNO")
    private Integer deptno;

    @Column(name="DNAME")
    private String dname;

    @Column(name="LOC")
    private String loc;

    //省略set和get方法       

}

4.定义DeptDao接口,可以继承JpaRepository

public interface DeptDao extends JpaRepository<Dept, Integer>{  //Dept 为eitity类型 Integer为主键类型

}

Repository:顶级接口,无任何操作

CrudRepository:继承了Repository,增加了增删改查操作方法

PagingAndSortingRepository:继承CrudRepository,增加了分页和排序操作

JpaRepository:继承了PagingAndSortingRepository,增加批处理操作

5.测试程序

public static void main(String[] args) {
    ApplicationContext ac = 
        SpringApplication.run(MyBootApplication.class, args);
    DeptDao deptDao = ac.getBean("deptDao",DeptDao.class);
    List<Dept> list = deptDao.findAll();
    for(Dept dept:list){
        System.out.println(dept.getDeptno()+" "+dept.getDname());
    }
}
 
 
 


 
 
 
 
 

SpringBoot数据库访问工具(JdbcTemplate、MyBatis、JPA、Hibernate)

标签:host   data-   ora   dal   and   tor   nat   rom   映射   

原文地址:https://www.cnblogs.com/xiaoka000/p/9691436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!