我枯了,终于运行出了实验十的第一个部分的内容。。
一、新建项目:
(有些配置有错,后面会给出解决方案)
new->file->project
添加依赖:
由于官网的源速度太慢,替换为老师给的setting.xml文件:
setting.xml:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups />
<proxies />
<servers />
<localRepository>D:/server/maven/repository</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>google-maven-central</id>
<name>Google Maven Central</name>
<url>https://maven-central.storage.googleapis.com
</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库在中国的镜像 -->
<mirror>
<id>maven.net.cn</id>
<name>oneof the central mirrors in china</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>
在references做如下修改:
二、项目内容
整个项目的目录如下:
根据提示导入相应的包。
BookCategory:
package cn.edu.ncu.bootjpademo.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.math.BigInteger;
import java.sql.Timestamp;
@Entity//默认对应的数据库的名字为book_category,实体
public class BookCategory {
@Id//主键
//IDENTITY:主键由数据库自动生成(主要是自动增长型)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger categoryId;
private String categoryName;
private Timestamp createTime;//时间戳
private Timestamp updateTime;
public BigInteger getCategoryId() {
return categoryId;
}
public void setCategoryId(BigInteger categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
public Timestamp getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "BookCategory{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}
BookCategoryDao.java:
package cn.edu.ncu.bootjpademo.dao;
import cn.edu.ncu.bootjpademo.entity.BookCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.math.BigInteger;
@Repository//将dao类声明为spring bean
//<实体,主键类型>
public interface BookCategoryDao extends JpaRepository<BookCategory, BigInteger> {
}
BookCategoryService.java:
package cn.edu.ncu.bootjpademo.service;
import cn.edu.ncu.bootjpademo.dao.BookCategoryDao;
import cn.edu.ncu.bootjpademo.entity.BookCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
//把dao类注入到service实现类中
@Service
public class BookCategoryService {
@Autowired
private BookCategoryDao bookCategoryDao;
public List<BookCategory> findAll(){
return bookCategoryDao.findAll();
}
}
BookCategoryController.java:
package cn.edu.ncu.bootjpademo.controller;
import cn.edu.ncu.bootjpademo.entity.BookCategory;
import cn.edu.ncu.bootjpademo.service.BookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "category")
public class BookCategoryController {
@Autowired
private BookCategoryService service;
@RequestMapping(value = "",method = RequestMethod.GET)
public List<BookCategory> findAll(){
return service.findAll();
}
}
BookCategoryServiceTest.java:
package cn.edu.ncu.bootjpademo.service;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.junit.runner.RunWith;
@RunWith(SpringRunner.class)//直接使用spring容器
@SpringBootTest
class BookCategoryServiceTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private BookCategoryService service;
@Test
public void findAll() {
logger.debug(service.findAll().toString());
}
}
BookJpaDemoApplication.java:
package cn.edu.ncu.bootjpademo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootJpaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(BootJpaDemoApplication.class, args);
}
}
application.yml:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 数据库密码
url: jdbc:mysql://localhost:3306/book?characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true
logging:
level:
cn.edu.ncu.bootjpademo: debug
数据库内容:在book_category表中已添加入数据
三、报错及解决方法
1、运行BootJpaDemoApplication.java文件(启动项目),发现报如下错误:
解决方案:
打开老师给的setting.xml文件,发现localRepository的路径是D盘,可是我没有D盘呀,问题就出现在这里。
查看maven的配置信息,把localRepository的路径修改为本地的路径(路径见下图),然后再导入这个setting.xml文件
2、再次运行 BootJpaDemoApplication.java文件(启动项目),发现报错:
这是一个看不懂的错误,但是注意到spring-context有两个版本,可能是这个原因导致的,故需要删除一个版本。
当删除低版本的spring-context的jar包之后,BootJpaDemoApplication.java能运行起来了,但是依旧会报错:
这很可能是由于重复导入,版本冲突引起的,因为在实验初始时,为了避免缺少某些jar文件,我把上一个实验的全部jar导进来了,却没有意识到重复导入可能会出错。在project-Structure的modules中查看导入的jar包,发现有许多重复的jar包,删除重复jar包,最终只需要额外导入黄框里面的两个包就可以了,其他的包在建项目的时候已经自动导入了。如下图:
四、运行结果
运行BookCategoryServiceTest可以正常查看到日志中的信息:
运行BootJpaDemoApplication,报错:当前的8080已经被占用(如果是第一次运行BootJpaDemoApplication的并不会报这个错误),解决方法如下:
再次运行,成功:
在postman中输入路径,可以查看到数据库中的信息:
啊!还好没放弃!调出来了!!!心情舒畅!Spring Boot还没有学,完全是在硬着头皮做实验!
以后绝不会从事网页的工作!!!(;´༎ຶД༎ຶ`)