no10-数据访问与事务第一次课-springboot项目整合jdbc-jpa框架

飞一样的编程
飞一样的编程
擅长邻域:Java,MySQL,Linux,nginx,springboot,mongodb,微信小程序,vue

分类: Java springboot 专栏: springboot学习 标签: springboot项目整合jpa框架和jdbc

2023-03-27 15:28:57 823浏览

springboot项目整合jpa框架和jdbc

JdbcTemplate

需要引入得依赖

  <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
@Repository
public class StudentDaoImpl implements StudentDao {
    @Resource
    private  JdbcTemplate jdbcTemplate;

    @Override
    public List<Student> listStudent() {
        List<Student> students = jdbcTemplate.query("select * from student", new BeanPropertyRowMapper<>(Student.class));
        return students;
    }

    @Override
    public int addStudent(Student student) {
        String sql ="insert into student (username,password,sex,address) values (?,?,?,?)";

        return jdbcTemplate.update(sql,student.getUsername(),student.getPassword(),student.getSex(),student.getAddress());
    }

    @Override
    public int deleteStudent(Integer id) {
        String sql = "delete from student where id = ?";
        int row = jdbcTemplate.update(sql, id);
        return row;
    }

    @Override
    public int updateStudent(Student student) {
        String sql = "update student set username = ?,password = ?, address = ? where id = ?";
        int row = jdbcTemplate.update(sql, student.getUsername(), student.getPassword(), student.getAddress(), student.getId());
        return row;
    }

    @Override
    public Student getStudentById(Integer id) {
        Student student = jdbcTemplate.queryForObject("select * from student where id = "+id,new BeanPropertyRowMapper<>(Student.class));
        return student;
    }
}

JPA

JPA (Java Persistence API)是由Sun官方提出的Java持久化规范,并不是一-套产品。它为java开发人员提供一种对象/关联映射工具来管理java应用中的关系数据。他的出现主要是为了简化现有的持久化开发工作和整合0RM技术,结束现在Hibernate、TopLink、JD0等0RM框架各自为营的局面。

JPA在充分吸收现有的Hibernate、TopLink、 JD0等0RM框架的基础上发展而来的,具有易于使用,伸缩性强等优点。Spring data JPA是Spring基于PRM框架、JPA规范基础上封装的一套JPA应用框架,底层使用了Hibernate的JPA技术实现,可使开发者用极简的代码实现对数据库的访问和操作。

  • 引入依赖
   <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  • 配置
spring.datasource.url=jdbc:mysql:///springdata-test?characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
#JPA相关配置
#开启根据实体类结构自动创建表
spring.jpa.properties.hibernate.hbm2ddl.auto=update
#指定sqL语句的使用版本
spring.jpa.properties.hibernate.hbm2ddl.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
#是否显示sql语句在控制台
spring.jpa.show-sql=true
  • 实体类
@Data
@Table(name = "tb_book")
@Entity
@Accessors(chain = true)
public class Book {
    @Id//主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)//自增生成id
    @Column(nullable =false )//不为空
    private Integer id;

    @Column
    private String bookName;
    @Column
    private String author;
}
  • dao层
public interface BookDao extends JpaRepository<Book,Integer> {

}
  • 测试类
@SpringBootTest
class Ch6JpaApplicationTests {
    @Autowired
    private BookDao bookDao;



    @Test
    void list() {

        //加一个排序 -根据ID降序排
        List<Book> bookList = bookDao.findAll(Sort.by("id").descending());
        bookList.forEach(b -> System.out.println(b));
    }
    @Test
    void findById() {
        Optional<Book> byId = bookDao.findById(1);
        Book book = byId.get();
        System.out.println(book);
    }
    @Test
    void save() {
        Book book = bookDao.save(new Book().setBookName("西游记").setAuthor("吴承恩"));
        System.out.println(book);
    }
    @Test
    void delete() {
        bookDao.delete(new Book().setId(3));

    }
    //修改
    @Test
    void update() {
        Book book = bookDao.save(new Book().setId(1).setBookName("红楼梦").setAuthor("曹雪芹"));
        System.out.println(book);

    }
}

jpa方法命名规则:

public interface BookDao extends JpaRepository<Book,Integer> {

      List<Book> findByIdBetween (int start,int end);

      List<Book> findByBookNameLike (String name);
}
    @Test
    void between(){
        List<Book> books = bookDao.findByIdBetween(1,3);
        books.forEach(b -> System.out.println(b));
    }
    @Test
    void like(){
        List<Book> books = bookDao.findByBookNameLike("%西%");
        books.forEach(b -> System.out.println(b));
    }

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695