1.先使用idea创建maven项目(这个就不详细讲了,很简单的操作)
2.创建完maven项目之后添加springboot依赖,pom.xml文件如下:
4.0.0
cn.tongdun.gwl
SpringBootTest
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
junit
junit
3.8.1
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-starter-thymeleaf
hibernateSpringDemo
org.springframework.boot
spring-boot-maven-plugin
true
pom文件编写完毕,配置maven路径,并导入依赖.
3.接下来写一个SpringBoot入门程序
(1)新建类MyTest.java
1 packagecn.huawei.gwl;2
3 importorg.springframework.boot.SpringApplication;4 importorg.springframework.boot.autoconfigure.SpringBootApplication;5 importorg.springframework.context.annotation.ComponentScan;6
7 @SpringBootApplication8 @ComponentScan(basePackages = "cn")9 public classMyTest {10
11 public static voidmain(String[] args) {12 SpringApplication.run(MyTest.class, args);13 }14 }
需要注意的是:这个类的上面需要添加SpringBoot的注解@SpringBootApplication,然后在主函数中开始启动.
(2)然后创建一个HelloContreller类,用来测试SpringBoot
1 packagecn.huawei.gwl;2
3 importorg.springframework.web.bind.annotation.PathVariable;4 importorg.springframework.web.bind.annotation.RequestMapping;5 importorg.springframework.web.bind.annotation.RestController;6
7
8 @RestController9 public classHelloController {10
11 @RequestMapping("/say")12 String home() {13 System.out.println("get into");14 return "hello world";15 }16
17 @RequestMapping("/hello/{name}")18 String hello(@PathVariable String name) {19 return "hello" +name;20 }21 }
需要注意的是这上面的几个注解:
@RestController:表示这是个控制器,和Controller类似
@EnableAutoConfiguration:springboot没有xml配置文件因为这个注解帮助我们干了这些事情,有了这个注解springboot启动的时候回自动猜测你的配置文件从而部署你的web服务器
@RequestMapping(“/say”):这个和SpringMvc中的类似了。
@PathVariable:参数
SpringBoot程序验证完毕.
5.接下来再pom.xml文件里面添加要使用的jpa依赖:
1
2 org.springframework.boot
3 spring-boot-starter-data-jpa
4
5
6 mysql
7 mysql-connector-java
8
并导入依赖
6.添加依赖之后需要配置一些连接MySQL所需要的配置,创建一个application.properties:
1 spring.datasource.url = jdbc:mysql://localhost:3306/test
2 spring.datasource.username =root3 spring.datasource.password =abcd12344 spring.datasource.driverClassName =com.mysql.jdbc.Driver5 # Specify the DBMS6 spring.jpa.database =MYSQL7 # Show or not log foreach sql query8 spring.jpa.show-sql = true
9 # Hibernate ddl auto (create, create-drop, update)10 spring.jpa.hibernate.ddl-auto =update11 # Naming strategy12 spring.jpa.hibernate.naming-strategy =org.hibernate.cfg.ImprovedNamingStrategy13 # stripped before adding them to the entity manager14 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
7.为main方法添加注解@EnableJpaRepositories
1 packagecn.tongdun.gwl;2
3 importorg.springframework.boot.SpringApplication;4 importorg.springframework.boot.autoconfigure.SpringBootApplication;5 importorg.springframework.context.annotation.ComponentScan;6 importorg.springframework.data.jpa.repository.config.EnableJpaRepositories;7
8
9 @SpringBootApplication10 @EnableJpaRepositories11 @ComponentScan(basePackages = "cn")12 public classMyTest {13
14 public static voidmain(String[] args) {15 SpringApplication.run(MyTest.class, args);16 }17 }
8.接下来创建dao层,不过在这里是repository包,例如创建一个User实体,然后再创建一个UserRepository:
1 packagecn.tongdun.gwl;2
3 importorg.springframework.data.jpa.repository.Query;4 importorg.springframework.data.repository.CrudRepository;5 importorg.springframework.data.repository.query.Param;6
7 public interface UserRepository extends CrudRepository{8
9 publicUser findOne(Long id);10
11 publicUser save(User user);
12 }
这里面是创建一个UserRepository接口,并不需要创建UserRepository实现,springboot默认会帮你实现,继承自CrudRepository,@Param代表的是sql语句中的占位符,例如这里的@Param(“name”)代表的是:name占位符。
9.下面再控制层使用UserRepository,创建一个HibernateController:
1 packagecn.tongdun.gwl;2
3 importorg.springframework.beans.factory.annotation.Autowired;4 importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;5 importorg.springframework.stereotype.Controller;6 importorg.springframework.web.bind.annotation.RequestMapping;7 importorg.springframework.web.bind.annotation.ResponseBody;8
9 importjava.util.Date;10
11 @Controller12 @RequestMapping("/hibernate")13 @EnableAutoConfiguration14 public classHibernateController {15
16 @Autowired17 privateUserRepository userRepository;18
19 @RequestMapping("getUserById")20 @ResponseBody21 publicUser getUserById(Long id) {22 User u =userRepository.findOne(id);23 System.out.println("userRepository: " +userRepository);24 System.out.println("id: " +id);25 returnu;26 }27
28 @RequestMapping("saveUser")29 @ResponseBody30 public voidsaveUser() {31 User u = newUser();32 u.setUserName("wan");33 u.setAddress("浙江省杭州市滨江区");34 u.setBirthDay(newDate());35 u.setSex("男");36 userRepository.save(u);37 }38 }
@Autowired代表按照类型注入,@Resource按照名称注入
至此,代码已经编写完毕,下面进入测试