近期从SSM框架转为springboot开发restful风格的接口。网络上的资源质量良莠不齐,看了很多博客,有很多项目名字、项目代码、项目id等等等的东西统统一模一样但是按着来却是运行不通,我就奇了怪了,
你们都是统一思考的么?
2018-11-28 更新
- 寻找逆向生成器配置文件中的classpath方法 跳转
对于之前有过从事SSM框架或者SSH框架开发的猿来说,springboot会简单很多,不用那么繁杂的各种配置文件,一个helloword项目,可能只需要几行代码而已。
本文将使用jetbrains公司的idea创建springboot项目,并且配置mybatis以及mybatis逆向代码生成器
user
idea创建springboot项目真的简单,添加依赖也是简单至极。
新建一个空白目录来存放项目,在desktop
创建hellospring
目录。
打开软件选择create a new project
,以及spring initializr
,如图
next
后,填写相应信息,这个看个人爱好了,对项目运行没有多大区别,这里选择maven管理工具,当然也可以gradle
next
后就是选择依赖的界面了,也可以在创建后pom.xml
中手动添加。
点击左侧web
点击左侧sql
next
后选择项目地址
finish
后等待创建完成。
最终文件目录如下:
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%&
修改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
即可。如果没有这个插件,刷新试试.
成功后会生成UserMapper.java
User.java
UserMapper.xml
文件。在UserMapper
添加@Repository
注解。有对这6个函数不理解的可以看看这个:
https://blog.csdn.net/babybabyup/article/details/79761618
代码生成后,就可以使用数据库函数了。
在generator.properties
中添加mybatis配置
mybatis.mapper-locations=classpath:sqlmap/*.xml
在com.nick.hello
包中新建services
包,实现数据库接口,新建controller
包进行业务逻辑操作。
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")
运行后测试一下
数据库中查看一下,
OK了。哈哈哈哈哈啊哈