简单搭建 Spring Boot Web项目(二)
继上一篇继续写,O(∩_∩)O
一.添加依赖
1)你可以新建项目
在Web的基础上,只需要在配置项中再添加MySQL 和 JPA 的依赖
然后下载就行了,接下来就是导入....等等等,与上一篇相似
具体请看https://blog.****.net/clg_rectchen/article/details/88707588
2)在原来的项目中添加依赖
打开你的pom.xml文件
在你现有的依赖项中添加
MySQL的依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
JPA的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
添加完成以后保存pom文件。
二.application.properties添加配置
1)添加MySQL数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
上述配置替换为你自己的mysql数据库的配置。
2)添加JPA的配置
spring.jpa.database = MYSQL
spring.jpa.show-sql = false
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
三.添加实体
新建一个model包,在model里添加Admin类
实体的具体代码如下:
package com.example.demo.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "demo_admin")
public class Admin implements Serializable{
private static final long serialVersionUID = 7279677413731335383L;
/** id */
private Long id;
/** 用户名 */
private String userName;
/** 密码 */
private String passWord;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
这个时候我们启动spring就会发现demo数据库中有了demo_admin这张表,那么接下来我们就要测试取数据了......
四.添加Service
新增service包,添加一个AdminService
代码如下:
package com.example.demo.service;
public interface AdminService {
}
新增serviceimpl包,添加一个AdminServiceImpl
代码如下:
package com.example.demo.serviceimpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.service.AdminService;
@Transactional
@Service
public class AdminServiceImpl implements AdminService{
}
五.添加Repository
新增repository包,添加AdminRepository
代码如下:
package com.example.demo.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.model.Admin;
@Repository
public interface AdminRepository extends PagingAndSortingRepository<Admin, Long> {
}
添加完上述的几个文件之后项目的结构基本为这样子:
六.添加AdminController
在controller包中添加一个AdminController
代码如下:
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.model.Admin;
import com.example.demo.service.AdminService;
@Controller
public class AdminController {
@Autowired
private AdminService adminService;
@RequestMapping(value="/list",method =RequestMethod.GET)
@ResponseBody
public List<Admin> getList(){
List<Admin> list = adminService.findList();
return list;
}
}
然后在AdminService中添加findList()方法
public interface AdminService {
List<Admin> findList();
}
AdminServiceImpl实现方法
@Transactional
@Service
public class AdminServiceImpl implements AdminService{
@Autowired
private AdminRepository adminRepository;
@Override
public List<Admin> findList() {
Iterable<Admin> findAll = adminRepository.findAll();
return (List<Admin>) findAll;
}
}
启动项目
在demo_admin中手动插入几条数据
在浏览器中输入http://localhost:8080/list获取demo_admin表的数据
成功实现