前言

前面参照SpringBoot官网,自动生成了简单项目点击打开链接

配置数据库和代码遇到的问题

问题1:cannot load driver class :com.mysql.jdbc.Driver不能加载mysql

原因:没有添加依赖

解决:pom.xml添加依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
    问题2:Consider defining a bean of type 'com.xx.dao.XxDao' in your configuration.注入UserDao失败

原因:UserDao没有添加注解

解决:在接口UserDao外层加上注解:@Mapper

问题3:controller中注入service失败

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.boot.service.DemoService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    原因:application.java文件默认扫描相同包名下的service,dao。

解决:application.java文件添加注解:@ComponentScan(basePackages = "com.xxx")

配置Mysql数据库

在pom.xml添加依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
在application.properties添加

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/girls
spring.datasource.username=root
spring.datasource.password=chendashan
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

server.port=8080
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8

mybatis.configLocations= classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
    建立库表省略,文章末尾附带

mapper文件

操作数据库,靠它完成。

<?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">
<!-- namespace用于绑定Dao接口 -->
<mapper namespace="com.housekeeper.dao.UserDao">
<!-- 用用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.housekeeper.model.User" >
<!-- column代表数据库列名,property代表实体类属性名 -->
<result column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
<result column="user_password" property="userPassword"/>
</resultMap>
<!-- 查询名字记录sql -->
<select id="selectUserByUserName" parameterType="String" resultMap="BaseResultMap">
SELECT * FROM girls_info WHERE user_name = #{userName}
</select>
</mapper>
    综上得知,UserDao通过映射文件mapper,执行了sql语句,返回了实体类User

UserDao接口

@Mapper
public interface UserDao {
/**
* 根据user_name查询数据库
* (映射执行mapper文件中的sql语句selectUserByUserName)
* @param userName 名字
* @return User
*/
public User selectUserByUserName(String userName);
}
User实体类

public class User {
private String userName;
private String userPassword;

public String getUserName() {
return userName;
}

public void setName(String userName) {
this.userName = userName;
}

public String getUserPassword() {
return userPassword;
}

public void setPassword(String userPassword) {
this.userPassword = userPassword;
}

}
逻辑结构

逻辑层在controller里处理,已知,执行Userdao的接口方法,即可操作数据库。为了更好处理逻辑分层,加入Service层,调用UserDao。在Service实现层,注入UserDao即可调用其方法。

@Service
public class UserServiceImp implements UserService {

@Autowired
private UserDao userDao;//注入UserDao

@Override
public User selectUserByName(String userName) {
return userDao.selectUserByUserName(userName);
}

}
public interface UserService {
/**
* 通过姓名查找User
* @param userName
* @return
*/
User selectUserByName(String useName);
}
controller

最后,controller层对外提供接口,返回查询数据结果

@Controller
public class UserController {

@Autowired
private UserService userService;//注入Service

@ResponseBody
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Map<String, Object> login(@RequestParam(value = "userName", required = true) String userName,
@RequestParam(value = "userPassword", required = true) String userPassword) {
Map<String,Object> result = new HashMap<String, Object>();
User user = null;
String retCode = "";
String retMsg = "";
if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(userPassword)){
retCode = "01";
retMsg = "用户名和密码不能为空";
}else{
user = userService.selectUserByName(userName);
if(null == user){
retCode = "01";
retMsg = "用户不存在";
}else{
if(userPassword.equals(user.getUserPassword())){
retCode = "00";
retMsg = "登录成功";
}else{
retCode = "01";
retMsg = "密码有误";
}
}
}
result.put(SystemConst.retCode, retCode);
result.put(SystemConst.retMsg, retMsg);
return result;
}

}
girls.sql文件

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `girls_info`
-- ----------------------------
DROP TABLE IF EXISTS `girls_info`;
CREATE TABLE `girls_info` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) NOT NULL,
`user_password` varchar(10) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of girls_info
-- ----------------------------
INSERT INTO `girls_info` VALUES ('1', '张帆', '123456');
INSERT INTO `girls_info` VALUES ('2', '李北', '123456');
INSERT INTO `girls_info` VALUES ('3', '陈珊珊', '123456');
INSERT INTO `girls_info` VALUES ('4', '王国立', '123456');
INSERT INTO `girls_info` VALUES ('5', '张三', '123456');
INSERT INTO `girls_info` VALUES ('6', '李四', '123456');
INSERT INTO `girls_info` VALUES ('7', 'Biligle', '123456');
下载地址:https://download.csdn.net/download/qq_29266921/10457479
---------------------
作者:Biligle
来源:CSDN
原文:https://blog.csdn.net/qq_29266921/article/details/80513146
版权声明:本文为博主原创文章,转载请附上博文链接!

【入门】Spring-Boot项目配置Mysql数据库的更多相关文章

  1. (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】

    大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...

  2. Spring Boot MyBatis配置多种数据库

    mybatis-config.xml是支持配置多种数据库的,本文将介绍在Spring Boot中使用配置类来配置. 1. 配置application.yml # mybatis配置 mybatis: ...

  3. Spring boot +mybatis 连接mysql数据库,获取JDBC失败,服务器时区价值”Oйu±e×¼e±¼的识别或代表多个时区

    报出的错误 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connec ...

  4. Spring Boot项目配置RabbitMQ集群

    //具体参看了配置的源码 org.springframework.boot.autoconfigure.amqp.RabbitProperties //RabbitMQ单机 spring:   rab ...

  5. Spring Boot 项目配置的使用方法

    第一种写法resources目录下的application.properties文件 第二种写法resources目录下的application.yml文件 在项目中获取配置项: 分组配置:  (配置 ...

  6. spring boot项目配置RestTemplate超时时长

    配置类: @Configuration public class FeignConfiguration { @Bean(name="remoteRestTemplate") pub ...

  7. spring boot项目配置跨域

    1.在项目启动入口类实现 WebMvcConfigurer 接口: @SpringBootApplication public class Application implements WebMvcC ...

  8. Spring boot jpa 设定MySQL数据库的自增ID主键值

    内容简介 本文主要介绍在使用jpa向数据库添加数据时,如果表中主键为自增ID,对应实体类的设定方法. 实现步骤 只需要在自增主键上添加@GeneratedValue注解就可以实现自增,如下图: 关键代 ...

  9. spring boot 项目配置字符编码

随机推荐

  1. Python爬虫入门教程 34-100 掘金网全站用户爬虫 scrapy

    爬前叨叨 已经编写了33篇爬虫文章了,如果你按着一个个的实现,你的爬虫技术已经入门,从今天开始慢慢的就要写一些有分析价值的数据了,今天我选了一个<掘金网>,我们去爬取一下他的全站用户数据. ...

  2. 前端笔记之JavaScript(十)深入JavaScript节点&DOM&事件

    一.DOM JavaScript语言核心.变量的定义.变量的类型.运算符.表达式.函数.if语句.for循环.算法等等.这些东西都属于语言核心,下次继续学习语言核心就是面向对象了.JavaScript ...

  3. Docker折腾手记-linux下安装

    Linux下的安装方法 博主用的是centos7,其它也是大同小异 我根据的是官网的教程进行的操作,地址是 https://docs.docker.com/engine/installation/li ...

  4. Asp.Net SignalR 集线器不使用代理的实现

    不使用生成代理JS的实现 可能有同学会觉得使用集线器很麻烦,要么引入虚拟目录,要么在生成期间生成js文件,再引入js文件进行开发.难道就没有比较清爽的方式吗?这个当然是有的,先不要(。・∀・)ノ゙嗨皮 ...

  5. 使用Flume消费Kafka数据到HDFS

    1.概述 对于数据的转发,Kafka是一个不错的选择.Kafka能够装载数据到消息队列,然后等待其他业务场景去消费这些数据,Kafka的应用接口API非常的丰富,支持各种存储介质,例如HDFS.HBa ...

  6. Linux 中Ctrl + s 的作用

    在Linux下使用vim编辑程序时,常常会习惯性的按下Ctrl + s保存文件内容.殊不知,这一按不紧,整个终端再也不响应了. 事实上Ctrl + s在终端下是有特殊用途的,那就是暂停该终端,这个功能 ...

  7. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

  8. Perl获取主机名、用户、组、网络信息

    获取主机名.用户.组.网络信息相关函数 首先是获取主机名的方式,Perl提供了Sys::Hostname模块,可以查询当前的主机名: use Sys::Hostname; print hostname ...

  9. 第37章 资源所有者密码验证(Resource Owner Password Validation) - Identity Server 4 中文文档(v1.0.0)

    如果要使用OAuth 2.0资源所有者密码凭据授权(aka password),则需要实现并注册IResourceOwnerPasswordValidator接口: public interface ...

  10. PhpStorm 安装ApiDebugger

    ApiDebugger,是一个开源的接口调试IntelliJ IDEA插件,具有与IDEA一致的界面,无需切换程序即可完成网络API请求,让你的code更加沉浸式. 安装 File->Setti ...