為什么就是有問題呢?都是按著老師的步驟來的呀~~
public class StudentsTest { private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//創建配置對象
Configuration config = new Configuration().configure();
//創建服務注冊對象(hibernate4.35之后該方法就不能再獲取到實體信息了)
// ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
//創建會話工廠
// sessionFactory = config.buildSessionFactory(serviceRegistry);
sessionFactory =new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
//會話對象
session = sessionFactory.openSession();
//開啟事務
transaction = session.beginTransaction();
}
@After
public void destory(){
transaction.commit();//提交事務
session.close();//關閉會話
sessionFactory.close();//關閉會話工廠
}
@Test
public void testSaveStudents(){
//生成學生對象
Students s = new Students(1,"張三豐","男",new Date(),"武當山");
session.save(s);//保存對象進入數據庫
}
}
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">LOVEmm1314!</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://114.116.152.128:3306/HYMAN_DEV?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC</property>
<!-- <property name="dialect">org.hibernate.dialect.MYSQLDialect</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping? resource="Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
五月 29, 2019 3:39:35 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
五月 29, 2019 3:39:35 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.11.Final}
五月 29, 2019 3:39:35 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
五月 29, 2019 3:39:35 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
五月 29, 2019 3:39:35 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
五月 29, 2019 3:39:35 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
五月 29, 2019 3:39:35 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Students.hbm.xml
五月 29, 2019 3:39:35 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
五月 29, 2019 3:39:35 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
五月 29, 2019 3:39:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
五月 29, 2019 3:39:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://114.116.152.128:3306/HYMAN_DEV?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC]
五月 29, 2019 3:39:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
五月 29, 2019 3:39:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
五月 29, 2019 3:39:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Wed May 29 15:39:35 GMT+08:00 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
五月 29, 2019 3:39:36 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-09-11
數據庫的驅動配置出現錯誤:
舊版本的驅動配置:
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="
"/>
?mysql8.0的驅動配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true