添加依赖 mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
在配置文件application.properties中配置数据源信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:\
//localhost:3306/day01?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=root
编写pojo
package com.offcn.pojo;
public class Muser {
private Integer uid;
private String uname;
private Integer pwd;
public Muser() {
}
public Muser(Integer uid, String uname, Integer pwd) {
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Integer getPwd() {
return pwd;
}
public void setPwd(Integer pwd) {
this.pwd = pwd;
}
}
mapper接口
package com.offcn.pojo;
public class Muser {
private Integer uid;
private String uname;
private Integer pwd;
public Muser() {
}
public Muser(Integer uid, String uname, Integer pwd) {
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Integer getPwd() {
return pwd;
}
public void setPwd(Integer pwd) {
this.pwd = pwd;
}
}
mapper映射文件
<?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.offcn.mapper.MuserMapper">
<select id="getUserList" resultType="com.offcn.pojo.Muser">
select * from user
</select>
</mapper>
手动配置mybatis的包扫描 在主启动类添加@MapperScan(basePackages = “com.offcn.mapper”),com.offcn.mapper为映射的mapper包的路径
package com.offcn;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.offcn.mapper")
public class Springbootdemo2Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo2Application.class, args);
}
}
若mapper与mapper映射文件在同一目录下
则会报错
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.offcn.mapper.UserMapper.getUserList
需要在pom.xml文件中配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
或把mapper映射文件放在resources中与mapper同样的目录结构下