• springboot+mybatis基本环境搭建


    SpringBoot整合Mybatis环境搭建:
    
    1.创建工程springboot_mybatis,导入依赖:包含父工程,web起步依赖,mysql驱动以及mybatis起步依赖
        <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
    
            <groupId>com.it</groupId>
            <artifactId>springboot_mybatis</artifactId>
            <version>1.0-SNAPSHOT</version>
            <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.1.4.RELEASE</version>
            </parent>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
    
                <!--测试起步依赖-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-test</artifactId>
                    <scope>test</scope>
                </dependency>
                <!--添加依赖:MySQL驱动,springboot整合mybatis-->
                <!--驱动-->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <scope>runtime</scope>
                </dependency>
                <!--mybatis的 起步依赖-->
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>2.0.1</version>
                </dependency>
            </dependencies>
    
    
        </project>
        
    2.创建启动类:使用@MapperScan指定要扫描的包
        package com.it;
    
        import org.mybatis.spring.annotation.MapperScan;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
    
        /**
         * 启动类
         *
         * @author Lyle
         * @date 2020/7/11
         */
        @SpringBootApplication
        @MapperScan(basePackages = "com.it.dao")
        public class SpringBootMybatisApplication {
            public static void main(String[] args) {
                SpringApplication.run(SpringBootMybatisApplication.class,args);
            }
        }
        
    3.配置springboot配置文件:数据源,mybatis映射文件位置,应用名称以及应用端口号;
        spring:
          datasource:
            driver-class-name: com.mysql.jdbc.Driver
            url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
            username: root
            password: root
          #配置mapper的映射文件的位置,配置该配置后,接口包路径和映射文件包路径可以不一致
          mybatis:
            mapper-locations: classpath:mappers/*Mapper.xml
          application:
            name: user
        server:
          port: 25501
          
    4.数据库表:
        CREATE TABLE `user` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `username` varchar(50) DEFAULT NULL,
          `password` varchar(50) DEFAULT NULL,
          `name` varchar(50) DEFAULT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
        
    5.数据库表对应的实体类:
        package com.it.pojo;
    
        import java.io.Serializable;
    
        /**
         * ToDo
         *
         * @author Lyle
         * @date 2020/4/1
         */
        public class User implements Serializable {
            private Integer id;
            private String username;//用户名
            private String password;//密码
            private String name;//姓名
    
            public Integer getId() {
                return id;
            }
    
            public void setId(Integer id) {
                this.id = id;
            }
    
            public String getUsername() {
                return username;
            }
    
            public void setUsername(String username) {
                this.username = username;
            }
    
            public String getPassword() {
                return password;
            }
    
            public void setPassword(String password) {
                this.password = password;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
        }
        
    6.典型的三层架构:controller,service,dao
        6.1 controller层:
            package com.it.controller;
    
            import com.it.pojo.User;
            import com.it.service.IUserService;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.web.bind.annotation.RequestMapping;
            import org.springframework.web.bind.annotation.RestController;
    
            import java.util.List;
    
            /**
             * ToDo
             *
             * @author Lyle
             * @date 2020/7/11
             */
            @RestController
            @RequestMapping(value = "/user")
            public class UserController {
    
                @Autowired
                private IUserService userService;
                @RequestMapping(value = "/findAll")
                public List<User> findList(){
                    return userService.findList();
                }
            }
        6.2 service接口及其实现类:
            接口:
            package com.it.service;
    
            import com.it.pojo.User;
    
            import java.util.List;
    
            /**
             * ToDo
             *
             * @author Lyle
             * @date 2020/7/11
             */
            public interface IUserService {
                List<User> findList();
            }
            
            实现类:
            package com.it.service.impl;
    
            import com.it.dao.UserMapper;
            import com.it.pojo.User;
            import com.it.service.IUserService;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.stereotype.Service;
    
            import java.util.List;
    
            /**
             * ToDo
             *
             * @author Lyle
             * @date 2020/7/11
             */
            @Service
            public class UserService implements IUserService {
    
                @Autowired
                private UserMapper userMapper;
    
    
                @Override
                public List<User> findList() {
                    return userMapper.findAll();
                }
            }
            
        6.3 dao接口及xml文件:
            dao接口:
            package com.it.dao;
    
            import com.it.pojo.User;
            import org.springframework.stereotype.Repository;
    
            import java.util.List;
    
            /**
             * ToDo
             *
             * @author Lyle
             * @date 2020/4/1
             */
            @Repository
            public interface UserMapper {
    
                public List<User> findAll();
            }
            
            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="com.it.dao.UserMapper">
                    <select id="findAll" resultType="com.it.pojo.User">
                        SELECT * from user
                    </select>
                </mapper>
    
    调用接口测试:打开浏览器,输入http://localhost:25501/user/findAll
  • 相关阅读:
    Asp.Net MVC 体验 1
    myisamchk命令进行崩溃恢复Myisam数据表
    nginx 全局变量
    centos开机启动项设置命令:chkconfig
    redis info 参数说明
    PHP中Imagick的使用
    查看当前nginx、mysql的连接数
    wget参数及用法
    编辑器与IDE
    广州求职,工作经验>5.期待伯乐
  • 原文地址:https://www.cnblogs.com/lyle-liu/p/13282742.html
Copyright © 2020-2023  润新知