在成功创建hello word后,接下来我首先想到了数据库的访问,之前也用过不少ORM,Hibernate、Mybatis,既然是学习就要学习没用过的不是,spring有自己的解决方案,那就是spring-data-jpa,这篇文章主要学习使用spring-data-jpa来访问数据库的过程。
1.我使用的数据库是mysql,首先将spring-data-jpa和mysql驱动的jar包导入到项目中来。
<span style="white-space:pre"> </span><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
2.配置连接mysql数据库,在application.properties添加以下内容
########################################数据库配置#########################################
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot
spring.datasource.username=root
spring.datasource.password=Password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.创建一个人员表对应的实体UserDomain
package com.zity.springboot.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class UserDomain {
/** 用户id */
@Id
@GeneratedValue
private long id;
/** 用户编号 */
@Column(nullable = true)
private String userNo;
/** 用户名称 */
@Column(nullable = true)
private String userName;
/** 用户密码 */
@Column(nullable = true)
private String userPwd;
/** 用户盐值 */
@Column(nullable = true)
private String salt;
/** 用户状态 */
@Column(nullable = true)
private String userStatus;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUserNo() {
return userNo;
}
public void setUserNo(String userNo) {
this.userNo = userNo;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public String getUserStatus() {
return userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
}
4.创建业务service层和数据访问dao层
5.在UserDao文件中写入以下代码
package com.zity.springboot.dao;
import com.zity.springboot.domain.UserDomain;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by Andy on 2016/10/30.
*/
public interface UserDao extends JpaRepository<UserDomain, Long> {
/**
* 根据用户名称查询用户
* @param userNo
* @return
*/
UserDomain findByUserNo(String userNo);
}
6.在UserService.java和UserServiceImpl.java中分别添加
package com.zity.springboot.service;
import com.zity.springboot.domain.UserDomain;
/**
* Created by Andy on 2016/10/30.
*/
public interface UserService {
/**
* 根据用户编号获取用户信息
* @param userNo
* @return
*/
UserDomain findUserByUserNo(String userNo);
}
package com.zity.springboot.service;
import com.zity.springboot.dao.UserDao;
import com.zity.springboot.domain.UserDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Andy on 2016/10/30.
*/
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public UserDomain findUserByUserNo(String userNo) {
return userDao.findByUserNo(userNo);
}
}
7.在Controller.java中添加查找方法如下:
package com.zity.springboot.web;
import com.zity.springboot.domain.UserDomain;
import com.zity.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Andy on 2016/10/26.
*/
@RestController
@RequestMapping(value = "/")
public class Controller {
@Autowired
private UserService userService;
/**
* hello word
* @return
*/
@RequestMapping(value = "/hello")
public String sayHello(){
return "Hello word";
}
/**
* 根据用户编号获取用户信息
* @param userNo
* @return
*/
@RequestMapping(value = "/getUserByNo")
public UserDomain getUserByNo(String userNo){
UserDomain user=null;
try {
user=userService.findUserByUserNo(userNo);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
}
8.在数据库中创建数据库表以及插入初始数据,这里需要说明的是,由于Hibernate具有根据实体类自动创建表的功能,所以我们只需要在application.properties中加入spring.jpa.properties.hibernate.hbm2ddl.auto=create,当创建完成后就将该配置删除,防止数据库数据在重启程序后丢失。
9.启动程序然后在浏览器中输入http://127.0.0.1:8080/getUserByNo?userNo=111111,将看到从数据库中查询出的数据。这样集成jpa的工作就结束了!