文章目录
前言
Mybatis-SpringBoot整合:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html
一、导入依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.cy</groupId>
<artifactId>mybatis_springboot_demo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis_springboot_demo01</name>
<description>Springboot-Mybatis Study</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--springboot整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springboot测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!--springboot整合maven插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、编写配置文件
SpringBoot项目配置文件有两种:
-
application.properties(创建项目默认)
spring.datasource.url=jdbc:mysql://localhost:3306/demo_admin?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root
-
application.yml
spring: datasource: url: jdbc:mysql://localhost:3306/demo_admin?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai username: root password: root
启动项目测试是否能正常启动,如启动失败遇到下述问题,可以参考下面的解决方案
APPLICATION FAILED TO START 异常报错原因及解决方案
二、编写User实体类
User
@Data
@Accessors(chain = true)
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
private String sex;
}
三、编写持久层UserMapper接口
UserMapper
@Mapper // 这里@Mapper注解加不加皆可
public interface UserMapper {
List<User> getAllUser();
}
这里 @Mapper注解 加不加皆可
四、编写UserMapper映射文件
UserMapper.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="cn.cy.mapper.UserMapper">
<select id="getAllUser" resultType="cn.cy.pojo.User">
select * from demo_user
</select>
</mapper>
五、在配置类上添加@MapperScan注解
1. 单独写一个配置类 MybatisConfig
@Configuration
@MapperScan("cn.cy.demo.mapper")
public class MybatisConfig {
}
2. 直接定义在启动类上
@SpringBootApplication
@MapperScan("cn.cy.demo.mapper")
public class MybatisSpringbootDemo01Application {
public static void main(String[] args) {
SpringApplication.run(MybatisSpringbootDemo01Application.class, args);
}
}
六、修改项目配置文件(根据配置文件的类型选择一种即可)
application.properties
mybatis.mapper-locations=classpath:mappers/*.xml
application.yml
mybatis:
mapper-locations: classpath:mappers/*.xml
七、编写UserMapperTests测试类
@SpringBootTest
public class UserMapperTests {
@Autowired
UserMapper userMapper;
@Test
void testGetAllUser() {
List<User> users = userMapper.getAllUser();
System.out.println(users); // [User(id=1, name=zs, age=18, sex=男), User(id=2, name=ls, age=20, sex=男), User(id=3, name=xiaohong, age=18, sex=女)]
}
}
运行测试类中testGetAllUser方法遇到下述问题,可以参考下面的解决方案
org.apache.ibatis.binding.BindingExceptio 异常报错原因及解决方案
八、demo_user数据库表模板
九、实体类属性的数据类型和表字段的数据类型的对应关系
MySQL中的数据类型 | Java中的数据类型 |
---|---|
tinyint / smallint / int | Integer |
bigint | Long |
double | Double |
char / varchar / text | String |
datetime | LocalDateTime |
decimal | BigDecimal |
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/107642.html