Spring boot and Spring Data JPA 超简单例子
来源:互联网 发布:linux 显示当前目录 编辑:程序博客网 时间:2024/06/05 11:24
DEMO 结构
build.gradle
buildscript {ext {springBootVersion = '1.5.6.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'org.springframework.boot'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8repositories {// mavenCentral()maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }}dependencies {compile('org.springframework.boot:spring-boot-starter-web')compile('org.springframework.boot:spring-boot-starter-data-jpa')compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.41'compile group: 'com.alibaba', name: 'druid', version: '1.0.29'testCompile('org.springframework.boot:spring-boot-starter-test')}
UserDao.java
// 必须继承 Repository 及其子接口(CrudRepository, PagingAndSortingRepository 等), 各种查询方法才会起作用// Repository 为空接口,必须自己定义相关的方法public interface UserDao extends CrudRepository<User, Integer> {// 解析方法名创建查询public User findByName(String name);// 使用 @Query 创建查询@Query("select bean from User bean where bean.name like %?1%")public List<User> findUserByQuery(String name);}
User.java
就是一个简单的 java bean
@Entity@Table(name = "user")public class User {private Integer id;private String name;private String age;@Id@GeneratedValue(strategy = GenerationType.AUTO)public Integer getId() {return id;}....}
UserService.java
public interface UserService {public void save();public User findUser(String name);public List<User> findUserByQuery(String name);}
UserServiceImpl.java
@Servicepublic class UserServiceImpl implements UserService {@Overridepublic void save() {Integer i = new Random().nextInt(10) + 1;User user = new User();user.setName("maiyo" + i);user.setAge("" + i);dao.save(user);}@Overridepublic User findUser(String name) {return dao.findByName(name);}@Overridepublic List<User> findUserByQuery(String name) {return dao.findUserByQuery(name);}@Autowiredprivate UserDao dao;}
application.properties
使用 druid 数据源 ( Spring boot 自动配置的数据源是 org.apache.tomcat.jdbc.pool.DataSource )
新版 Mysql 需要指定 useSSL
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring_boot_db?characterEncoding=UTF-8&useSSL=truespring.datasource.username=rootspring.datasource.password=123321abcspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.type=com.alibaba.druid.pool.DruidDataSourceApplicationTests.java
使用 ApplicationTests 类进行单元测试
@RunWith(SpringRunner.class)@SpringBootTestpublic class ApplicationTests {@Autowiredprivate UserService userService;@Testpublic void saveUser() {userService.save();}@Testpublic void findUser() {User user = userService.findUser("maiyo");System.out.println(user.getAge());}@Testpublic void findUserByQuery() {List<User> users = userService.findUserByQuery("maiyo");for (User u : users) {System.out.println(u.getAge());}}}OK.
阅读全文
0 0
- Spring boot and Spring Data JPA 超简单例子
- spring boot and spring data jpa
- spring-boot-data-jpa
- Spring Data JPA例子[基于Spring Boot、Mysql]
- Spring Data JPA例子[基于Spring Boot、Mysql]
- Spring Data JPA例子[基于Spring Boot、Mysql]
- Spring Boot+Spring Data JPA+Spring Cache
- spring boot集成data-jpa
- Spring Data JPA 入门例子
- Spring Boot中使用Spring-data-jpa
- spring boot 使用spring data jpa
- spring boot 使用 spring data jpa
- Spring Boot中使用Spring-data-jpa
- 《spring-boot学习》-07-spring data jpa
- spring boot中使用spring-data-jpa
- Spring Boot中使用Spring-data-jpa
- 初识spring boot和spring data jpa
- Spring Boot [组件学习-Spring Data JPA]
- 输出cglib以及jdk动态代理产生的class文件
- lombok的相关注解
- JavaScript对象(3)——继承
- PHP的PDO模式数据操作
- 包装类、正则验证
- Spring boot and Spring Data JPA 超简单例子
- 测试用例优先级划分
- 第一天小结
- 欢迎使用CSDN-markdown编辑器
- Nginx的配置与部署研究,Upstream负载均衡模块
- 配置过程中的一些问题
- 集合一
- android中ViewPager禁止滑动
- 第3章-垃圾收集器与内存分配策略 第4章-虚拟机性能监控与故障处理工具