ideaJPA实现mysql_Idea搭建Spring Boot框架,结合jpa实现增删改查

Spring Boot出来很久了,也用了很久了,虽然用的时候也用的挺方便的,但毕竟那个框架不是自己搭的,再怎么运用也不如自己搭建个,在搭的过程中,还是踩了些小坑,下面则是搭建的过程:

打开idea,File--> New--> Project -->Maven -->Next --> Next -->Finish

f17e1af726945b479d33da1a9da9f23a.png

21854776e3af28f42eaa12303416c7e6.png

17a4356a2f9597692436aec87035e583.png

建立项目之后,按照下面的目录依次搭建:

dc43cd6562eb214dbbc5b77ed8fbbf00.png

pom.xml文件如下:

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">

4.0.0

com.test.demo

demo002

1.0-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

1.3.3.RELEASE

1.8

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

dev

true

dev

product

product

config_filters/filter-${active.profile}.properties

src/main/resources

true

org.apache.maven.plugins

maven-compiler-plugin

3.2

1.8

1.8

utf-8

org.springframework.boot

spring-boot-maven-plugin

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

addressList = demoService.queryAddress("%"+name+"%");

List

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

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

{

@Query(value = "select * from address where address like ?",nativeQuery = true)

List

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

发布之后:

84ed1c9e8f8377e6fbec618688987bac.png

发布之后在浏览器访问:http://localhost:8025/hello/query?id=5

8780254fd35837df5a441a0236155a43.png

git地址:https://github.com/DYH2020/springBootDemo/tree/master/demo002

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值