社区版idea搭建SpringBoot+Mybatis WEB工程,附源码 - Go语言中文社区

社区版idea搭建SpringBoot+Mybatis WEB工程,附源码


软件环境:jdk 1.8.0_171

                MySql安装版 8.0.11

                idea Community Edition

搭建步骤

  • 创建maven web工程
  • 创建maven web工程


  • GroupId和ArtifactId介绍!!GroupId应使用com.chai和包结构对应

出自:https://blog.csdn.net/snowin1994/article/details/53024871

  • 添加archetypeCatalog=internal,加快maven生成结构

  • 创建完成的目录结构

  • 添加相关目录,并设置java、resources根目录

  • 创建完成的目录

SampleController放在其他代码包的外层目录下,如com.chai.testProj

以下为具体代码:

application.properties:

#设置Tomcat端口,默认8080
server.port=8080
#设置项目ContextPath
server.context-path=/
#设置Tomcat编码
server.tomcat.uri-encoding=UTF-8
#设置视图解析器路径
spring.mvc.view.prefix=/WEB-INF/views/
#设置视图解析器后缀
spring.mvc.view.suffix=.jsp

#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=000000
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#配置模型路径
mybatis.type-aliases-package=com.chai.testProj.model

SampleController:

package com.chai.testProj;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.chai.testProj.mapper")
@SpringBootApplication
public class SampleController {

    public static void main(String[] args) {
        SpringApplication.run(SampleController.class,args);
    }

}

model层:

package com.chai.testProj.model;

/**
 * 教师类
 */
public class Teacher {
    private int id;
    private String name;

    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;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + ''' +
                '}';
    }
}

mapper:

package com.chai.testProj.mapper;


import com.chai.testProj.model.Teacher;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TeacherMapper {
    @Select("SELECT * FROM Teacher WHERE id = #{id}")
    Teacher selectTeacher(int id);

    @Select("SELECT * FROM TEACHER WHERE 1=1")
    List<Teacher> selectAll();
}

service:

package com.chai.testProj.service;

import com.chai.testProj.model.Teacher;

import java.util.List;

public interface TeacherService {
    Teacher selectTeacher(int id);
    List<Teacher> selectAll();
}

package com.chai.testProj.service.impl;

import com.chai.testProj.mapper.TeacherMapper;
import com.chai.testProj.model.Teacher;
import com.chai.testProj.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public Teacher selectTeacher(int id) {

        return teacherMapper.selectTeacher(id);
    }

    @Override
    public List<Teacher> selectAll() {

        return teacherMapper.selectAll();
    }
}

controller:

package com.chai.testProj.controller;

import com.chai.testProj.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/showTeacher/{id}")
    public String selectTeacher(@PathVariable int id){
        return teacherService.selectTeacher(id).toString();
    }

    @RequestMapping("")
    public String testController(){
        return "进入showTeacher";
    }

    @RequestMapping("/selectAll")
    public String selectAll(){
        return teacherService.selectAll().toString();
    }

}

源码:https://gitee.com/healthyJ/testProj.git

遇到问题:

1.异常信息:java.math.BigInteger cannot be cast to java.lang.Long

解决方案:更换mysql驱动版本为5.1.46,之前好像是5.1.12

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.46</version>
    </dependency>

2.警告日志:Fri Dec 11 18:28:21 CST 2015 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

解决方法:在mysql连接后加上useSSL=false

jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=false

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_24271463/article/details/80181531
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-08-26 22:36:20
  • 阅读 ( 2636 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢