1. 程式人生 > >maven的導入

maven的導入

private pri action 簡單 over per pla man local

一、maven 包的導入

清單如下:

<!-- Inherit defaults from Spring Boot -->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.1.BUILD-SNAPSHOT</version>

</parent>

<!-- Add typical dependencies for a web application -->

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- spring data jpa -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<!-- mysql driver -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<!-- 其他依賴 -->

<dependency>

<groupId>javax.persistence</groupId>

<artifactId>persistence-api</artifactId>

<version>1.0.2</version>

</dependency>

</dependencies>

<!-- Package as an executable jar -->

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

<!-- 編譯器版本指定 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.7</source>

<target>1.7</target>

</configuration>

</plugin>

</plugins>

</build>

<!-- Add Spring repositories -->

<pluginRepositories>

<pluginRepository>

<id>spring-snapshots</id>

<url>http://repo.spring.io/snapshot</url>

</pluginRepository>

<pluginRepository>

<id>spring-milestones</id>

<url>http://repo.spring.io/milestone</url>

</pluginRepository>

</pluginRepositories>

<repositories>

<repository>

<id>spring-snapshots</id>

<url>http://repo.spring.io/snapshot</url>

<snapshots><enabled>true</enabled></snapshots>

</repository>

<repository>

<id>spring-milestones</id>

<url>http://repo.spring.io/milestone</url>

</repository>

</repositories>

以上為整個spring boot 構建簡單web項目所需要的maven配置文件。

二、創建實體類

package com.whucker.platform.domain;

@Entity //實體

@Table(name="t_user") //指定表名

public class User {

@Id //定義主鍵id

@GeneratedValue(strategy=GenerationType.AUTO) //自增

private Long id;

private String name;

private int age;

private int gender;

//...此處省略get/set

}

Repository代碼(此處使用的是spring data jpa,由於該文章主要講解spring boot,所有不在此對jpa做解釋)

package com.whucker.platform.repository;

public interface UserRepository extends CrudRepository<User, Long>{

}

接口定義

package com.whucker.platform.service;

public interface IUserService {

//添加

void save(User user);

//刪除

void delete(Long id);

//修改

void update(User user);

//查詢單個對象

User findOne(Long id);

//查詢所有

List<User> findAll();

}

接口實現

package com.whucker.platform.service.impl;

@Service(value="userService")

@Transactional

public class UserServiceImpl implements IUserService{

@Autowired

private UserRepository userRepository;

@Override

public void save(User user) {

this.userRepository.save(user);

}

}

控制層

package com.whucker.platform.controler;

@RestController

public class IndexManageAction {

@RequestMapping(value="/")

public String index(){

return "歡迎訪問首頁!";

}

}

三、Application類

ackage com.whucker.platform;

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class);

}

配置文件

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/spring-boot-db

spring.datasource.username=root

spring.datasource.password=root

spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update

maven的導入