SpringBoot整合hibernate

来源:互联网 发布:卓越精算软件使用方法 编辑:程序博客网 时间:2024/05/17 23:28

添加springboot依赖

先使用idea创建maven项目,创建过程可以参考下面的博客:

http://blog.csdn.net/supervictim/article/details/53490046

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

<dependencyManagement>    <dependencies>      <dependency>        <!-- Import dependency management from Spring Boot -->        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-dependencies</artifactId>        <version>1.3.5.RELEASE</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencyManagement>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

springMVC访问实例

上面添加了springboot依赖,可以使用springMVC,另外还添加了thymeleaf模板的依赖,下面看一个最简单的springMVC实例:

package cn.karent.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by wan on 2017/1/17. */@Controller@RequestMapping("/hello")public class HelloController {    @RequestMapping("first")    public String first(Model model) {        model.addAttribute("hello", "world");        return "first";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

thymeleaf模板如下,fist.html:

<!DOCTYPE html><html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"><head>    <meta charset="UTF-8"></meta>    <title>Title</title></head><body>    <p th:text="'nihao:' + ${hello}"/></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

结果: 
这里写图片描述 
注意:里面的thymeleaf模板必须创建在resources文件夹下面的templates文件夹(这个文件夹默认不存在,需要自己创建一个名字叫做templates的文件夹)下面 
这里写图片描述

添加jpa依赖

接下来再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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

spring.datasource.url = jdbc:mysql://localhost:3306/testspring.datasource.username = rootspring.datasource.password = rootspring.datasource.driverClassName = com.mysql.jdbc.Driver# Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sql queryspring.jpa.show-sql = true# Hibernate ddl auto (create, create-drop, update)spring.jpa.hibernate.ddl-auto = update# Naming strategyspring.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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注意:这个application.properties文件也是创建在resources文件夹下面,在myecplise里面创建配置文件是放在src文件夹下面,但是最后编译出来的文件还是在classes文件夹下面 
这里写图片描述 
这里写图片描述 
这里写图片描述 
main方法的写法(添加一些注解):

package cn.karent;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.orm.jpa.EntityScan;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;/** * Created by wan on 2017/1/17. */@SpringBootApplication@EnableJpaRepositories(basePackages = "cn.karent.repository")@EntityScan(basePackages = "cn.karent")public class AppConfig {    public static void main(String[] args) {        SpringApplication.run(AppConfig.class, args);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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

package cn.karent.entity;import javax.persistence.*;import java.util.Date;/** * Created by wan on 2017/1/17. */@Entity@Table(name="user")public class User {    @Id    @GeneratedValue(strategy= GenerationType.AUTO)    private Long id;    private String userName;    private Date birthDay;    private String sex;    private String address;    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public Date getBirthDay() {        return birthDay;    }    public void setBirthDay(Date birthDay) {        this.birthDay = birthDay;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

UserRepository:

package cn.karent.repository;import cn.karent.entity.User;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.CrudRepository;import org.springframework.data.repository.query.Param;import org.springframework.stereotype.Repository;import javax.persistence.Table;/** * Created by wan on 2017/1/17. */@Repository@Table(name="user")@Qualifier("userRepository")public interface UserRepository extends CrudRepository<User, Long > {    public User findOne(Long id);    public User save(User u);    @Query("select t from User t where t.userName=:name")    public User findUserByName(@Param("name") String name);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

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

package cn.karent.controller;import cn.karent.entity.User;import cn.karent.repository.UserRepository;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;/** * Created by wan on 2017/1/17. */@Controller@RequestMapping("/hibernate")@EnableAutoConfigurationpublic 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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

@Autowired代表按照类型注入,@Resource按照名称注入 
访问http://localhost:8080/hibernate/saveUser 之后,jpa自动生成sql语句: 
这里写图片描述 
访问http://localhost:8080/hibernate/getUserById?id=2: 
这里写图片描述

参考

http://www.cnblogs.com/leskang/p/5445698.html 
http://www.tuicool.com/articles/zEz2QrY

0 0
原创粉丝点击