上篇演示了通过Maven构建Spring Boot 项目,引用web模块启动应用,完成简单的web 应用访问,本章内容在此基础上面加入数据访问与端口修改,下文代码与演例(本用例纯手工测试通过,放心入坑)。
修改默认端口
- 在src\mainresources下加入application.properties内容如下
server.port=8888
项目目录结构
- 启动应用,日志显示:
端口已经由默认的8080 变更为8888
JPA访问mysql数据库
- POM中加入
<!-- Spring Boot JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>RELEASE</version>
</dependency>
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
- 在src\testresources下加入application.properties内容如下(正式应用中请把配置加入至src\mainresources下application.properties中):
server.port=8888
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
- create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变 (生产禁用,不小心用了会哭的。。)
- create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除 (生产禁用)
- update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行
- validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值
- 新建实体
src\main\java\com\entity\User.java
package com.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name="t_user")
public class User implements Serializable {
private static final long serialVersionUID = -3258839839160856613L;
@Id
@GeneratedValue
private Long id;
private String name;
private String moblie;
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 String getMoblie() {
return moblie;
}
public void setMoblie(String moblie) {
this.moblie = moblie;
}
}
- 新建数据访问接口(JPA)
src\main\java\com\dao\UserRepository .java
package com.dao;
import entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Description:
* date: 2017/3/15 16:28
*/
public interface UserRepository extends JpaRepository<User, Long> {
User findByName(String name);
}
从源码上面可以看出,JpaRepository已经实现了save(更新与保存)、delete、getOne、findAll等方法,所以对于基础数据的操作,接口上不需要再定义,直接使用就好。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.data.jpa.repository;
import java.io.Serializable;
import java.util.List;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
@NoRepositoryBean
public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
List<T> findAll();
List<T> findAll(Sort var1);
List<T> findAll(Iterable<ID> var1);
<S extends T> List<S> save(Iterable<S> var1);
void flush();
<S extends T> S saveAndFlush(S var1);
void deleteInBatch(Iterable<T> var1);
void deleteAllInBatch();
T getOne(ID var1);
<S extends T> List<S> findAll(Example<S> var1);
<S extends T> List<S> findAll(Example<S> var1, Sort var2);
}
4.编写对应的单元测试来验证编写的内容是否正确
- POM中加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
- 创建单元测试用例
src\test\java\UserTest.java
import com.SampleController;
import com.dao.UserRepository;
import com.entity.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* date: 2017/3/15 17:21
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SampleController.class)
public class UserTest {
@Autowired
private UserRepository userRepository;
@Test
public void saveTest() {
User user = new User();
user.setName("王大锤");
user.setMoblie("13300000000");
userRepository.save(user);
Assert.assertEquals("13300000000", userRepository.findByName("王大锤").getMoblie());
}
}
运行结果示例
参考资料:
http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/