springboot与mybatis整合过程。
程序员文章站
2022-03-08 15:41:47
...
1.向pom.xml部署环境,添加运行需要的jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- 连接MySQL数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.书写application.properties
#数据源
spring.datasource.url=jdbc:mysql://localhost:3306/sys
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#加载mybatis配置文件
mybatis.config-locations=classpath:mapper/config/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/GoodMapper.xml
mybatis.type-aliases-package=com.example.springboot_mybatis.domain
#指定配置服务端口
server.port = 9527
3.书写controller类,添加映射URL
@RestController
public class GoodController {
@Autowired
Goodservice goodservice;
@RequestMapping("findAll")
public List<Goods> findAll(){
System.out.println("进入了finALL()");
List<Goods> list = goodservice.findAll();
System.out.println("list:"+list);
return list;
}
}
4.书写service
@Service
public class Goodservice {
//注入GoodMapper
@Autowired
GoodMapper goodMapper;
public List<Goods> findAll() {
System.out.println("执行service");
System.out.println(goodMapper);
List<Goods> findAll = goodMapper.findAll();
return findAll;
}
}
5.书写接口Mapper
@Mapper
public interface GoodMapper {
public List<Goods> findAll();
}
6.书写*Mpper.xml文件,并在放在resource文件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.example.springboot_mybatis.mapper.GoodMapper">
<select id="findAll" resultType="com.example.springboot_mybatis.domain.Goods">
select * from goods
</select>
</mapper>
7.书写mybatis默认配置文件
<?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">
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
8.运行,打开浏览器,访问http://localhost:9527/findAll,就可以看到结果。
上一篇: vue3.0封装axios:
下一篇: 复习Vue11:生命周期探讨
推荐阅读
-
SpringBoot轻松整合MongoDB的全过程记录
-
SpringBoot整合MyBatis获得插入数据后获取主键,返回值总是1
-
SpringBoot项目整合mybatis的方法步骤与实例
-
Spring Boot 整合mybatis 与 swagger2
-
springboot整合H2内存数据库实现单元测试与数据库无关性
-
Spring Boot+Mybatis的整合过程
-
springboot整合H2内存数据库实现单元测试与数据库无关性
-
springboot整合mybatis将sql打印到日志的实例详解
-
SpringBoot框架与MyBatis集成,连接Mysql数据库
-
MyBatis 与 Spring 的完美整合方法