<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--导入hibernate--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
主要就是导入hibernate的两个依赖,还有mysql的驱动。
补充,jpa依赖也要加上(因为idea没有及时刷新,视频说错了,还是要的):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
springboot的yml配置文件
#开发环境 server: port: 80 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/myboot?autoReconnect=true username: root password: 123 hikari: #池中最大链接数 maximum-pool-size: 50 #池中链接最长生命周期 max-lifetime: 120000 #连接允许在池中闲置的最长时间 idle-timeout: 600000 #等待来自池的连接的最大毫秒数 connection-timeout: 30000 #池中最小空闲链接数 minimum-idle: 10
注册hibernate的依赖
package com.example.myboot.config; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.util.Properties; @Configuration @EnableJpaRepositories @EnableTransactionManagement public class HibernateConfig { @Value("${spring.datasource.url}") private String jdbcUrl; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Autowired private SessionFactory sessionFactory; /** 数据源采用springboot推荐的hikari* */ @Bean public DataSource dataSource(){ return DataSourceBuilder.create() .url(jdbcUrl) .username(username) .password(password) .driverClassName(driverClassName) .build(); } @Bean public LocalSessionFactoryBean localSessionFactoryBean(){ Properties properties=new Properties(); properties.setProperty("hibernate.show_sql", "true"); properties.setProperty("hibernate.hbm2ddl.auto", "update"); /** * 如果是高版本的mysql,请使用MySQLDialect */ properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); LocalSessionFactoryBean lsb=new LocalSessionFactoryBean(); lsb.setDataSource(dataSource()); /** * 设置实体类的位置 */ lsb.setPackagesToScan("com.example.myboot.pojo"); lsb.setHibernateProperties(properties); return lsb; } @Bean public HibernateTemplate hibernateTemplate(){ HibernateTemplate hibernateTemplate = new HibernateTemplate(); hibernateTemplate.setSessionFactory(sessionFactory); return hibernateTemplate; } }
package com.example.myboot.pojo; import javax.persistence.*; @Entity @Table(name = "t_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "s_id") private Long id; @Column(name = "s_name") private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
这样在程序启动的时候,就会自动生成 t_user 表。
搞一个UserController,里面写新增和查询的方法:
package com.example.myboot.controller; import com.example.myboot.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("user") public class UserController { @Autowired HibernateTemplate hibernateTemplate; @RequestMapping("add") @Transactional(rollbackFor = Exception.class) public String add(User user){ hibernateTemplate.saveOrUpdate(user); return "success"; } @RequestMapping("findAll") public List<User> findAll(User user){ List<User> users = hibernateTemplate.findByExample(user); return users; } }