springboot中Hibernate的Jpa使用NativeQuery原始sql,再用fastjson转pojo

springboot的数据库配置,application-dev.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.1.222:3306/ppppp?useOldAliasMetadataBehavior=true&useSSL=false
    username: 66666
    password: 123456
  jpa:
    database: mysql
    properties:
      hibernate:
        default_schema: ppppp
        show_sql: true
        format_sql: false

# log bind parameter
logging:
  level:
    org:
      hibernate:
        type:
          descriptor:
            sql: trace

原始sql语句普通查询,TestD是pojo类dto,model等等

import com.alibaba.fastjson.JSONObject;
import com.chatserver.dto.TestD;
import com.chatserver.entity.TestE;
import org.hibernate.transform.AliasToEntityMapResultTransformer;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


        String sql = "select name,pwd,time_create from t_test where id=4";

        Query dbQuery = entityManager.createNativeQuery(sql);
        //cast to hibernate query
        org.hibernate.Query hibernateQuery =((org.hibernate.jpa.HibernateQuery)dbQuery)
                .getHibernateQuery();
        hibernateQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);

        List<Map<String,Object>> res = hibernateQuery.list();

        List<TestD> testDS = new ArrayList<>();
        res.forEach(e->{
            TestD one = JSONObject.parseObject(JSONObject.toJSONString(e), TestD.class);
            testDS.add(one);
        });

        System.out.println(res);

使用参数的预处理查询,Named parameters

		String sql = "select name,pwd,time_create from t_test where name=:nAme";
		Query dbQuery = entityManager.createNativeQuery(sql);

        //cast to hibernate query
        org.hibernate.Query hibernateQuery =((org.hibernate.jpa.HibernateQuery)dbQuery)
                .getHibernateQuery();
        hibernateQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);

        hibernateQuery.setParameter("nAme","123");

        List<Map<String,Object>> res = hibernateQuery.list();

或者是简单参数绑定

		String sql = "select name,pwd,time_create from t_test where name=? and pwd=?";
		Query dbQuery = entityManager.createNativeQuery(sql);

        //cast to hibernate query
        org.hibernate.Query hibernateQuery =((org.hibernate.jpa.HibernateQuery)dbQuery)
                .getHibernateQuery();
        hibernateQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);

		hibernateQuery.setParameter(0,"123");
		hibernateQuery.setParameter(1,"pwd");

		List<Map<String,Object>> res = hibernateQuery.list();

测试批量保存

spring:
  jpa:
    properties:
      hibernate:
        generate_statistics: true
        jdbc:
          batch_size: 20
    @Autowired
    private EntityManager entityManager;

    @Rollback(false)
    @Transactional
    @Test
    public void test5(){

        for (int i = 0; i < 10000; i++) {
            if ( i != 0 && i % 20 == 0 ) {
                testR.flush();
            }

            TestE one = new TestE().setName("123").setPwd("pwd");
            testR.save(one);
        }

        for (int i = 0; i < 10000; i++) {
            if ( i != 0 && i % 20 == 0 ) {
                entityManager.flush();
                entityManager.clear();
            }
            TestE one = new TestE().setName("123").setPwd("pwd");
            entityManager.persist(one);
        }
    }

你可能感兴趣的:(spring)