前言

前面参照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实现UI自动化

    1.   Selenium2 + WebDriver API 2.   unittest单元测试框架 3.   HTMLTestRunner.html测试报告 4.   自动化测试模型介绍 5.    ...

  2. mysqldump备份表中有大字段失败的排错过程

    几天前收到某个业务项目,MySQL数据库逻辑备份mysqldump备份失败的邮件,本是在休假,但本着工作认真负责,7*24小时不间断运维的高尚职业情操,开始了DBA的排错之路(一开始数据库的备份都是成 ...

  3. 基于 Zookeeper 的分布式锁实现

    1. 背景 最近在学习 Zookeeper,在刚开始接触 Zookeeper 的时候,完全不知道 Zookeeper 有什么用.且很多资料都是将 Zookeeper 描述成一个“类 Unix/Linu ...

  4. 使用yum安装不知道到底安装在什么文件夹

    find /* >yum001    #记录之前的文件夹 find /* >yum002    #记录安装完成后的文件夹 diff yum001 yum002 >yum000     ...

  5. 设计模式总结篇系列:组合模式(Composite)

    在探讨Java组合模式之前,先要明白几个概念的区别:继承.组合和聚合. 继承是is-a的关系.组合和聚合有点像,有些书上没有作区分,都称之为has-a,有些书上对其进行了较为严格区分,组合是conta ...

  6. 解读经典《C#高级编程》泛型 页122-127.章4

    前言 本篇继续讲解泛型.上一篇讲解了泛型类的创建.本篇讲解泛型类创建和使用的细节. 泛型类 上篇举了个我产品中用到的例子,本篇的功能可以对照着此案例进行理解. /// <summary> ...

  7. Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现

    自Spring Cloud Alibaba发布第一个Release以来,就备受国内开发者的高度关注.虽然Spring Cloud Alibaba还没能纳入Spring Cloud的主版本管理中,但是凭 ...

  8. EF 的 CURD 操作

    EF 的 CURD 操作 这里采用了数据库 Northwind,下载地址:https://northwinddatabase.codeplex.com/ 增 /// <summary> / ...

  9. Java开发笔记(十九)规律变化的for循环

    前面介绍while循环时,有个名叫year的整型变量频繁出现,并且它是控制循环进出的关键要素.不管哪一种while写法,都存在三处与year有关的操作,分别是“year = 0”.“year<l ...

  10. Java开发笔记(三十四)字符串的赋值及类型转换

    不管是基本的char字符型,还是包装字符类型Character,它们的每个变量只能存放一个字符,无法满足对一串字符的加工.为了能够直接操作一连串的字符,Java设计了专门的字符串类型String,该类 ...