一,準(zhǔn)備工作,建立spring-boot-sample-mysql工程
1、http://start.spring.io/
A、Artifact中輸入spring-boot-sample-MySQL
B、勾選Web下的web
C、勾選SQL下的JPA MYSQL
2、Eclips中導(dǎo)入工程spring-boot-sample-mysql
A、解壓快捷工程spring-boot-sample-mysql到某文件夾
B、eclips中file->import->Import Existing Maven Projects-->Select Maven projects-->finish導(dǎo)入工程
3、工程導(dǎo)入之后,文件結(jié)構(gòu)如下圖
4、在包com.example下建立web文件夾
5、便于測試,引入spring-boot-sample-helloworld的HelloController及配置文件logback.xml
HelloController代碼為
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.example.web; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 { protected static Logger logger=LoggerFactory.getLogger(HelloController. class ); @RequestMapping ( "/" ) public String helloworld(){ logger.debug( "訪問hello" ); return "Hello world!" ; } @RequestMapping ( "/hello/{name}" ) public String helloName( @PathVariable String name){ logger.debug( "訪問helloName,Name={}" ,name); return "Hello " +name; } } |
logback.xml配置為
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
|
<configuration> <!-- %m輸出的信息,%p日志級別,%t線程名,%d日期,%c類的全名,,,, --> <appender name= "STDOUT" class = "ch.qos.logback.core.ConsoleAppender" > <encoder> <pattern>%d %p (%file:%line\)- %m%n</pattern> <charset>GBK</charset> </encoder> </appender> <appender name= "baselog" class = "ch.qos.logback.core.rolling.RollingFileAppender" > <File>log/base.log</File> <rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <fileNamePattern>log/base.log.%d.i%</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class = "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > <!-- or whenever the file size reaches 64 MB --> <maxFileSize> 64 MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern> %d %p (%file:%line\)- %m%n </pattern> <charset>UTF- 8 </charset> <!-- 此處設(shè)置字符集 --> </encoder> </appender> <root level= "info" > <appender-ref ref= "STDOUT" /> </root> <logger name= "com.example" level= "DEBUG" > <appender-ref ref= "baselog" /> </logger> </configuration> |
注:logback.xml文件位于src/main/resources下
6、啟動工程,通過瀏覽器查看正確性
http://localhost:8080/
http://localhost:8080/hello/上帝
二,使用JPA,構(gòu)建業(yè)務(wù)對象及訪問庫
1、在包com.example下建立domain文件夾
2、在domain中建立類Person
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
|
package com.example.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Person { @Id @GeneratedValue private Long id; private String name; private Integer age; private String address; public Person() { super (); } public Person(Long id, String name, Integer age, String address) { super (); this .id = id; this .name = name; this .age = age; this .address = address; } public Long getId() { return id; } public void setId(Long id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } public String getAddress() { return address; } public void setAddress(String address) { this .address = address; } } |
注意:構(gòu)造函數(shù)
3、在包com.example下建立repository文件夾
4、在repository中建立接口PersonRepository
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.example.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.example.domain.Person; @Repository public interface PersonRepository extends JpaRepository<Person,Long> { List<Person> findByName(String name); List<Person> findByAddress(String address); List<Person> findByNameAndAddress(String name,String address); @Query ( "select p from Person p where p.name=:name and p.address=:address" ) List<Person> withNameAndAddressQuery( @Param ( "name" )String Name, @Param ( "address" )String address); } |
5、在web中建立DataController
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
|
package com.example.web; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.domain.Person; import com.example.repository.PersonRepository; @RestController public class DataController { protected static Logger logger=LoggerFactory.getLogger(DataController. class ); @Autowired PersonRepository personRepository; @RequestMapping ( "/save" ) public Person save(String name,String address,Integer age){ logger.debug( "save 開始" ); Person p=personRepository.save( new Person( null ,name,age,address)); logger.debug( "save 結(jié)束" ); return p; } @RequestMapping ( "/q1" ) public List<Person> q1(String address){ logger.debug( "q1 開始" ); logger.debug( "q1 接收參數(shù)address={}" ,address); List<Person> people=personRepository.findByAddress(address); return people; } @RequestMapping ( "/q2" ) public List<Person> q2(String name,String address){ logger.debug( "q2 開始" ); logger.debug( "q2接收參數(shù)name={},address={}" ,name,address); return personRepository.findByNameAndAddress(name, address); } @RequestMapping ( "/q3" ) public List<Person> q3(String name,String address){ logger.debug( "q3 開始" ); logger.debug( "q3接收參數(shù)name={},address={}" ,name,address); return personRepository.withNameAndAddressQuery(name, address); } @RequestMapping ( "/sort" ) public List<Person> sort(){ logger.debug( "sort 開始" ); List<Person> people=personRepository.findAll( new Sort(Direction.ASC, "age" )); return people; } @RequestMapping ( "/page" ) public Page<Person> page(){ logger.debug( "page 開始" ); Page<Person> people=personRepository.findAll( new PageRequest( 1 , 2 )); return people; } } |
6、配置數(shù)據(jù)庫連接,在application.properties(src/main/resources下)
1
2
3
4
5
6
7
|
spring.datasource.url=jdbc:mysql: //192.168.56.201:3306/bootsample?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password= 123456 spring.datasource.driver- class -name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql= true spring.jackson.serialization.indent_output= true |
7、運行測試
A、先保存數(shù)據(jù)
http://localhost:8080/save?name=aa&&address=北京&&age=1
http://localhost:8080/save?name=ab&&address=北京&&age=2
http://localhost:8080/save?name=cq1&&address=重慶&&age=50
http://localhost:8080/save?name=cq2&&address=重慶&&age=51
B、查詢q1
http://localhost:8080/q1?address=北京
C、查詢q2
http://localhost:8080/q2?address=北京&&name=aa
D、查詢q3
http://localhost:8080/q3?address=北京&&name=aa
E、排序
http://localhost:8080/sort
F、分頁
http://localhost:8080/page
運用hibernate訪問mysql,基本也是老技術(shù),只是用JPA簡化了dao層代碼,對于業(yè)務(wù)對象基本沒有變化。
以上所述是小編給大家介紹的SpringBoot入門系列之JPA mysql,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/sosfnima/article/details/51993689