SpringBoot和Hibernate整合
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:
SpringBoot和Hibernate整合的更多相关文章
- 带着新人学springboot的应用08(springboot+jpa的整合)
这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...
- 在 springboot 中如何整合 shiro 应用 ?
Shiro是Apache下的一个开源项目,我们称之为Apache Shiro. 它是一个很易用与Java项目的的安全框架,提供了认证.授权.加密.会话管理,与spring Security 一样都是 ...
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...
- Hibernate整合C3P0实现连接池
Hibernate整合C3P0实现连接池 hibernate中可以使用默认的连接池,无论功能与性能都不如C3PO(网友反映,我没有测试过),C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI ...
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- Spring与Hibernate整合,实现Hibernate事务管理
1.所需的jar包 连接池/数据库驱动包 Hibernate相关jar Spring 核心包(5个) Spring aop 包(4个) spring-orm-3.2.5.RELEASE.jar ...
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- Spring与Hibernate整合中,使用OpenSessionInViewFilter后出现sessionFactory未注入问题
近期在知乎看到一句话,保持学习的有一种是你看到了很多其它的牛人,不甘心,真的不甘心. Spring和hibernate整合的时候,jsp页面做展现,发现展现属性出现: org.apache.jaspe ...
- 框架篇:Spring+SpringMVC+hibernate整合开发
前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...
随机推荐
- 小白学Python(3)——输入和输出,显示你的名字
任何计算机程序都是为了执行一个特定的任务,有了输入,用户才能告诉计算机程序所需的信息,有了输出,程序运行后才能告诉用户任务的结果. 输入是Input,输出是Output,因此,我们把输入输出统称为In ...
- 教你用原生CSS写炫酷页面切换效果,跟第三方组件说拜拜
因为项目需要,别人想让我给他写一个个人博客,并且给了我一个其他人的网页,可以点此查看.有的同学可能说了,第三方博客框架这么多,为什么还要去手写的,你说这个有可能是没有看到打开这个博客. 样式介绍 给大 ...
- 100天搞定机器学习|day44 k均值聚类数学推导与python实现
[如何正确使用「K均值聚类」? 1.k均值聚类模型 给定样本,每个样本都是m为特征向量,模型目标是将n个样本分到k个不停的类或簇中,每个样本到其所属类的中心的距离最小,每个样本只能属于一个类.用C表示 ...
- appium+python自动化项目实战(一):引入nose和allure框架
本文将介绍一套比较完整的appium自动化框架,以python为编写脚本语言,是因为python有强大的库,同时易学易懂. 最终的测试框架代码,将在jenkins项目中一键构建,执行自动化测试用例,并 ...
- nginx之location详解
location有定位的意思,根据uri来进行不同的定位,在虚拟主机中是必不可少的,location可以定位网站的不同部分,定位到不同的处理方式上. location匹配分类 精准匹配 一般匹配 正则 ...
- 使用flask-restful搭建API
最简单的例子 ---~~~~ 访问http://127.0.0.1:5000/ , 返回{"hello": "world"} from flask import ...
- PIXIJS的一些使用
我发现pixijs在国内简直就是一片静土啊,只有那么一点点的微弱的不能再微弱的声音. 我在这里整理了下我使用过程中解决和可能理解的一些问题吧,都是一个个点,而不是完整的示例. 先放官网示例: http ...
- Linux之Shell编程(14)
变量: 定义变量的规则: 1)变量名可以由字母.数字和下划线组成,但不能以数字开头 2)等号两侧不能有空格 3)变量名一般习惯大写 将命令的返回值赋值给变量: 1)使用``将命令括起来 2)使用$() ...
- GMTC 2019-前端夜话《聊聊前端工程师的成长和发展》会后简要总结
今天晚上去参加了winter主持的前端夜话:聊聊前端工程师的成长和发展圆桌论坛分享会,真的是收益颇多,这次的这个嘉宾阵容也是很有诚意的,在现在前端领域都是有一定影响力的嘉宾,嘉宾阵容也列一下: 主 ...
- ios5与ios7数字输入样式一致
<input name="activeCode" id="activeCode" ontouchstart="this.type='numbe ...