Idea搭建Spring Boot框架,结合jpa实现增删改查
Spring Boot出来很久了,也用了很久了,虽然用的时候也用的挺方便的,但毕竟那个框架不是自己搭的,再怎么运用也不如自己搭建个,在搭的过程中,还是踩了些小坑,下面则是搭建的过程:
打开idea,File--> New--> Project -->Maven -->Next --> Next -->Finish
建立项目之后,按照下面的目录依次搭建:
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.0</modelVersion>
<groupId>com.test.demo</groupId>
<artifactId>demo002</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- jpa:将对象映射到关系数据库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- web核心包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<!-- 不同的环境,可以在不同的环境下分开,连接不同的数据库 -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<active.profile>dev</active.profile>
</properties>
</profile>
<profile>
<id>product</id>
<properties>
<active.profile>product</active.profile>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>config_filters/filter-${active.profile}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties 文件如下:
server.port=8025
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=1
spring.datasource.initial-size=1
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=18800
spring.datasource.url=@db.dyh2020.url@
spring.datasource.username=@db.dyh2020.username@
spring.datasource.password=@db.dyh2020.password@
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
# 显示后台处理的SQL语句
spring.jpa.show-sql=true
# 自动检查实体和数据库表是否一致,如果不一致则会进行更新数据库表
spring.jpa.hibernate.ddl-auto=none
Main.java文件如下:
package com.test.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan(basePackages = "/com.test.demo")
public class Main {
public static void main(String[] args){
SpringApplication.run(Main.class, args);
}
}
DemoController.java文件:
package com.test.demo.controllers;
import com.test.demo.domain.entities.Address;
import com.test.demo.domain.entities.AddressRepository;
import com.test.demo.services.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/hello")
public class DemoController {
@Autowired
private DemoService demoService;
@Autowired
private AddressRepository addressRepository;
/**
* RequestParam 参数里面的name和value的效果是一样的
* RequestMapping 参数里面就只能是value了,
* @param name
* @return
*/
@RequestMapping(value = "/demo")
public String demo(@RequestParam(name = "name")String name, @RequestParam(value = "id")int id){
return "hello "+ name+"\t"+id;
}
/**
* RequestParam 参数里面的name和value的效果是一样的
* RequestMapping 参数里面就只能是value了,
* @param name
* @return
*/
@RequestMapping(value = "/queryaddress")
public String demo(@RequestParam(name = "name")String name){
List<Address> addressList = demoService.queryAddress("%"+name+"%");
List<Address> addressList2 = addressRepository.queryListByName("%"+name+"%");
System.out.println(addressList.toString());
System.out.println(addressList2.toString());
return addressList.toString();
// return "wolaile";
}
/**
* 查找
* @param id
* @return
*/
@RequestMapping("/query")
public String query(@RequestParam(name = "id")int id){
Address a = demoService.findById(id);
return a==null?"没有符合查找的东西":a.toString();
}
/**
* 查找
* @param id
* @return
*/
@RequestMapping("/update")
public String query(@RequestParam(name = "id")int id,@RequestParam(name = "address")String address){
Address a = demoService.updateName(id,address);
return a==null?"什么也没有":a.toString();
}
}
DemoService.java文件如下:
package com.test.demo.services;
import com.test.demo.domain.entities.Address;
import com.test.demo.domain.entities.AddressRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DemoService {
@Autowired
private AddressRepository addressRepository;
/**
* 根据名称查找地区
* @param name
* @return
*/
public List<Address> queryAddress(String name){
return addressRepository.queryListByName(name);
}
public Address findById(int id){
return addressRepository.findOne(id);
}
public Address updateName(int id, String address){
Address a = addressRepository.findOne(id);
if (a!=null){
a.setAddress(address);
addressRepository.save(a);
return a;
}else
return null;
}
}
Address.java实体如下:
package com.test.demo.domain.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Entity
@Table(name = "address")
public class Address {
/**
* 主键
*/
@Id
private Integer id;
/**
* 省
*/
@Column(name = "province", nullable = false)
private String province;
/**
* 市
*/
@Column(name = "city", nullable = false)
private String city;
/**
* address
*/
@Column(name = "address", nullable = false)
private String address;
/**
* 创建时间
*/
@Column(name = "create_time", nullable = false)
private Date createTime;
/**
* 创建时间
*/
@Column(name = "update_time", nullable = false)
private Date updateTime;
/**
* contact
*/
@Column(name = "contact", nullable = false)
private String contact;
/**
* appellation
*/
@Column(name = "appellation", nullable = false)
private String appellation;
/**
* deleted
*/
@Column(name = "deleted", nullable = false)
private Boolean deleted;
/**
* deleted
*/
@Column(name = "mobile", nullable = false)
private String mobile;
/**
* status
*/
@Column(name = "status", nullable = false )
private Integer status;
/**
* user_id
*/
@Column(name = "user_id", nullable = false )
private Integer userId;
/**
* province_id
*/
@Column(name = "province_id", nullable = false )
private Integer provinceId;
/**
* city_id
*/
@Column(name = "city_id", nullable = false )
private Integer cityId;
/**
* area_id
*/
@Column(name = "area_id", nullable = false )
private Integer areaId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getAppellation() {
return appellation;
}
public void setAppellation(String appellation) {
this.appellation = appellation;
}
public Boolean getDeleted() {
return deleted;
}
public void setDeleted(Boolean deleted) {
this.deleted = deleted;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getProvinceId() {
return provinceId;
}
public void setProvinceId(Integer provinceId) {
this.provinceId = provinceId;
}
public Integer getCityId() {
return cityId;
}
public void setCityId(Integer cityId) {
this.cityId = cityId;
}
public Integer getAreaId() {
return areaId;
}
public void setAreaId(Integer areaId) {
this.areaId = areaId;
}
@Override
public String toString() {
return "Address{" +
"id=" + id +
", province='" + province + '\'' +
", city='" + city + '\'' +
", address='" + address + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
", contact='" + contact + '\'' +
", appellation='" + appellation + '\'' +
", deleted=" + deleted +
", mobile='" + mobile + '\'' +
", status=" + status +
", userId=" + userId +
", provinceId=" + provinceId +
", cityId=" + cityId +
", areaId=" + areaId +
'}';
}
}
AddressRepository.java 文件如下:
package com.test.demo.domain.entities;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface AddressRepository extends JpaRepository<Address, Integer>{
@Query(value = "select * from address where address like ?",nativeQuery = true)
List<Address> queryListByName(String name);
}
数据库连接文件eg:filter-dev.properties文件
# 数据库连接
db.dyh2020.url=jdbc:mysql://127.0.0.1:3306/dyh_test?characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true&autoReconnect=true
db.dyh2020.username=root
db.dyh2020.password=admin
#日志级别
#logging.level.root=ERROR
发布之后:
发布之后在浏览器访问:http://localhost:8025/hello/query?id=5
git地址:https://github.com/DYH2020/springBootDemo/tree/master/demo002