博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot应用连接数据库MySQL、及一个简单的demo
阅读量:7158 次
发布时间:2019-06-29

本文共 2829 字,大约阅读时间需要 9 分钟。

一、修改pom.xml文件

  在项目的pom.xml文件上增加如下代码,添加依赖文件。

mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-data-jpa

二、设置全局配置文件

  在src/main/resources/application.properties中设置数据源和jpa配置。标红处是自己建的库

spring.datasource.url=jdbc:mysql://123.207.46.41:3306/gwfdemo?useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=159753spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.max-idle=10spring.datasource.max-wait=1000spring.datasource.min-idle=5spring.datasource.initial-size=5server.port=8080server.servlet.session.timeout=10server.tomcat.uri-encoding=UTF-8

三、分层业务逻辑

  POJO:domain层

package com.example.demo.Domain;public class Student {    private Integer id;    private String name;    private Integer age;    private String grade;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getGrade() {        return grade;    }    public void setGrade(String grade) {        this.grade = grade;    }}

  service层:

package com.example.demo.Service;import com.example.demo.Dao.StudentDao;import com.example.demo.Domain.Student;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class StudentService {    @Autowired StudentDao studentDao;    public List
getAll(){ return studentDao.getAll(); }}

  dao层:

package com.example.demo.Dao;import com.example.demo.Domain.Student;import org.apache.ibatis.annotations.Select;import java.util.List;public interface StudentDao {    @Select("select * from student")    public List
getAll();}

  Controller层:

package com.example.demo.controller;import com.example.demo.Domain.Student;import com.example.demo.Service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestControllerpublic class StudentController {    @Autowired    StudentService studentService;    @ResponseBody    @RequestMapping("/student")    public List
getStudents(){ return studentService.getAll(); }}

  然后运行,即可查出数据,很简单的一个demo,但是代码结构大部分如此

转载地址:http://omegl.baihongyu.com/

你可能感兴趣的文章
centos安装epel源
查看>>
想不到的异或操作。。
查看>>
理解UIApplication
查看>>
例子 /maven-service-factory-api
查看>>
iOS运行回路(RunLoop)总结
查看>>
链表crud
查看>>
GitHub Pages上写完简历后报404
查看>>
硬盘的读写原理
查看>>
eclipse svn时忽略target .project .classpath等目录文件
查看>>
告警系统主脚本、主配置文件、监控项脚本
查看>>
CSS层叠样式表之CSS解析机制的优先级及样式覆盖问题探讨
查看>>
angularjs关于controller之间如何通讯
查看>>
nodejs npm 全局安装路径和本地安装路径区别
查看>>
Android---Button
查看>>
MVC介绍
查看>>
JSONArray的应用
查看>>
NFS服务日志分析
查看>>
3.spring整合ActiveMQ
查看>>
收集:Hibernate中常见问题 No row with the given identifier
查看>>
Trie树
查看>>