首页 > 编程语言 > 详细

Idea---SpringBoot整合Mybatis问题整理

时间:2021-04-20 20:43:48      阅读:25      评论:0      收藏:0      [点我收藏+]

1.数据库连接失败

错误提示关键词:"Server returns invalid timezone","setTimeZone"

我的解决过程:

(1)登录mysql:mysql -uroot -p;然后输入密码

(2)查看mysql中设置的时区值:show variables like ‘%time_zone%‘; (要加分号),默认System

(3)设置时区值:set global time_zone=‘+8:00‘;  (加分号)

技术分享图片

(4)编辑环境变量:变量值 MYSQL_HOME,变量值 C:\Program Files\MySQL\MySQL Server 5.7

技术分享图片

 

 

(5)在path中新建变量%MYSQL_HOME%\bin

技术分享图片

 

 

2.mapper自动注入失败

2.1错误提示关键词

(1)Failed to load ApplicationContext

(2)No qualifying bean of type ‘com.example.mapper.ArticleMapper‘ available

2.2解决过程:在application类中加入注解@MapperScan("com.example.mapper");

(1)检查Mapper接口的@Repository(实践之后发现它不是原因)@Mapper注解

(2)检查Mapper.xml文件中的类名,列明,对象属性名是否有错写或漏写(namespace,resultMap,type,paramType之类的hhh

<?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为Mapper接口的类路径-->
<mapper namespace="com.example.mapper.ArticleMapper" >
    <!-- 1、对应“public Article findArticleById(Integer id)” -->
    <select id="findArticleById" resultMap="articleWithComments">
        SELECT a.*,c.id c_id,c.content c_content,c.author,c.a_id c_aid
        FROM t_article a
                 LEFT JOIN t_comment c
                           ON a.id=c.a_id
        WHERE a.id=#{id}
    </select>
    <!--结果映射集-->
    <resultMap id="articleWithComments" type="Article">
        <id property="id" column="id" />
        <result property="title" column="title" />
        <result property="content" column="content" />
        <collection property="commentList" ofType="Comment">
            <id property="id" column="c_id" />
            <result property="content" column="c_content" />
            <result property="author" column="author" />
            <result property="aId" column="c_aid" />
        </collection>
    </resultMap>
    <!-- 3、对应“public int insertArticle(Article article)” -->
    <insert id="insertArticle" parameterType="Article">
        insert into t_article(title,content)
        values(#{title},#{content})
    </insert>
    <!-- 4、对应“public int updateArticle(Article article)” -->
    <update id="updateArticle" parameterType="Article" >
        UPDATE t_article
        <set>
            <if test="title !=null and title !=‘‘">
                title=#{title},
            </if>
            <if test="content !=null and content !=‘‘">
                content=#{content}
            </if>
        </set>
        WHERE id=#{id}
    </update>
</mapper>

  

(3)检查全局配置文件是否加载Mapper文件

#配置MyBatis的映射文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
#配置映射文件中指定的实体类别名路径
mybatis.type-aliases-package=com.example.domain

  

2.3测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Chapter05Application.class)
class Chapter05ApplicationTest {

    @Resource
    private ArticleMapper articleMapper;

    @Test
    void insertArticle() {
        System.out.print(articleMapper);
        Article article=articleMapper.findArticleById(1);
        System.out.print(article);

    }
}

  

 

3.servies自动注入失败

3.1错误提示关键词:

(1)Could not autowire. No beans of ‘ArticleService‘ type found. 

(2)Error creating bean with name ‘com.example.chapter05.Chapter05ApplicationTest‘

(3) No qualifying bean of type ‘com.example.services

3.2我的解决过程:在application类中加入注解@MapperScan("com.example.mapper")(保证Mapper成功注入才能继续解决Service问题)@ComponentScan("com.example.services");

3.3排错试错的暴躁之旅QAQ

(1)检查Mapper接口的@Repository(实践之后发现它不是原因)@Mapper注解

(2)检查service实现类中的@Service注解

(3)检查Mapper.xml文件中的类名,列明,对象属性名是否有错写或漏写

(4)检查全局配置文件是否加载Mapper文件

(5)mapper是否注入成功

(6)Test类中@RunWith(SpringRunner.class),@SpringBootTest(classes = Chapter05Application.class)

3.4测试类

@Resource
    private ArticleService articleService;
    @Test
    void addArticle(){
        System.out.print(articleService);
        articleService.addArticle();

    }

  

哦哦!还有pom.xml文件该有都有,不该有的都没有

 

Idea---SpringBoot整合Mybatis问题整理

原文:https://www.cnblogs.com/Rong-Xiu/p/14681949.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!