1.先使用idea创建maven项目(这个就不详细讲了,很简单的操作)

2.创建完maven项目之后添加springboot依赖,pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.</modelVersion> <groupId>cn.tongdun.gwl</groupId>
<artifactId>SpringBootTest</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5..RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency>
<!--springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies> <build>
<finalName>hibernateSpringDemo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build> </project>

pom文件编写完毕,配置maven路径,并导入依赖.

3.接下来写一个SpringBoot入门程序

(1)新建类MyTest.java

 package cn.huawei.gwl;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@ComponentScan(basePackages = "cn")
public class MyTest { public static void main(String[] args) {
SpringApplication.run(MyTest.class, args);
}
}

需要注意的是:这个类的上面需要添加SpringBoot的注解@SpringBootApplication,然后在主函数中开始启动.

(2)然后创建一个HelloContreller类,用来测试SpringBoot

 package cn.huawei.gwl;

 import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @RequestMapping("/say")
String home() {
System.out.println("get into");
return "hello world";
} @RequestMapping("/hello/{name}")
String hello(@PathVariable String name) {
return "hello" + name;
}
}

需要注意的是这上面的几个注解:

  • @RestController:表示这是个控制器,和Controller类似
  • @EnableAutoConfiguration:springboot没有xml配置文件因为这个注解帮助我们干了这些事情,有了这个注解springboot启动的时候回自动猜测你的配置文件从而部署你的web服务器
  • @RequestMapping(“/say”):这个和SpringMvc中的类似了。
  • @PathVariable:参数

4.在浏览器中输入:http://localhost:8080/say 和 http://localhost:8080/hello/haha 浏览器上即可得到输出结果.

SpringBoot程序验证完毕.

5.接下来再pom.xml文件里面添加要使用的jpa依赖:

         <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

并导入依赖

6.添加依赖之后需要配置一些连接MySQL所需要的配置,创建一个application.properties:

 spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = abcd1234
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# 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

7.为main方法添加注解@EnableJpaRepositories

 package cn.tongdun.gwl;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication
@EnableJpaRepositories
@ComponentScan(basePackages = "cn")
public class MyTest { public static void main(String[] args) {
SpringApplication.run(MyTest.class, args);
}
}

8.接下来创建dao层,不过在这里是repository包,例如创建一个User实体,然后再创建一个UserRepository:

 import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param; public interface UserRepository extends CrudRepository<User, Long> { public User findOne(Long id); public User save(User user); @Query("select t from User t where t.userName=:name")
public User findUserByName(@Param("name") String name); }

这里面是创建一个UserRepository接口,并不需要创建UserRepository实现,springboot默认会帮你实现,继承自CrudRepository,@Param代表的是sql语句中的占位符,例如这里的@Param(“name”)代表的是:name占位符。

9.下面再控制层使用UserRepository,创建一个HibernateController:

 package cn.tongdun.gwl;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.Date; @Controller
@RequestMapping("/hibernate")
@EnableAutoConfiguration
public class HibernateController { @Autowired
private UserRepository userRepository; @RequestMapping("getUserById")
@ResponseBody
public User getUserById(Long id) {
User u = userRepository.findOne(id);
System.out.println("userRepository: " + userRepository);
System.out.println("id: " + id);
return u;
} @RequestMapping("saveUser")
@ResponseBody
public void saveUser() {
User u = new User();
u.setUserName("wan");
u.setAddress("浙江省杭州市滨江区");
u.setBirthDay(new Date());
u.setSex("男");
userRepository.save(u);
}
}

@Autowired代表按照类型注入,@Resource按照名称注入

至此,代码已经编写完毕,下面进入测试

10.访问http://localhost:8080/hibernate/saveUser 之后,jpa自动生成sql语句:

访问http://localhost:8080/hibernate/getUserById?id=2

最新文章

  1. android Sqlite小记
  2. 创建第一个 local network(I) - 每天5分钟玩转 OpenStack(80)
  3. Code First :使用Entity. Framework编程(6) ----转发 收藏
  4. WPF-流文档元素
  5. HTML的音频和视频
  6. 简易qq对话框
  7. oracle直通车6关于rman备份恢复数据文件,以及创建分区表的实验
  8. POSTGRESQL9.5之pg_rman工具
  9. Unity3D入门(一):环境搭建
  10. 酷我音乐API
  11. 两个异步处理AsyncTask和Handler的优缺点
  12. Linux升级Python提示Tkinter模块找不到解决
  13. Android 大约Dialog弹出窗口
  14. Erlang的Unicode支持
  15. 消息中间件kafka+zookeeper集群部署、测试与应用
  16. P2837 晚餐队列安排
  17. 输入参数的默认值设定${3:-var_d}
  18. 使用ThreadPoolExecutor进行多线程编程
  19. Python爬虫实例:糗百
  20. WinRAR破解

热门文章

  1. 关于selenium自动化对iframe内嵌元素的处理
  2. springboot中oracle的依赖添加失败的解决
  3. 【转】Python实现智能五子棋
  4. 设计模式(C#)——02抽象工厂模式
  5. [Python] 通过采集两万条数据,对《无名之辈》影评分析
  6. html基础——下拉式菜单
  7. Windows 7 sometimes breaks FTP connections on Java 7 if firewall is enabled.
  8. CF 551 E GukiZ and GukiZiana
  9. Luogu-P2512 [HAOI2008]糖果传递 贪心
  10. bzoj 1051 [HAOI2006]受欢迎的牛(tarjan缩点)