阅读 157

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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文件

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

  

2.3测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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测试类

1
2
3
4
5
6
7
8
@Resource
    private ArticleService articleService;
    @Test
    void addArticle(){
        System.out.print(articleService);
        articleService.addArticle();
 
    }

  

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


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐