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+8
application.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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringDemo-2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>SpringDemo-2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </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);
    
    List<Admin> findAll();
}
dao
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 List<Admin> findAll(){
        return adminDao.findAll();
    }
    
    public Admin add(Admin admin){
        adminDao.insert(admin);
        return admin;
    }
    public Admin getById(Integer id) {
        return adminDao.selectByPrimaryKey(id);
    }
}
service
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 List<Admin> adminnList() {
        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);
    }
}
controller

 

没配置连接池 ,自己做一下记录......

 测试项目结构:

 

三、SSM 的整合教程(提高基础功底)

链接:https://www.cnblogs.com/zyw-205520/p/4771253.html

posted @ 2019-07-11 17:34  MoonTwilight  阅读(262)  评论(0编辑  收藏  举报