zoukankan      html  css  js  c++  java
  • 超详细的 springboot & mybatis 程序入门

    ps:网上有很多类似的入门案例,我也是看了被人的之后自己写的一个

     

    估计有哥们懒 我把数据表格拿上来,数据自己填吧

    CREATE TABLE `tb_user` (
      `id` int(10) DEFAULT NULL,
      `name` varchar(25) CHARACTER SET utf8 DEFAULT NULL,
      `age` int(10) DEFAULT NULL,
      `address` varchar(25) CHARACTER SET utf8 DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    第一步 导入pom

    <?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.bluecard.zh</groupId>
        <artifactId>zh-springboot</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.7.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.0</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.44</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.0</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
            <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>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>-->
    
            </resources>
        </build>
    </project>

    第二步  建包

    第三步 基本配置 application.properties

    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    
    spring.datasource.initialSize=5  
    spring.datasource.minIdle=5  
    spring.datasource.maxActive=20  
    spring.datasource.maxWait=60000  
    
    #mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
    mybatis.mapper-locations=classpath*:com/bluecard/zh/mapper/sql/*Mapper.xml
    mybatis.type-aliases-package=com.bluecard.zh.model
    server.port= 9090

    >>> UserController 类

    package com.bluecard.zh.controller;
    
    import com.bluecard.zh.model.User;
    import com.bluecard.zh.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * Created by zheng_fx on 2018-08-08 11:57
     */
    @Controller
    @RequestMapping("user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @RequestMapping("/getUserInfo")
        @ResponseBody
        public List<User> getUserInfo(){
            return userService.getUserInfo();
        }
    }
    

    >>> UserService 类

    package com.bluecard.zh.service;
    
    import com.bluecard.zh.model.User;
    
    import java.util.List;
    
    /**
     * Created by zheng_fx on 2018-08-08 13:23
     */
    public interface UserService {
    
        public List<User> getUserInfo();
    }
    

    >>> UserServiceImpl

    package com.bluecard.zh.service.impl;
    
    import com.bluecard.zh.mapper.UserMapper;
    import com.bluecard.zh.model.User;
    import com.bluecard.zh.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * Created by zheng_fx on 2018-08-08 13:23
     */
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public List<User> getUserInfo() {
            return userMapper.getUserInfo();
        }
    }
    

    >>> UserMapper 

    package com.bluecard.zh.mapper;
    
    import com.bluecard.zh.model.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    /**
     * Created by zheng_fx on 2018-08-08 13:24
     */
    
    public interface UserMapper {
    
    //    @Select("select * from tb_user")
        public List<User> getUserInfo();
    }
    

    >>> 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="com.bluecard.zh.mapper.UserMapper">
        <resultMap id="userinfo" type="com.bluecard.zh.model.User">
            <id property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="age" column="age"/>
            <result property="address" column="address"/>
        </resultMap>
        <select id="getUserInfo" resultType="User">
            select * from tb_user
        </select>
    
    </mapper>
    

    >>> 启动类 ApplicationStart

    package com.bluecard.zh;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * Created by zheng_fx on 2018-08-08 13:26
     */
    @SpringBootApplication
    @MapperScan("com.bluecard.*.mapper")
    public class ApplicationStart {
        public static void main(String[] args) {
            SpringApplication.run(ApplicationStart.class,args);
            System.out.println("================== SpringBoot Start Success ==================");
        }
    }
    

    最后启动项目 

    测试地址: http://localhost:9090/user/getUserInfo

    大功告成 !!!!

  • 相关阅读:
    年轻人的第一个 Spring Boot 应用,太爽了!
    面试问我 Java 逃逸分析,瞬间被秒杀了。。
    Spring Boot 配置文件 bootstrap vs application 到底有什么区别?
    坑爹的 Java 可变参数,把我整得够惨。。
    6月来了,Java还是第一!
    Eclipse 最常用的 10 组快捷键,个个牛逼!
    Spring Cloud Eureka 自我保护机制实战分析
    今天是 Java 诞生日,Java 24 岁了!
    厉害了,Dubbo 正式毕业!
    Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!
  • 原文地址:https://www.cnblogs.com/zhengfx/p/13974368.html
Copyright © 2011-2022 走看看