springboot+mybtis逆向工程 Restful IntelliJ IDEA+mysql

1、创建表

DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
`id` int(64) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`password` varchar(64) DEFAULT NULL,
`salt` varchar(64) DEFAULT NULL,
`state` varchar(8) DEFAULT NULL,
`username` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

2、POM.xml

<?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.example</groupId>
<artifactId>springboot-mybatis-generator-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springboot-mybatis-generator-test</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>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>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>


</project>

3、在resources下创建 generator文件夹,并创建generator.xml文件

4、generator.xml

<?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="D:\Maven\repository\repo\mysql\mysql-connector-java\5.1.45\mysql-connector-java-5.1.45.jar"/>
<!-- 表设计时有联合主键时, 不需要单独主键实体类时 把defaultModelType设置为flat -->
<context id="DB2Tables" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- JavaBean 实现 序列化 接口 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!-- 生成toString -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- <property name="suppressAllComments" value="true"/>-->
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/library"
userId="root"
password="root">
</jdbcConnection>
<!-- 类型转换 -->
<javaTypeResolver >
<!-- 是否使用bigDecimal,
false: 把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer(默认)
true: 把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.example.restfuldemo.domain" targetProject="src/main/java">
<!-- 默认false 是否允许子包 -->
<property name="enableSubPackages" value="true" />
<!-- 默认false 是否对model添加 构造函数 -->
<property name="constructorBased" value="false"/>
<!-- 默认false 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
<!-- 默认false 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.restfuldemo.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="user_info"></table>
</context>
</generatorConfiguration>

5、Run->Edit Configurations->+号Maven 选择Working directory,项目所在路径,在Command line输入:mybatis-generator:generate -e
或者 右键 run mybatis-generator 生成文件
6、application.properties
#数据源配置
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username = root
spring.datasource.password = root

## Mybatis 配置
# 配置为 com.example.restfuldemo.domain 指向实体类包路径。
mybatis.typeAliasesPackage=com.example.restfuldemo.domain
# 配置为 classpath 路径下 mapper 包下,* 代表会扫描所有 xml 文件。
mybatis.mapperLocations=classpath:/mappers/*.xml

7、UserInfoService与Impl
package com.example.restfuldemo.service;

import com.example.restfuldemo.domain.UserInfo;

import java.util.List;

public interface UserInfoService {

/**
* 新增用户
* @param userInfo
* @return
*/
int addUser(UserInfo userInfo);

/**
* 修改用户
* @param userInfo
* @return
*/
int updateUser(UserInfo userInfo);

/**
* 删除用户
* @param id
* @return
*/
int delUser(int id);

/**
* 查询所有用户
* @return
*/
List<UserInfo> findAll();
}


package com.example.restfuldemo.service;

import com.example.restfuldemo.dao.UserInfoMapper;
import com.example.restfuldemo.domain.UserInfo;
import com.example.restfuldemo.domain.UserInfoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService{
@Autowired
UserInfoMapper userInfoMapper;

@Override
public int addUser(UserInfo userInfo) {
int result = 0;
result = userInfoMapper.insert(userInfo);
return result;
}

@Override
public int updateUser(UserInfo userInfo) {
int result = 0;
result = userInfoMapper.updateByPrimaryKey(userInfo);
return result;
}

@Override
public int delUser(int id) {
int result = 0;
result = userInfoMapper.deleteByPrimaryKey(id);
return result;
}

@Override
public List<UserInfo> findAll() {
UserInfoExample example = new UserInfoExample();
//有查询条件框时这样写
// UserInfoExample.Criteria criteria = example.createCriteria();
// criteria.andUsernameLike("%"+username+"%");
List<UserInfo> userInfos = userInfoMapper.selectByExample(example);
return userInfos;
}
}

8、controller

package com.example.restfuldemo.controller;

import com.example.restfuldemo.domain.UserInfo;
import com.example.restfuldemo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserInfoController {

@Autowired
UserInfoService userInfoService;

@PostMapping
@GetMapping("/POST/users")
public int addUser(UserInfo userInfo){
return userInfoService.addUser(userInfo);
}

@PutMapping
@GetMapping("/PUT/users")
public int updateUser(UserInfo userInfo){
return userInfoService.updateUser(userInfo);
}

@DeleteMapping
@GetMapping("/DELETE/users")
public int delUser(int id){
return userInfoService.delUser(id);
}

@GetMapping("/GET/users/userAll")
public List<UserInfo> findAll(){
return userInfoService.findAll();
}

}

springboot+mybtis逆向工程 Restful IntelliJ IDEA+mysql

上一篇:MyCat实现MySQL双主双从的读写分离


下一篇:Java mail发送邮件报错550 Mailbox not found or access denied原因