Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)

前言

近期从SSM框架转为springboot开发restful风格的接口。网络上的资源质量良莠不齐,看了很多博客,有很多项目名字、项目代码、项目id等等等的东西统统一模一样但是按着来却是运行不通,我就奇了怪了,你们都是统一思考的么?

2018-11-28 更新

  • 寻找逆向生成器配置文件中的classpath方法 跳转

正文

对于之前有过从事SSM框架或者SSH框架开发的猿来说,springboot会简单很多,不用那么繁杂的各种配置文件,一个helloword项目,可能只需要几行代码而已。
本文将使用jetbrains公司的idea创建springboot项目,并且配置mybatis以及mybatis逆向代码生成器

原料及工具

  • jetbrains idea
  • 本地mysql数据库
  1. url:jdbc:mysql://localhost:3306/hello
  2. user:root
  3. password:Hlm970%&
  4. 数据库有一张表user

创建springboot项目

idea创建springboot项目真的简单,添加依赖也是简单至极。

新建目录

新建一个空白目录来存放项目,在desktop创建hellospring目录。
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第1张图片

创建并运行

打开软件选择create a new project,以及spring initializr,如图
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第2张图片

next 后,填写相应信息,这个看个人爱好了,对项目运行没有多大区别,这里选择maven管理工具,当然也可以gradle

next后就是选择依赖的界面了,也可以在创建后pom.xml中手动添加。
点击左侧web

点击左侧sql
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第3张图片

next 后选择项目地址
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第4张图片
finish后等待创建完成。
最终文件目录如下:
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第5张图片

ok,现在写个测试api:
编辑HellospringAplication.java

package com.nick.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class HellospringApplication {

	public static void main(String[] args) {
		SpringApplication.run(HellospringApplication.class, args);
	}

    /**
     * 测试api
     * @return 测试返回字符串
     */
	@RequestMapping(value = "/")
    public String index() {
	    return "ok!";
    }
}

修改application.properties

server.port=8080

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8&nullCatalogMeansCurrent=true
spring.datasource.username=root
spring.datasource.password=Hlm970%&

或者也可以选择更加简介的yml文件进行配置,点击运行:
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第6张图片

访问8080端口
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第7张图片
一个简单的api已经可以用了。

配置mybatis 逆向代码生成器

添加插件

修改pom.xml文件,新增generator插件

			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				1.3.2

					src/main/resources/generatorConfig.xml
					true
					true
						
			

新建刚刚指定的src/main/resources/generatorConfig.xml,为了便于修改,我是又建了一个generator.properties文件来存储属性

generator.properties

#基本信息
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8&nullCatalogMeansCurrent=true
username=root
password=Hlm970%&

#entity 包名和 java目录
modelPackage=com.nick.hello.entity
modelProject=src/main/java

#sqlmap包名 和resources目录
sqlPackage=sqlmap
sqlProject=src/main/resources

#mapper包名和 java目录
mapperPackage=com.nick.hello.dao
mapperProject=src/main/java


table=user

generatorConfig.xml 要把classPathEntry中的location换成自己的。寻找方法




    
    

    
    
        
        
            
             
        

        

        
        
            
            
        

        
        
        

        
        
使用插件生成代码

打开左侧maven project 找到Plugins->mybatis-generator,双击mybatis-generator:generator即可。如果没有这个插件,刷新试试.

Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第8张图片
成功后会生成UserMapper.java User.java UserMapper.xml文件。在UserMapper添加@Repository注解。有对这6个函数不理解的可以看看这个:
https://blog.csdn.net/babybabyup/article/details/79761618

使用mybatis

代码生成后,就可以使用数据库函数了。
generator.properties中添加mybatis配置

mybatis.mapper-locations=classpath:sqlmap/*.xml

com.nick.hello包中新建services包,实现数据库接口,新建controller包进行业务逻辑操作。
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第9张图片

services包中,新建UserService.java,并添加@Service注解.只用insert函数测试一下

package com.nick.hello.services;

import com.nick.hello.dao.UserMapper;
import com.nick.hello.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements UserMapper {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return 0;
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return 0;
    }

    @Override
    public User selectByPrimaryKey(String id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return 0;
    }
}

controller包内新建TestContrller.java

package com.nick.hello.controller;

import com.nick.hello.entity.User;
import com.nick.hello.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class TestController {
    
    @Autowired
    private UserService userService;


    /**
     * 测试用
     * @param name 传入参数name
     * @param password 传入参数password
     * @return 插入结果,失败or成功+id
     */
    @RequestMapping(value = "insert", method = RequestMethod.GET)
    public String insertUser(@RequestParam("name") String name, @RequestParam("password") String password) {

        User user = new User();
        String id = UUID.randomUUID().toString().toLowerCase();
        user.setId(id);
        user.setName(name);
        user.setPassword(password);
        try {
            userService.insert(user);
            return "插入成功,其id为" + id;
        } catch (Exception e) {
            return "插入失败!";
        }
        
    } 
        
}

最后在HellospringApplication添加mapper扫描注解@MapperScan("com.nick.hello.dao")
运行后测试一下
Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第10张图片
数据库中查看一下,Mac下idea从零创建springboot项目以及整合mybatis和mybatis逆向生成工具-springboot(1)_第11张图片

OK了。哈哈哈哈哈啊哈

你可能感兴趣的:(springboot,java)