用navicat连接Mysql
1.点击连接,选择Mysql
2.输入连接名称,密码,安装Mysql时输入的密码,本人默认123456,好记
3.点击测试连接
出现上面这种情况是Mysql服务没有开启
解决方案:
任务管理器 ------服务------找到mysql服务----右键开始
启动服务之后,打开我们的navicat,再次测试连接,点击确定
打开本地,新建数据库test1,新建一张表
创建表语句:
/*
Navicat Premium Data Transfer
Source Server : 本地
Source Server Type : MySQL
Source Server Version : 50525
Source Host : localhost:3306
Source Schema : test1
Target Server Type : MySQL
Target Server Version : 50525
File Encoding : 65001
Date: 24/04/2022 10:49:21
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for people
-- ----------------------------
DROP TABLE IF EXISTS `people`;
CREATE TABLE `people` (
`id` bigint(20) NOT NULL COMMENT 'id',
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`age` int(20) NULL DEFAULT NULL COMMENT '年龄',
`address` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of people
-- ----------------------------
INSERT INTO `people` VALUES (1, '张三', 20, '上海');
INSERT INTO `people` VALUES (2, '李四', 25, '北京');
INSERT INTO `people` VALUES (3, '王五', 30, '重庆');
SET FOREIGN_KEY_CHECKS = 1;
数据库就准好了,接下来创建项目,连接数据库获取数据
创建Java项目
引入依赖:
<dependencies>
<!-- springmvc的场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.5</version>
</dependency>
<!-- 添加lombok的依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!-- MySQL驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- Mybatis的场景启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- 单元测试场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.5</version>
</dependency>
</dependencies>
application.yml配置
server:
#端口号
port: 9527
spring:
#数据库配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
# MyBatis配置
mybatis:
mapper-locations: com/thk/mapper/*Mapper.xml
启动类:
@SpringBootApplication
public class app {
public static void main(String[] args) {
SpringApplication.run(app.class,args);
System.out.println("启动成功!!!");
}
}
实体类:
package com.thk.domain;
public class People {
/**
* id
*/
private Long id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 地址
*/
private String address;
public Long getId() {
return id;
}
public void setId(Long 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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
Controller:
@RestController
public class PeopleController {
@Autowired
private IPeopleService peopleService;
/**
* 查询全部用户
* @param people
* @return
*/
@GetMapping("/selectAll")
public List<People> selectAll( People people) {
return peopleService.selectAll(people);
}
}
service接口:
public interface IPeopleService {
/**
* 查询全部用户
* @param people
* @return
*/
List<People> selectAll(People people);
}
service实现类:
@Service
public class PeopleImpl implements IPeopleService {
@Autowired
private PeopleMapper peopleMapper;
/**
* 查询全部用户
* @param people
* @return
*/
public List<People> selectAll(People people) {
return peopleMapper.selectAll(people);
}
}
mapper接口:
@Mapper
public interface PeopleMapper {
/**
* 查询全部用户
* @param people
* @return
*/
List<People> selectAll(People people);
}
mapperxml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.thk.mapper.PeopleMapper">
<select id="selectAll" resultType="com.thk.domain.People">
SELECT * FROM people
</select>
</mapper>
启动项目:
测试:
浏览器输入:http://localhost:9527/selectAll
浏览器数据:
数据库数据:
浏览器显示的数据就是从数据库当中查出来的数据,项目已经成功连接到数据库了
打印运行日志:
浏览器访问成功,但是没有输出运行的日志,看不到执行的Sql语句,就很不方便
我们在application.yml配置一下就好了
# 日志配置
logging:
level:
com: debug
org.springframework: warn
重启项目,再次运行
这样就成功完成java对数据库的配置连接了。
标签:return,name,people,数据库,id,MySql,Java,public,String From: https://blog.51cto.com/u_16281588/8676073