自己按照教程创建一个springboot+mybatis项目

自己按照教程创建一个springboot+mybatis项目

参考资料

参考:SpringBoot整合Mybatis完整详细版-海岛拾贝

参考:SpringBoot整合mybatis快速入门-蜻蜓队长家长

 

问题记录

引入依赖的时候什么是核心的:

         <!--web核心依赖-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
 ​
         <!--mysql数据库驱动-->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>
 ​
         <!--mybatis-->
         <dependency>
             <groupId>org.mybatis.spring.boot</groupId>
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>2.1.3</version>
         </dependency>

 

配置文件中的问题:

  • 按照教程创建了两个yml的配置文件【删除原有的application.properties(据说在springboot底层会把application.yml文件解析为application.properties)】:

    • application.yml

      image-20200821115123270

    • application-dev.yml

      image-20200821115057345

     

  • 那么创建的两个文件的关系是啥呢:

    • 在一个项目中,不把所有的环境配置都放在一个文件(application.yml)中,而是分开写,通过application.yml中的 spring.profile.active 属性来调用。

    • 多环境配置的文件名满足: application-{profile}.yml 格式。其中,{profile}对应环境标识。比如:

      • application-dev.yml ---- 开发环境

      • application-test.yml ---- 测试环境

      • application-prod.yml ---- 生产环境

 

  • 配置文件

  •  #spring:
     #  driver-class-name   ?
     #mybatis:
     #  mapper-Locations:      是mapper的xml文件位置
     #  type-alases-package:   为了配置xml文件中resultType返回值的包位置
     #logging:
     #  level:
     #    com:
     #      xd:
     #        jiaoyu:
     #          dao: debug   ?

 

项目结构:

image-20200821135922247

  • Java框架中entity、mapper、service、controller层作用_LiMingRan

    • entity(modo、domain)--实体层--存放实体类,与数据库中的属性值基本一致,实现get、set方法

    • mapper-----------------------dao层---数据库持久化操作,语句针对数据库的操作;mybatis中,其方法与xxx.xml一一映射

      • 使用数据持久化有以下好处:

        1、程序代码重用性强,即使更换数据库,只需要更改配置文件,不必重写程序代码。

        2、业务逻辑代码可读性强,在代码中不会有大量的SQL语言,提高程序的可读性。

        3、持久化技术可以自动优化,以减少对数据库的访问量,提高程序运行效率。

    • service-------------------------业务层--给controller层的类提供接口。自己写的方法的声明,实现在serviceimpl包中

    • controller(web)-------------控制层--业务流程控制:接收前端请求,调用service层接口控制业务流程,并将处理结果返回

  • 启动类文件存放位置:controlle 包的同级目录下

    image-20200821102336452

 

创建数据库最初是在navicat中用SQL语句写吗?

那个发过来的sql文件里面除了sys其他的表是干啥的?

解决报错

无法启动

错误提示(其实在一行,被我断句了):

 org.springframework.beans.factory.
 ​
 UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.
 ​
 BeanCreationException: Error creating bean with name 'UserService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.
 ​
 UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [E:\test\springboot_mybatis\target\classes\com\fan\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.
 ​
 BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.
 ​
 BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [E:\test\springboot_mybatis\target\classes\mapper\UserMapper.xml]'; nested exception is org.apache.ibatis.builder.
     
 BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 47; 文档根元素 "mapper" 必须匹配 DOCTYPE 根 "null"。
 ​

解决参考:解决异常Error creating bean with name 'xxxxxController': Unsatisfied dependency expressed through field

这个面讲的是在Serviceimpl中没有加@Service的后果,但是有一句提到了相关的配置,我记得我创建UserMapper.xml 的时候没有配置,我把另一个项目的配置粘过来,就好了。

 

项目代码

项目结构

image-20200821153514797

pom.xml(自动生成,无改动)

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.3.3.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
     <groupId>com.fan</groupId>
     <artifactId>springboot_mybatis</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>springboot_mybatis</name>
     <description>Demo project for Spring Boot+mybatis</description>
 ​
     <properties>
         <java.version>1.8</java.version>
     </properties>
 ​
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jdbc</artifactId>
         </dependency>
 ​
         <!--web核心依赖-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
 ​
         <!--mybatis-->
         <dependency>
             <groupId>org.mybatis.spring.boot</groupId>
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>2.1.3</version>
         </dependency>
 ​
         <!--mysql数据库驱动-->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>
 ​
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
             <exclusions>
                 <exclusion>
                     <groupId>org.junit.vintage</groupId>
                     <artifactId>junit-vintage-engine</artifactId>
                 </exclusion>
             </exclusions>
         </dependency>
     </dependencies>
 ​
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
 ​
 </project>
 ​

配置文件(application.yml)

 spring:
   profiles:
     active: dev

配置文件(application-dev.yml)

 server:
   port: 8083
 ​
 spring:
   datasource:
     username: root
     password: root
     url: jdbc:mysql://localhost:3306/demo-test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT
     driver-class-name: com.mysql.cj.jdbc.Driver
 ​
 mybatis:
   mapper-Locations: classpath:mapper/*.xml
   type-aliases-Package: com.fan.entity
 ​
 #show SQL
 logging:
   level:
     com:
       fan:
         mapper: debug
 ​

UserController.java

 package com.fan.controller;
 ​
 import com.fan.entity.User;
 import com.fan.service.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 ​
 import java.util.List;
 ​
 @RestController
 @RequestMapping("/user")
 public class UserController {
     @Autowired
     private UserService userService;
 ​
     @RequestMapping("/findAll")
     public List<User> findAll(){
         return userService.findAll();
     }
 }
 ​

User.java

 package com.fan.entity;
 ​
 import org.springframework.context.annotation.EnableLoadTimeWeaving;
 ​
 /**
  * @Author:fxj
  * @Data:2020.08.21
  * @Time: 11:54
  */
 ​
 public class User {
 ​
     private Integer id;                 //编号
     private String userName;            //用户名
     private String passWord;            //密码
     private String roleName;            //角色(管理员、普通成员)
 ​
     public Integer getId() {
         return id;
     }
 ​
     public void setId(Integer id) {
         this.id = id;
     }
 ​
     public String getUserName() {
         return userName;
     }
 ​
     public void setUserName(String userName) {
         this.userName = userName;
     }
 ​
     public String getPassWord() {
         return passWord;
     }
 ​
     public void setPassWord(String passWord) {
         this.passWord = passWord;
     }
 ​
     public String getRealName() {
         return roleName;
     }
 ​
     public void setRealName(String realName) {
         this.roleName = realName;
     }
 ​
     @Override
     public String toString() {
         return "User{" +
                 "id=" + id +
                 ", userName='" + userName + '\'' +
                 ", passWord='" + passWord + '\'' +
                 ", realName='" + roleName + '\'' +
                 '}';
     }
 }

UserMapper.java

package com.fan.mapper;

import com.fan.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    List<User> findAll();
}

UserService.java

package com.fan.mapper;

import com.fan.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    List<User> findAll();
}

UserServiceImpl.java

package com.fan.service.impl;

import com.fan.entity.User;
import com.fan.mapper.UserMapper;
import com.fan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

}

SpringbootMybatisApplication.java

package com.fan;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.fan.mapper") //使用MapperScan批量扫描所有的Mapper接口;
public class SpringbootMybatisApplication {

    public static void main(String[] args)
    {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

UserMapper.xml

package com.fan;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.fan.mapper") //使用MapperScan批量扫描所有的Mapper接口;
public class SpringbootMybatisApplication {

    public static void main(String[] args)
    {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值