Spring Boot Hibernate

Source: Internet
Author: User
Tags json parent directory

In the previous article I have described how to implement the Spring BOOTMVC framework and now introduce hibernate

First of all, spring boot has fused hibernate, so you don't need to import any jar files.

Here you only need to add a jar file for jar,springboot data processing

<!--spring boot data processing-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Of course, there is the most basic database link jar file

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>


All right

Then we add the relevant database configuration, in the original application.properties file to add

Spring.datasource.url =jdbc:mysql://localhost:3306/test


Spring.datasource.username = root


Spring.datasource.password = 123456


Spring.datasource.driverClassName =com.mysql.jdbc.driver

#指定连接池中最大的活跃连接数.
Spring.datasource.max-active=20

#指定连接池最大的空闲连接数量.
Spring.datasource.max-idle=8

#指定必须保持连接的最小值
Spring.datasource.min-idle=8

#指定启动连接池时, initial number of connections established
spring.datasource.initial-size=10


#指定目标数据库.
Spring.jpa.database = MYSQL


#是否开启sql的log, the default is: false
Spring.jpa.show-sql = True

#hibernate打印sql
Hiernate.show_sql=true

#
Spring.jpa.properties.hibernate.hbm2ddl.auto=update

What do these configurations mean, take a look at this article, I think you'll learn more when you're done, so there's no explanation here.

https://segmentfault.com/a/1190000004316491

Then create the DAO interface

Package Com.mx.dao;


Import java.util.List;


Import Com.mx.model.User;


public interface userdao{

List<user> getList ();


}

And then the realization, here I am hibernate familiar with some of the ways, kind of it.

Package Com.mx.dao.impl;


Import java.util.List;


Import Javax.persistence.EntityManager;
Import Javax.persistence.PersistenceContext;
Import Javax.persistence.Query;


Import Org.springframework.stereotype.Repository;


Import Com.mx.dao.UserDao;
Import Com.mx.model.User;


@Repository
public class Userdaoimpl implements Userdao {

@PersistenceContext
Private Entitymanager Entitymanager;


@Override
Public list<user> getList () {
TODO auto-generated Method Stub
Query query=entitymanager.createquery ("from User");
return Query.getresultlist ();
}


}

Then create the service

Package com.mx.service;


Import java.util.List;


Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;


Import Com.mx.dao.UserDao;
Import Com.mx.model.User;


@Service ("UserService")
public class UserService {

@Autowired
Private Userdao Userdao;

Public list<user> getList () {
List<user> list=userdao.getlist ();
return list;
}


}

Here if the service interface, out of the presence of beans can not be injected, specific reasons, I am not clear, follow-up study and then change, if someone understand the following comments welcome.

Here we introduce an article

http://blog.csdn.net/ye1992/article/details/19971467

OK, I'll test with JUnit first:

Here I'm going to add a jar, Ali's JSON tool, formatted data, looks convenient to write

 <!-- json-->
  <dependency>
    <groupId> Com.alibaba</groupid>
    <artifactid>fastjson</artifactid>
     <version>1.2.37</version>
</dependency>

Package MX1;


Import java.util.List;


Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.boot.test.SpringApplicationConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


Import Com.alibaba.fastjson.JSON;
Import COM.MX.STARTMVC;
Import Com.mx.model.User;
Import Com.mx.service.UserService;


@RunWith (Springjunit4classrunner.class)
@SpringApplicationConfiguration (Classes=startmvc.class)
public class JunitTest1 {


@Autowired
UserService UserService;

@Test
public void Test1 () {
List<user> list=userservice.getlist ();
String json=json.tojsonstring (list);
SYSTEM.OUT.PRINTLN (JSON);
}


}

Run a test, please.


Then implement the Controller

Package Com.mx.controller;


Import java.util.List;


Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;


Import Com.mx.model.User;
Import Com.mx.service.UserService;




@Controller
public class Indexcontroller {

@Autowired
UserService UserService;


@RequestMapping ("/index1")
Public String Index () {
List<user> list=userservice.getlist ();
SYSTEM.OUT.PRINTLN (list);
Return "index";
}

@RequestMapping ("/test3")
Public String test3 () {


return "Eee";
}


}

Here's a very important question.

Remember that the spring boot class must be placed in front of all packages

The Startmvc.java is placed in the other file's parent directory. For example, it is generally com.mx.controller so Startmvc.java is placed under the com.mx bag. Otherwise, no other beans will be found and cannot be injected



All right, test it yourself.

There are questions, the following comments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.