在做了前期的配置之后,我们现在需要链接数据库,这里将以mysql和jpa演示。
第一步,在pom.xml中引入mysql以及jpa的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
第二步,在application.properties中,配置jpa及mysql的链接方式
#通用数据源配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#test为数据库名称
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
# JPA 相关配置
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
#这个地方也最好加上,这样才能自动更新表
spring.jpa.hibernate.ddl-auto=update
第三步,建立实体类,user
package com.example.wx.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
/**
* Created by w on 2019/2/28.
*/
@Data
@NoArgsConstructor
@Entity//这个表示不能少
@Table(name="user")//表名
public class User {
@Id//主键 不能缺
@GeneratedValue(strategy= GenerationType.IDENTITY)//设置自增长
private Long id;
//用户姓名
@Column(length = 50)//字段长度
private String name;
//邮箱
@Column(length = 50)
private String email;
//性别
@Column(length = 2)
private String sex;
//手机
@Column(length = 20)
private String mobile;
//QQ
@Column(length = 20)
private String QQ;
}
第四步,建立仓库层,引入注解,继承JpaRepository
package com.example.wx.repository;
import com.example.wx.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* Created by w on 2019/2/28.
*/
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
第六步,建立业务层,IUserSerivce,及UserServiceImpl,在里面写业务代码
package com.example.wx.service;
import com.example.wx.entity.User;
/**
* Created by w on 2019/2/28.
*/
public interface IUserService {
//新增一个User
void saveUser(User user);
}
实现类
package com.example.wx.service.impl;
import com.example.wx.entity.User;
import com.example.wx.repository.UserRepository;
import com.example.wx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
/**
* Created by w on 2019/2/28.
*/
@Service
public class UserServiceImpl implements IUserService{
@Autowired
UserRepository userRepository;
@Override
@Transactional
public void saveUser(User user) {
userRepository.save(user);
}
}
第7步,调用就完事了
package com.example.wx.controller;
import com.example.wx.common.core.exception.MyException;
import com.example.wx.common.core.ret.ApiResult;
import com.example.wx.entity.User;
import com.example.wx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by w on 2019/2/28.
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
IUserService userService;
@RequestMapping("/demo")
ApiResult demo(@RequestParam String name)
{
ApiResult apiResult = new ApiResult();
apiResult.setData(name);
return apiResult;
}
//异常测试
@RequestMapping("/test")
ApiResult test()
{
throw new MyException("自定义异常测试");
}
//新增一个User
@RequestMapping("/saveUser")
public ApiResult saveUser(@RequestBody User user)
{
userService.saveUser(user);
return new ApiResult();
}
}
接口调用
上一篇,spring boot框架搭建-5 自定义异常以及全局异常捕捉
下一篇,spring boot框架搭建-7 建立数据传输对象dto以及映射ModelMapper