我使用spring boot和hibernate创建了一个DAO库。 DAO方法的所有单元测试都运行良好。 但是当我在另一个spring引导项目中使用这个jar时,建立了与数据源的JDBC连接,但是语句既没有准备也没有执行。 正确注入DAO bean,会话和事务打开。 我没有其他错误或例外。
Hibernate统计信息记录了这个:
sxhash
有什么想法吗?
编辑:我的一些代码
DAO实施:
2017-07-18 14:57:31.440 INFO 13736 --- [ main] i.StatisticalLoggingSessionEventListener : Session Metrics {
75050 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
52279 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
DAO界面:
@Repository
public class CompagnieDaoImpl implements CompagnieDao {
private final Logger logger = LoggerFactory.getLogger(CompagnieDaoImpl.class);
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@Override
public List<Compagnie> get() {
Session session = null;
Transaction transaction = null;
List<Compagnie> compagnies = null;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
compagnies = session.createCriteria(Compagnie.class).list();
transaction.commit();
} catch (Throwable ex){
logger.error("CompagnieDaoImpl.get exception : " + ex.getMessage());
if (session != null) {
transaction.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
return compagnies;
}
自定义属性:
public interface CompagnieDao {
public List<Compagnie> get();
}
现在,在使用此库的项目中:
应用程序属性:
{"properties": [
{
"name": "clipperton.datasource.password",
"type": "java.lang.String",
"description": "A description for 'clipperton.datasource.password'"
},
{
"name": "clipperton.datasource.url",
"type": "java.lang.String",
"description": "A description for 'clipperton.datasource.url'"
},
{
"name": "clipperton.datasource.driver-class-name",
"type": "java.lang.String",
"description": "A description for 'clipperton.datasource.driver-class-name'"
},
{
"name": "clipperton.datasource.username",
"type": "java.lang.String",
"description": "A description for 'clipperton.datasource.username'"
}
]}
主要课程
#Datasource
spring.jpa.hibernate.ddl-auto=update
clipperton.datasource.url=jdbc:mysql://localhost:3306/clipperton
clipperton.datasource.username=*****
clipperton.datasource.password=*****
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
clipperton.datasource.driver-class-name=com.mysql.jdbc.Driver
#Hibernate
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.type=TRACE
spring.jpa.properties.hibernate.generate_statistics=true
#debug
debug=true
最后,测试类:
@SpringBootApplication(scanBasePackages = {"fr.example.spring.clipperton.clippertondao"})
@Import({DataSourceConfiguration.class, SessionFactoryConfiguration.class})
public class ClippertonStandaloneApplication {
public static void main(String[] args) {
SpringApplication.run(ClippertonStandaloneApplication.class, args);
}
}
数据源
@RunWith(SpringRunner.class)
@SpringBootTest(classes=ClippertonStandaloneApplication.class)
public class ClippertonStandaloneApplicationTests {
@Autowired
CompagnieDao compagnieDao;
@Test
public void contextLoads() {
assertNotNull(compagnieDao);
}
@Test
public void readCompanies() {
List<Compagnie> compagnies = compagnieDao.get();
assertEquals(1, compagnies.size());
}
的SessionFactory
@Configuration
@EnableAutoConfiguration
public class DataSourceConfiguration {
@Bean
@ConfigurationProperties(prefix="clipperton.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
答案 0 :(得分:0)
Spring引导应用程序主类缺少@EntityScan注释。
query : {_id:mongojs.ObjectID(id)}