一、pom文件导入主要jar包:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
二、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文件:
<?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" >
<mapper namespace="com.test.dao.IUserMapper">
<resultMap id="User" type="com.test.entity.User"></resultMap>
<select id="getUser" resultType="User" parameterType="java.lang.Long">
SELECT * FROM USER WHERE id = #{id}
</select>
<insert id="addUser" parameterType="User">
INSERT INTO USER (name,username,age,balance)VALUES (#{name},#{userName},#{age},#{balance})
</insert>
<update id="modifyUser" parameterType="User">
UPDATE USER SET username = #{userName} WHERE id = #{id}
</update>
<delete id="removeUser" parameterType="java.lang.Long">
DELETE FROM USER WHERE id = #{id}
</delete>
</mapper>
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
如果检查都没问题,还是有这个异常,请看看这样一个配置:
mapper-locations: classpath:com.test.dao.*.xml
把它改为:
mapper-locations: classpath:com/zhxf/dao/*.xml
嗯嗯,一般情况下就简单集成完成了,途中遇到了这些问题,如有其他问题,请留言,一起探讨,谢谢!
注意:springboot 集成 mybatis时候就不用mybatis-plux包,会发生冲突的;