欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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,就可以看到结果。