技术标签: spring boot java MySQL SpringBoot
如何创建详见:IDEA 创建 SpringBoot 项目
以前开发Web项目我们都知道要想把数据添加到数据库,不仅必须要数据库的驱动程序,还要有各种各样的配置文件,像java Bean配置,数据源配置,对象和数据库字段的映射配置等等。使用SpringBoot开发,我们只需要加入依赖文件就可以了,SpringBoot已经都帮我配置好了
。配置如下图所示:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
在application.properties中配置数据库连接的相关信息:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:18103/db_test?characterEncoding=GBK
spring.datasource.username=root
spring.datasource.password=root
编程是利用面向对象的思想把自然界中的事物抽象成模型,利用模型来解决实际中的问题。如下图:
package com.springboottest.bean;
public class StudentBean {
private int id;
private String name;
public StudentBean() {
}
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;
}
}
注:这里的字段名称与数据库表字段名称一致。
这个类主要是和数据进行交互联系的,需要配置好实体类和数据库字段的映射关系。由于SpringBoot已经做了大量的工作,我们只需要做好相关注解就可以使用了。如下图所示:
package com.springboottest.sql.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface StudentMapper {
@Select("select * from tb_student where name=#{name}")
StudentBean getStudentInfoByName(String name);
}
@Mapper
表明该类是一个Mapper接口,使用@Select
、@Insert
等注解我们可以直接在类中书写sql语句来实现我们的目的。
我们在接口类里定义要实现的业务功能接口,在它的实现类里实现接口。接口类如下图:
package com.springboottest.sql.service;
import com.springboottest.bean.StudentBean;
public interface StudentService {
StudentBean getStudentInfoByName(String name);
}
实现类如下图:
package com.springboottest.sql.service;
import com.springboottest.bean.StudentBean;
import com.springboottest.sql.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentMapper studentMapper;
@Override
@Transactional
public StudentBean getStudentInfoByName(String name) {
return studentMapper.getStudentInfoByName(name);
}
}
@Service
注解表明它是一个服务类Bean,可以被SpringBoot识别使用,相当于以前在xml里配置的bean。
Web项目的请求经过映射找到控制器类里对应的方法,然后再实现完业务返回响应信息。如下图:
package com.springboottest.controller;
import com.springboottest.bean.StudentBean;
import com.springboottest.sql.MySQLProcessor;
import com.springboottest.sql.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/mysql")
public class SqlController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/student")
public String studentSelect(@RequestParam String name){
StudentBean bean = studentService.getStudentInfoByName(name);
if(bean != null){
return "Name = " + bean.getName();
} else {
return "null";
}
}
}
请求地址:http://localhost:8991/mysql/student?name=tom
文章浏览阅读1.2w次,点赞11次,收藏94次。cfg参数net层[net]batch=96 # 每次iteration训练的时候,输入的图片数量subdivisions=48 # 将每一次的batch数量,分成subdivision对应数字的份数,一份一份的跑完后,在一起打包算作完成一次iterationwidth=512 # width=height,大小为32的倍数momentum=0.9 # 动量,影响梯度下降到最优的速度,一般默认0.9decay=0.0005 # 权重衰减正则系数,防止过拟合angle=0 # 旋转角度,生成更_yolov4参数设置
文章浏览阅读2.5k次。参考 https://forum.image.sc/t/opening-large-tif/4133利用Fiji带有的Bio-Formats 插件导出Tiff格式文件_imagej 4gb
文章浏览阅读8.7k次。data = pd.DataFrame({"A": [True, False, True], "B": [1.1, 2.2, 3.33], "C": ["c1", "c2", "c3"] })print("data : \n", data.
文章浏览阅读1.9k次。gzx_dropdown_menu是一个Flutter自定义功能强大的轻量级下拉筛选菜单Package,它支持iOS和Android。_gzx_dropdown_menu
文章浏览阅读289次。1、安全扫描MySQL5.7安全漏掉较多,要求将数据库升级到指定的8.0版本2、MySQL已经存有大概6个库的正在跑业务3、时间要求紧迫,需要尽快处理4、5.7用的是物理机yum安装。_docker环境mysql5.7升级mysql8.0
文章浏览阅读151次。paip.php的调试--attilax总结php的调试可用PDT与XDEBUGGER,或者与zend debugger来。。如果是php WEB项目,只能进行远程调试,XDEBUGGER/zend debugge 加载起来后,把PHP的信息截获,然后连接PDT的9000/10000端口,把内部信息发往ECLIPSE PDT了。。---------1.使用xdebugger--..._dllhopst
文章浏览阅读224次。24个节气之美,每一个都如诗如画!提交我的留言加载中已留言 24个节气之美,每一个都如诗如画! 【立春】河南白马寺春是希望,念诵梵音祈愿新年平安快乐【雨水】苏州山塘街春风遍吹送来湿润的空气,和氤氲雾霭【惊蛰】江西婺源春雷初鸣,万象更新明媚的春光很多情【春分】浙江乌镇万物复苏的气息让人莫名的,脸红心跳【清明..._二十四节气如诗如画
文章浏览阅读376次。linux系统及编程基础习题答案 第 1 章 Linux 基础及安装 1. 什么是 Linux ? Linux 是一款优秀的计算机操作系统,支持多用户、多进程、多线程,实时性好,功能强大且稳定。 同时,它又具有良好的兼容性和可移植性,被广泛应用于各种计算机平台上。作为 Internet 的产物, Linux 操作系统由全世界的许多计算机爱好者共同合作开发,是一个自由的操作系统。 2. Linux ..._linux编程基础黑马程序员课后答案
文章浏览阅读1.7w次,点赞29次,收藏174次。图像1、模拟图像模拟图像,又称连续图像,是指在二维坐标系中连续变化的图像,即图像的像点是无限稠密的,同时具有灰度值(即图像从暗到亮的变化值)。2、数字图像数字图像,又称数码图像或数位图像,是二维图像用有限数字数值像素的表示。数字图像是由模拟图像数字化得到的、以像素为基本元素的、可以用数字计算机或数字电路存储和处理的图像。通常的二维数字图像是一个矩阵,可以用一个二维数组 f(x,y) 来表示,其中 x,y 是二维空间中的某坐标系的坐标,f(x,y) 表示图像在该点处的灰度值等性质。3、颜_图像处理理论基础
文章浏览阅读217次。该楼层疑似违规已被系统折叠隐藏此楼查看此楼首先先看两个函数函数名:findfirst,findnext功能:搜索磁盘目录;取得下一个匹配的findfirst模式的文件用法:intfindfirst(char*pathname,structffblk*ffblk,intattrib);intfindnext(structffblk*ffblk);程序例:/*..._dos 遍历 目录下 所有文件
文章浏览阅读2.1k次,点赞11次,收藏23次。前言下周即将展开一个http接口测试的需求,刚刚完成的java类接口测试工作中,由于之前犯懒,没有提前搭建好自动化回归测试框架,以至于后期rd每修改一个bug,经常导致之前没有问题的case又产生了bug,所以需要一遍遍回归case,过程一直手工去执行,苦不堪言。所以,对于即将开始的http接口测试需求,立马花了两天时间搭建了一个http接口自动化测试框架用于测试后期回归测试,实在是被大量的重复手工执行搞怕了。基础框架选择最方便的方法就是用python直接写代码,代码和测试数据分离,测试数据放在_python robotframework
文章浏览阅读364次。有关dubbo的基础、架构等介绍请参考之前博客:Dubbo背景及架构简介XML版(官方推荐)示例请参考:Dubbo入门示例XML版(官方推荐)注解版本示例请参考:Dubbo入门示例注解版1. 创建暴露服务模块(dubbo-demo-api)本模块下没有实际的业务逻辑,主要是定义提供者和消费者公用服务接口/** * 需要暴露出去的服务 * */public interface ..._原生api实现dubbo调用