SpringBoot pom文件
程序员文章站
2022-03-02 16:51:19
...
POM.XML引入JAR包
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mavendemo</groupId>
<artifactId>INVOICINGSYSTEM</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>INVOICINGSYSTEM Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<!-- 项目全局属性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
<junit-version>4.12</junit-version>
</properties>
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot devtools 热部署 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- Spring Boot JPA 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Junit 单元测试 依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<!-- SpringBoot 项目发布 打jar包 依赖 -->
<build>
<plugins>
<!-- SpringBoot 项目发布 打jar包 依赖 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Maven test junit 报告中文UTF-8编码 插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置
spring.datasource.url=jdbc:mysql://localhost:3306/INVOICINGSYSTEMSQL?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
server.port=8090
server.tomcat.uri-encoding=UTF-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8:
APP
package com.invoicin.App;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.invoicin.App.dao")
@EntityScan(basePackages = "com.invoicin.App.entity")
public class App {
public static void main(String[] args) {
SpringApplication springs = new SpringApplication(App.class);
springs.run(args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
}
DAO
package com.invoicin.App.dao;
import java.util.List;
import javax.persistence.Transient;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.invoicin.App.entity.City;
import com.invoicin.App.entity.ProvinceInfo;
public interface CityDao extends JpaRepository<City,Integer> {
@Query(value="SELECT * FROM cities WHERE city like %:city% ",nativeQuery=true)
List<City> selByLike(@Param("city")String city);
City findByCityId(Integer cityId);
@Query(value="SELECT * FROM cities c LEFT JOIN city_province_tb cp ON c.c_id=cp.c_id "
+ "WHERE cp.p_id=?1",nativeQuery=true)
@Modifying
List<City> selByPid(Integer pId);
}
Entity
package com.invoicin.App.entity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import lombok.Data;
@Entity
@Table(name="cities")
public class City {
@Id
@GeneratedValue
private Integer cId; //id
private String cityId; //城市
private String city; //城市id
@ManyToMany
@JoinTable(name="city_province_tb",joinColumns={@JoinColumn(name="cId")}
,inverseJoinColumns= {@JoinColumn(name="pId")})
@NotFound(action = NotFoundAction.IGNORE)
private List<ProvinceInfo> province = new ArrayList<ProvinceInfo>();
public Integer getcId() {
return cId;
}
public void setcId(Integer cId) {
this.cId = cId;
}
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public List<ProvinceInfo> getProvince() {
return province;
}
public void setProvince(List<ProvinceInfo> province) {
this.province = province;
}
public City(Integer cId, String cityId, String city, List<ProvinceInfo> province) {
super();
this.cId = cId;
this.cityId = cityId;
this.city = city;
this.province = province;
}
public City() {
super();
// TODO Auto-generated constructor stub
}
}
Controller
package com.invoicin.App.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.invoicin.App.dao.CityDao;
import com.invoicin.App.entity.City;
import com.invoicin.App.entity.ProvinceInfo;
import com.invoicin.App.service.CityService;
import com.invoicin.App.service.ProvinceService;
@RestController
@CrossOrigin
public class CityController {
@Autowired
private CityService cService;
@Autowired
private CityDao cdao;
private Map<String, Object> map = new HashMap<String,Object>();
//localhost:8090/cityall
@RequestMapping("/cityall")
public Object queryAll() {
List<City> list = cdao.findAll();
return list;
}
@RequestMapping("/cityByname")
public Object queryByname(String city) {
List<City> pro = cService.selByLike(city);
return pro;
}
@RequestMapping("/cityById")
public Object selByPid(Integer pId) {
List<City> list = cdao.selByPid(pId);
return list;
}
}
Service
package com.invoicin.App.service;
import java.util.List;
import org.springframework.data.repository.query.Param;
import com.invoicin.App.entity.City;
import com.invoicin.App.entity.ProvinceInfo;
public interface CityService {
List<City> selByLike(String city);
City findByCityId(Integer cityId);
}
ServiceImpl
package com.invoicin.App.serviceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.invoicin.App.dao.CityDao;
import com.invoicin.App.entity.City;
import com.invoicin.App.service.CityService;
@Service
public class CityServiceImpl implements CityService{
@Autowired
private CityDao cdao;
@Override
public List<City> selByLike(String city) {
// TODO Auto-generated method stub
return cdao.selByLike("%"+city+"%");
}
@Override
public City findByCityId(Integer cityId) {
// TODO Auto-generated method stub
return cdao.findByCityId(cityId);
}
}