上篇演示了通过Maven构建Spring Boot 项目,引用web模块启动应用,完成简单的web 应用访问,本章内容在此基础上面加入数据访问与端口修改,下文代码与演例(本用例纯手工测试通过,放心入坑)。
修改默认端口
在src\main\resources下加入application.properties内容如下
server.port=8888
项目目录结构
启动应用,日志显示:
端口已经由默认的8080 变更为8888
JPA访问mysql数据库
1、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>
2、在src\test\resources下加入application.properties内容如下(正式应用中请把配置加入至src\main\resources下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
3、新建实体
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()); } }
运行结果示例
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。