SpringBoot自学笔记(三)
特此声明:本自学笔记,主要是参照《从零开始学Spring Boot》(作者: 林祥纤)一书,并对部分例子进行了改动,便于理解,非盈利为目的,仅供学习交流,如有侵权,立即撤下!
历史笔记链接:《SpringBoot自学笔记(一)》
历史笔记链接:《SpringBoot自学笔记(二)》
(六)Srping Boot——整合Mysql及Hibernate
1.新建Maven工程,博主以“spring-boot-database”命名为例。
2.修改pom.xml配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>yan.li</groupId> <artifactId>spring-boot-database</artifactId> <version>0.0.1-SNAPSHOT</version>
<!-- 引入spring-boot-start-parent 依赖管理,引入以后在申明其它dependency的时候就不需要version了 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <!-- 引入spring-boot-starter-web 包含了spring webmvc和tomcat等web开发的特性 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 引入fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.7</version> </dependency> <!-- 引入mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 引入JPA规范 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> <!-- 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven 的spring-boot:run的话是不需要此配置的 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> </plugin> <dependencies> <!--springloaded hot deploy 热部署--> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.4.RELEASE</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugins> </build> </project> |
3.在src/main/resources目录下创建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 |
4.在yan.li包下,创建启动类MainApp.java(其后例子中将不在赘述):
package yan.li; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //申明让spring boot自动给程序进行必要的配置 public class MainApp { public static void main(String[] args) { SpringApplication.run(MainApp.class, args); } } |
5.在yan.li.pojo包下,创建Student.java:
package yan.li.pojo; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * 实体类 * @author Liyan * @date 2016年11月24日 下午5:58:58 */ @Entity//加入这个注解,Demo就会进行持久化了,在这里没有对@Table进行配置,请自行配置。 public class Student { @Id @GeneratedValue private Long id;//主键 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; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }
|
5.在yan.li.dao包下,创建DemoRepositor.java:
package yan.li.dao; import org.springframework.data.repository.CrudRepository; import yan.li.pojo.Student; /** * 这是一个接口,没有具体的实现,这就是JPA * @author Liyan * @date 2016年11月24日 下午5:58:36 */ public interface DemoRepositor extends CrudRepository<Student,Long>{ /** * Spring Data的核心接口里面Repository是最基本的接口了, spring提供了很多其 * 实现接口,如:CrudRepository,PagingAndSortingRepository, * SimpleJpaRepository,QueryDslJpaRepository等大量查询接口 **/ } |
5.在yan.li.service包下,创建DemoService.java:
package yan.li.service; import javax.annotation.Resource; import javax.transaction.Transactional; import org.springframework.stereotype.Service; import yan.li.dao.DemoRepositor; import yan.li.pojo.Student; /** * service层 * @author Liyan * @date 2016年11月24日 下午5:59:25 */ @Service public class DemoService { @Resource private DemoRepositor repositor; @Transactional public void save(Student student) { repositor.save(student); } }
|
5.在yan.li.controller包下,创建DemoController.java:
package yan.li.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import yan.li.pojo.Student; import yan.li.service.DemoService;
@RestController @RequestMapping("/demo") public class DemoController { @Resource private DemoService service; @RequestMapping("/save") public String save() { Student student = new Student(); student.setName("Tom"); //注意别用中文,会乱码。后面会解决! service.save(student); return "成功保存:"+student.toString(); } }
|
3.MainApp 启动类上右键Run As → Java Application启动(或是使用Maven的spring-boot:run),浏览器访问http://localhost:8080/demo/save,即可看到如下信息:
成功保存:Student [id=2, name=Tom] |
打开SQLyog会发现JPA已经自动帮我们建成了表结构,同时字段已经添加成功。