• springboot整合mybatis-高效篇


    1、导入依赖

    <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>

    2、在配置文件中配置数据库连接信息

    #DB Configation
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/users
    spring.datasource.username=root
    spring.datasource.password=root
    # JPAConfiguration
    spring.jpa.database=MySQL
    spring.jpa.show-sql=true
    spring.jpa.generate-ddl=true

    或者:在yml配置文件中格式如下(仅mybatis)

    spring:
      datasource:
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/users
        username: root
        password: root

    3、创建数据库对应实体类

    在这使用了lombook插件,省略了set,get等方法

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public class Muser implements Serializable {
        private int uid;
        private String uname;
        private String pwd;
    }
    4、写对应操作的mapper接口以及sql映射文件
    public interface UserMapper  {
        List<Muser> getAll();
    }
    <?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.ghh.staticdemo.mapper.UserMapper">
        <select id="getAll" resultType="com.ghh.staticdemo.pojo.Muser">
        select * from users
      </select>
    </mapper>

    注意更改namespace以及selcet的id属性

    5、在启动类上加注解

    @MapperScan("需要扫描的包"),相当于ssm框架中spring中的两个配置文件,SqlSessionfactory和mapperScannerConfigure两个bean对象
    @SpringBootApplication
    @MapperScan("com.ghh.staticdemo.mapper") //配置需要扫描的mapper接口所在包下
    public class StaticdemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(StaticdemoApplication.class, args);
        }
    }
    6、在controller层注入mapper接口,获取其代理类对象
    @Resource
        private UserMapper userMapper;
    
    @GetMapping("findAll")
    public ModelAndView getAll(){
            ModelAndView mv = new ModelAndView("user");
            List<Muser> list =  userMapper.getAll();
            mv.addObject("userList",list);
            return mv;
        }  

    至此配置完成

    但我们在启动项目去访问时,会报找不到映射文件

    1、需要我们在pom文件中配置一下代码避免打包时java目录下的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>

    2、或者在resources文件夹下创建与对应mapper接口相同文件路径的包下

  • 相关阅读:
    tomcat安装
    卸载重安firefox
    Metasploit笔记之信息收集命令
    postgresql-9.0.18-1-linux.run启动
    ubuntu 安装自启动管理
    MySQL数据库”mysql SQL Error:1146,SQLState:42S02 “解决方法
    PE笔记之节表
    标准类型String(学习中)
    链表实现(打印元素的实现)
    C++中new和delete来创建和释放动态数组
  • 原文地址:https://www.cnblogs.com/guanyuehao0107/p/11800455.html
Copyright © 2020-2023  润新知