3、spring-boot访问数据库
2017-11-26 20:59
351 查看
1、在之前的基础上添加两个包
**2、创建source folder源文件夹,在其中创建application.properties文件,
application.properties中代码如下所示**
**3、新建一个实体类,并在期中加入注解@Entity和@Id和@GeneratedValue
代码如下所示:**
PS:在这里你要注意,在我上面第二步给出的代码
中,连接的数据库test要提前建好,因为hibernate只会建表,不会建库。
4、运行结果:test数据库中生成了一个Demo表
(记得刷新一下数据库才能看见噢,我就吃了这个亏)
<dependency><!-- 数据库驱动包 --> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency><!-- 据说可以使对象持久化,待我了解后再来更新 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
**2、创建source folder源文件夹,在其中创建application.properties文件,
application.properties中代码如下所示**
######################################################## ###datasource ######################################################## spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root#这里写密码 spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10 ######################################################## ### Java Persistence Api ######################################################## # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
**3、新建一个实体类,并在期中加入注解@Entity和@Id和@GeneratedValue
代码如下所示:**
@Entity public class Demo { @Id @GeneratedValue private int id; private String name; public long getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
PS:在这里你要注意,在我上面第二步给出的代码
spring.datasource.url = jdbc:mysql://localhost:3306/test
中,连接的数据库test要提前建好,因为hibernate只会建表,不会建库。
4、运行结果:test数据库中生成了一个Demo表
(记得刷新一下数据库才能看见噢,我就吃了这个亏)
相关文章推荐
- Spring Boot学习进阶笔记(二)-JdbcTemplate访问数据库
- 通过Spring Boot配置动态数据源访问多个数据库的实现代码
- SpringBoot开发详解(十) -- 使用JPA访问数据库下篇及使用Page进行数据分页
- Spring Boot 配置JdbcTemplate访问数据库
- SpringCloud SpringBoot mybatis 分布式微服务(八)Spring Boot中使用JdbcTemplate访问数据库
- Spring Boot 学习笔记4——结合Mybatis访问数据库及事务控制
- SpringBoot之拦截器对数据库的访问
- SpringBoot第二讲利用Spring Data JPA实现数据库的访问(一)
- (3)Spring-boot学习 做一个简单的带访问数据库的web项目
- Spring Boot学习笔记之数据库访问
- Spring Boot中使用JdbcTemplate访问数据库
- SpringBoot第二讲 利用Spring Data JPA实现数据库的访问(一)
- Spring Boot---(13)Spring Boot 使用JPA访问数据库
- SpringBoot进阶之访问数据库(含源码)
- Spring Boot-使用Spring Data JPA简化数据库访问
- Spring Boot初探之数据库访问
- SpringBoot-创建RESTful风格的 http接口访问jpa 来操作数据库
- Spring Boot中使用JdbcTemplate访问数据库
- Springboot 添加JdbcTemplates 访问mysql 数据库
- Spring Boot集成MyBatis访问数据库