springcloud 简单集成mybatis 及所遇到问题解决方法

一、pom文件导入主要jar包:


    mysql
    mysql-connector-java
    5.1.21



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.2

二、application.xml配置文件配置:

  datasource:
    url: jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  mapper-locations: classpath:com.test.dao.*.xml //实体类所要映射sql的xml文件
  type-aliases-package: com.test.entity //实体类全路径所在的最后一级目录

三、java代码及xml映射xml文件:

xml文件:





    

    

    
        INSERT INTO USER (name,username,age,balance)VALUES (#{name},#{userName},#{age},#{balance})
    

    
        UPDATE USER SET username = #{userName} WHERE id = #{id}
    

    
        DELETE FROM USER WHERE id = #{id}
    

java实体类:

package com.test.entity;


public class User {
    private Long id;
    private String name;
    private String userName;
    private Integer age;
    private Double balance;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getUserName() {
        return userName;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }
}

java实体对应xml的映射类:

package com.test.dao;

import com.test.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface IUserMapper {
    User getUser(Long id);
    void addUser(User user);
    void modifyUser(User user);
    void removeUser(Long id);
}

代码简单,这里就省略service层,自己写一个测试controller层进行测试。。。

注意:未完成测试之前都是浮云。。。

异常开始:

1、java.sql.SQLException: Access denied for user '***'@'localhost' (using password)

xml配置文件中数据库配置:username不要写成了name,修改成username,继续。。。

2、There is no getter for property named

检查一下配置文件中sql入参是否是对象的(而不是数据库字段)属性字段,继续。。。

3、Maven项目mybatis Invalid bound statement (not found)解决方法

出现这个异常,用如下方式检查,

转载:https://www.cnblogs.com/ewwwe/p/6181872.html

嗯嗯,一般情况下就简单集成完成了,途中遇到了这些问题,如有其他问题,请留言,一起探讨,谢谢!

 

你可能感兴趣的:(spring,cloud)