Spring Boot 学习笔记(二)
一、整的差点奔溃的spring boot 报错
spring + mybatis 的搭建过程:需要完成的部分包括 controller、service、dao、mapper。
同时要注意,pom.xml 与 application.properties 文件的配置关系。其他就不多说了...
期间出现的问题很多,包括连接池报错,时区报错等等
二、完成一半的配置信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springdemo?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password= mybatis.mapperLocations=classpath:mapping/*.xml mybatis.typeAliasesPackage=com.example.demo.entity spring.jackson.time-zone=GMT+8application.properties
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.1.6.RELEASEversion> <relativePath/> parent> <groupId>com.examplegroupId> <artifactId>SpringDemo-2artifactId> <version>0.0.1-SNAPSHOTversion> <packaging>warpackaging> <name>SpringDemo-2name> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>2.0.1version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-tomcatartifactId> <scope>providedscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>pom.xml
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo.dao") public class SpringDemo2Application { public static void main(String[] args) { SpringApplication.run(SpringDemo2Application.class, args); } }SpringDemo2Application
package com.example.demo.entity; public class Admin { private int id; private String name; private String pass; private String head; private int follow; private int notices; private int blogs; private String phone; public Admin() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public int getFollow() { return follow; } public void setFollow(int follow) { this.follow = follow; } public int getNotices() { return notices; } public void setNotices(int notices) { this.notices = notices; } public int getBlogs() { return blogs; } public void setBlogs(int blogs) { this.blogs = blogs; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + ", head=" + head + ", follow=" + follow + ", notices=" + notices + ", blogs=" + blogs + ", phone=" + phone + "]"; } }entity
package com.example.demo.dao; import java.util.List; import com.example.demo.entity.Admin; public interface AdminDao { int deleteByPrimaryKey(Integer id); int insert(Admin record); int insertSelective(Admin record); Admin selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Admin record); int updateByPrimaryKey(Admin record); ListdaofindAll(); }
package com.example.demo.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.demo.dao.AdminDao; import com.example.demo.entity.Admin; @Service public class AdminService { @Autowired private AdminDao adminDao; public ListservicefindAll(){ return adminDao.findAll(); } public Admin add(Admin admin){ adminDao.insert(admin); return admin; } public Admin getById(Integer id) { return adminDao.selectByPrimaryKey(id); } }
package com.example.demo.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.example.demo.entity.Admin; import com.example.demo.service.AdminService; @RestController @RequestMapping(value= "/admin/") public class AdminController { @Autowired private AdminService adminService; @GetMapping(value = "/list") private ListcontrolleradminnList() { System.out.println(adminService.findAll()); return adminService.findAll(); } @ResponseBody @RequestMapping(value = "/id") public Admin get(){ System.out.println(adminService.getById(5)); return adminService.getById(5); } @ResponseBody @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"}) public Admin add(){ Admin a = new Admin(); a.setName("rose"); a.setPass("123456"); a.setNotices(6); return adminService.add(a); } }
没配置连接池 ,自己做一下记录......
测试项目结构:
三、SSM 的整合教程(提高基础功底)