springboot+springmvc+mybatis+mybatis-generator+REST 基础配置,搭建user 查询和增加
环境
- Spring CLI V2.0.0.0RELEASE
- Spring Tool Suite
- Version: 3.9.3.RELEASE
git
- 源代码:https://github.com/YILEOSC/hybrid/tree/hybrid-base
目录结构
![](https://i-blog.csdnimg.cn/blog_migrate/7eccba98fe486d5ba4253857a96f7899.png)
- com.yile.db.dao 数据访问层 自动生成
- com.yile.db.model 数据访问层 自动生成
- com.yile.demo spring 启动配置
- com.yile.services REST API
- src/main/resources 配置文件
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.yile</groupId>
<artifactId>yile-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>yile-service</name>
<description>yile service project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.3.6</version>
<type>pom</type>
</dependency>
<!-- mysql generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mysql generator -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<configurationFile>src/main/resources/mybatis/mybatis-generator.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
mybatis-generator 插件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="C:/Users/IBM_ADMIN/.m2/repository/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45-sources.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/yile" userId="root"
password="ROOT">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.yile.db.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper.xml"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.yile.db.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="users" tableName="users" domainObjectName="Users">
<property name="useActualColumnNames" value="true" />
<generatedKey column="id" sqlStatement="MySql"
identity="true"></generatedKey>
</table>
</context>
</generatorConfiguration>
mybatis-generator 运行
Goals:mybatis-generator:generate
spring 配置
server.port= 8000
server.servlet.context-path=/yile
mybatis.typeAliasesPackage=com.yile.db.model
mybatis.mapper-locations=classpath:mapper/xml/*.xml
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yile
spring.datasource.username=root
spring.datasource.password=ROOT
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring 启动类
package com.yile.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan("com.yile.services")
@MapperScan("com.yile.db.dao")
@SpringBootApplication
public class YileServiceApplication {
public static void main(String[] args) {
SpringApplication.run(YileServiceApplication.class, args);
}
}
REST
package com.yile.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yile.db.dao.UsersMapper;
import com.yile.db.model.Users;
import com.yile.db.model.UsersExample;
@RestController
public class UserController {
@Autowired
private UsersMapper userDao;
@RequestMapping("/getAllUsers")
public List<Users> getAllUsers() {
UsersExample userExample = new UsersExample();
userExample.createCriteria().andIdGreaterThan(0);
List<Users> users=this.userDao.selectByExample(userExample);
return users;
}
@RequestMapping("/saveUsers")
public boolean saveUsers(){
boolean res=false;
Users users=new Users();
users.setAddress("adress");
users.setName("name");
users.setPhone("1234567");
int i= this.userDao.insert(users);
if(i>0) {
res=true;
}
return res;
}
}
启动
mvn install
java -jar target jar
访问
http://localhost:8000/yile/getAllUsers
http://localhost:8000/yile/saveUsers