springboot jpa使用

1、添加pom依赖:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

2、application添加配置:

spring.jpa:
  show-sql: true
  naming.implicit-strategy: org.hibernate.cfg.ImprovedNamingStrategy
  properties:
#    hibernate.current_session_context_class: org.springframework.orm.hibernate4.SpringSessionContext
    hibernate.dialect: org.hibernate.dialect.MySQL5Dialect

3、DAO

@Repository
public class BaseDAOImpl<T,ID extends Serializable> implements BaseDAO<T,ID> {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Object findBySql(String tablename, String filed, Object o) {
        String sql="select * from slot where slot_id = " + o;
        System.out.println(sql+"--------sql语句-------------");
        Query query = entityManager.createNativeQuery(sql);
        entityManager.close();
        return query.getSingleResult();
    }

  ...其他查询方法待续 }

 

posted @ 2018-10-29 20:26  花拾夕  阅读(534)  评论(0)    收藏  举报