springdata? 模型生成sql
1 application.properties 配置账号密码数据库模式等?
spring.application.name=kalunTest
server.port=8088
spring.datasource.url=jdbc:mysql://192.168.241.129:3306/wulian_db1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#这个参数是在建表的时候,将默认的存储引擎切换为 InnoDB 用的
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
#配置在日志中打印出执行的 SQL 语句信息。
spring.jpa.show-sql=true
#配置自动建表:updata:没有表新建,有表更新操作,控制台显示建表语句 create:要删除并且创建实体类对应的表
spring.jpa.hibernate.ddl-auto=update
2?pom.xml? 中需要有这些配置
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
...
3?mode?最简单的表
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class OrderDB {
public OrderDB() {
}
@Id
public String id;
}
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface OrderRepository extends CrudRepository<OrderDB,Integer> {
}
4?测试
@Autowired
OrderRepository orderRepository;
@PostMapping("/creatOrder")
public void creatOrder(OrderDB order) {
orderRepository.save(order);
}
|