超详细的 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

 

大功告成 !!!!

posted @ 2018-08-08 15:01  暴躁的菜鸡  阅读(70)  评论(0编辑  收藏  举报