springboot+mybatis+mysql 利用mybatis自动生成sql语句
工具和环境
idea,mysql,JDK1.8
效果图如下

结构图如下
java

resources

sql文件
/*
Navicat MySQL Data Transfer Source Server : localhost_3306
Source Server Version : 50562
Source Host : localhost:3306
Source Database : miaosha Target Server Type : MYSQL
Target Server Version : 50562
File Encoding : 65001 Date: 2020-07-06 20:34:49
*/ SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for user_info
-- ----------------------------
DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL DEFAULT '',
`gender` int(11) NOT NULL DEFAULT '0' COMMENT '//1代表男性,2代表女性',
`age` int(11) NOT NULL,
`telphone` varchar(0) NOT NULL DEFAULT '',
`register_mode` varchar(0) NOT NULL DEFAULT '' COMMENT '//byphone,bywechat,byalipay',
`third_party_id` varchar(64) NOT NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; -- ----------------------------
-- Records of user_info
-- ----------------------------
INSERT INTO `user_info` VALUES ('1', '王王', '1', '12', '', '', '');
INSERT INTO `user_info` VALUES ('2', 'ww', '0', '21', '', '', ''); -- ----------------------------
-- Table structure for user_password
-- ----------------------------
DROP TABLE IF EXISTS `user_password`;
CREATE TABLE `user_password` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`encrpt_password` varchar(128) NOT NULL DEFAULT '',
`user_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; -- ----------------------------
-- Records of user_password
-- ----------------------------
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>miaosha</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
</build> </project>
自动生成映射关系的mybatis-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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/miaosha" userId="root" password="magicimage"></jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成dataObject的包名和位置 -->
<javaModelGenerator targetPackage="com.miaoshaproject.dataobject" targetProject="src/main/java">
<property name="enableSbuPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 映射文件生成所在的目录为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.miaoshaproject.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--tableName是数据库中表的名字,domainObjectName是实体的名字-->
<table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user_password" domainObjectName="UserPasswordDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
application.properties文件
server.port=8090 #数据库
spring.datasouce.name=miaosha
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=magicimage #druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver #mybatis
mybatis.mapper-locations=classpath:mapping/*.xml
mybatis.type-aliases-package=com.miaoshaproject.dataobject #模板
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html #指定某些文件不进行监听,即不会进行热加载
spring.devtools.restart.exclude=application.properties
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java
MiaoshaApplication
@SpringBootApplication(scanBasePackages={"com.miaoshaproject.*"})
@MapperScan("com.miaoshaproject.dao")
public class MiaoshaApplication {
public static void main(String[] args) {
SpringApplication.run(MiaoshaApplication.class, args);
}
}
controller
@Controller
@RequestMapping("/")
public class Forward {
@RequestMapping("/index")
public String index() {
return "/index";
}
}
@RestController
public class UserController { @Autowired
private UserService userService; @RequestMapping("/getUser")
public UserDO getName(Integer id) {
UserDO user = userService.getName(id);
System.out.println(user.getName());
return user;
}
}
service
public interface UserService {
UserDO getName(Integer id);
}
serviceImpl
@Service
public class UserServiceImpl implements UserService { @Autowired
private UserDOMapper userDOMapper; @Override
public UserDO getName(Integer id) {
return userDOMapper.selectByPrimaryKey(id);
}
}
dao
public interface UserDOMapper {
int deleteByPrimaryKey(Integer id);
int insert(UserDO record);
int insertSelective(UserDO record);
UserDO selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(UserDO record);
int updateByPrimaryKey(UserDO record);
}
dataobject
package com.miaoshaproject.dataobject;
public class UserDO {
private Integer id;
private String name;
private Integer gender;
private Integer age;
private String telphone;
private String registerMode;
private String thirdPartyId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone == null ? null : telphone.trim();
}
public String getRegisterMode() {
return registerMode;
}
public void setRegisterMode(String registerMode) {
this.registerMode = registerMode == null ? null : registerMode.trim();
}
public String getThirdPartyId() {
return thirdPartyId;
}
public void setThirdPartyId(String thirdPartyId) {
this.thirdPartyId = thirdPartyId == null ? null : thirdPartyId.trim();
}
}
mapping
<?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.miaoshaproject.dao.UserDOMapper">
<resultMap id="BaseResultMap" type="com.miaoshaproject.dataobject.UserDO">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="gender" jdbcType="INTEGER" property="gender" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="telphone" jdbcType="VARCHAR" property="telphone" />
<result column="register_mode" jdbcType="VARCHAR" property="registerMode" />
<result column="third_party_id" jdbcType="VARCHAR" property="thirdPartyId" />
</resultMap>
<sql id="Base_Column_List">
id, name, gender, age, telphone, register_mode, third_party_id
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user_info
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from user_info
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.miaoshaproject.dataobject.UserDO">
insert into user_info (id, name, gender,
age, telphone, register_mode,
third_party_id)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{gender,jdbcType=INTEGER},
#{age,jdbcType=INTEGER}, #{telphone,jdbcType=VARCHAR}, #{registerMode,jdbcType=VARCHAR},
#{thirdPartyId,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.miaoshaproject.dataobject.UserDO">
insert into user_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="gender != null">
gender,
</if>
<if test="age != null">
age,
</if>
<if test="telphone != null">
telphone,
</if>
<if test="registerMode != null">
register_mode,
</if>
<if test="thirdPartyId != null">
third_party_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="gender != null">
#{gender,jdbcType=INTEGER},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="telphone != null">
#{telphone,jdbcType=VARCHAR},
</if>
<if test="registerMode != null">
#{registerMode,jdbcType=VARCHAR},
</if>
<if test="thirdPartyId != null">
#{thirdPartyId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.miaoshaproject.dataobject.UserDO">
update user_info
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="gender != null">
gender = #{gender,jdbcType=INTEGER},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
<if test="telphone != null">
telphone = #{telphone,jdbcType=VARCHAR},
</if>
<if test="registerMode != null">
register_mode = #{registerMode,jdbcType=VARCHAR},
</if>
<if test="thirdPartyId != null">
third_party_id = #{thirdPartyId,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.miaoshaproject.dataobject.UserDO">
update user_info
set name = #{name,jdbcType=VARCHAR},
gender = #{gender,jdbcType=INTEGER},
age = #{age,jdbcType=INTEGER},
telphone = #{telphone,jdbcType=VARCHAR},
register_mode = #{registerMode,jdbcType=VARCHAR},
third_party_id = #{thirdPartyId,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
css
p,label{
color: green;
}
js
$(document).ready(function(){
$("button#aa").click(function(){
//var jsonObj = {id: $("#id").val(),name:"abcd",age:"123"};
$.ajax({
async : false,
url: "/getUser",
data:{
id:$("#id").val()
},
//data:JSON.stringify(jsonObj), 传入json数据
type: "POST",
dataType:"json",//返回数据类型
//headers:{"Content-Type":"application/json"}, 传入json数据时需设置请求头
success:function(data){
alert(data.name);
},
error:function(err){
alert('连接失败');
}
});
});
});
html
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<title>测试网站t</title>
<link th:href="@{/css/index.css}" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" th:src="@{/js/index.js}"></script>
</head>
<body>
<p>
<label class="name">t用户id:</label>
<input placeholder="输入id" type="text" class="text-box" id="id"/>
<button id="aa">按钮</button>
</p>
</body>
</html>
springboot+mybatis+mysql 利用mybatis自动生成sql语句的更多相关文章
- 利用反射自动生成SQL语句(仿Linq)
转:http://www.cnblogs.com/the7stroke/archive/2012/04/22/2465597.html using System; using System.Colle ...
- 使用Excel自动生成sql语句
在近一段日子里,进入了新的项目组,由于项目需要,经常要将一些Excel表中的数据导入数据库中,以前并没有过多的接触过数据导入与数据处理,对于我来说比较痛苦,今天下午花了几个小时处理数据,但是同事给我提 ...
- 根据DELTA自动生成SQL语句
上传客户端的CLIENTDATASET.delta到服务器的clientdataset.data,服务端解析clientdataset的数据生成相应的SQL语句. 相对于直接调用datasetprov ...
- 城市联动 - 自动生成SQL语句
字段比较简单/ 如果有需要可以自己定制字段和排序/ 一共二级城市联动, 本人业务需要, 所以就两层, 网上关于三层的挺多, 有需要可以借鉴/ 废话不多说, 先看效果图, 代码在下面 <?php ...
- Excel 提供数据 更新或者插入数据 通过函数 自动生成SQL语句
excel 更新数据 ="UPDATE dbo.yt_vehicleExtensionBase SET yt_purchase_date='"&B2&"' ...
- 打开powerDesigner时,创建table对应的自动生成sql语句没有注释
在创建pdm时由于需要在name列填写的是以后要在表中创建的注释信息,comment中则写的说明信息字数比较多.默认情况下在生成建表sql时不能将name生成注释信息,进行如下设置可以讲name生成注 ...
- 使用Java注解开发自动生成SQL
使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...
- Excel表格生成sql语句
假如excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age ,在你的excel表格中增加一列,利用excel的公式自动生成sql语句,方法如下: ...
- springboot整合mybatis,利用mybatis-genetor自动生成文件
springboot整合mybatis,利用mybatis-genetor自动生成文件 项目结构: xx 实现思路: 1.添加依赖 <?xml version="1.0" e ...
随机推荐
- windows 7系统安装与配置Tomcat服务器环境
windows 7系统安装与配置Tomcat服务器环境 学习了一个月的java基础,终于要迈向java web领域.学习java web开发就离不开服务器的支持,由于本人是菜鸟,只好求助度娘谷哥.在此 ...
- 06:Database returned an invalid datetime value. Are time zone definitions for your database installed?
出现时区问题 解决方案: 修改settings.py的时区变量. 修改前: LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N =True USE_L ...
- 我试了试用 SQL查 Linux日志,好用到飞起
大家好,我是小富~ 最近发现点好玩的工具,迫不及待的想跟大家分享一下. 大家平时都怎么查Linux日志呢? 像我平时会用tail.head.cat.sed.more.less这些经典系统命令,或者aw ...
- 【模板】map入门
map 在数据特别庞大,数组已经满足不了的某些情况下codevs p1230,可以用上map; 我们可以将map容器作为一个有序的映射表,看作为一个下表可以是任意类型的数组: map是一个红黑树,单次 ...
- Centos8.3、proxysql2.0读写分离实战记录
接着主从复制继续往下讲,这个项目中我是使用proxysql做读写分离的中间件,之前是使用mycat.老实说mycat属于比较重量级的中间件,1.0还好到了2.0配置变得很复杂而且文档不是很齐全,我看着 ...
- Oracle不知道用户密码情况下,如何在不更改密码的前提下解锁用户或者延期密码有效期
1.问题描述: 生产环境,zabbix告警业务用户密码即将过期,但是如何不知道业务用户密码的情况下来解决该问题? 2.实验一: 1)创建新的用户test,并授予test resource角色和conn ...
- 技术实践:教你用Python搭建gRPC服务
摘要:gRPC是一个高性能.通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf序列化协议开发,且支持众多开发语言. 本文分享自华为云社区& ...
- 服务器硬件和RAID配置
服务器 硬件和RAID配置 目录 一.RAID 磁盘阵列介绍 1.1.RAID 0(条带化存储) 1.2.RAID 1(镜像存储) 1.3.RAID 5 1.4.RAID 6 1.5.RAID 1+0 ...
- SpringCloud Alibaba实战(9:Hystrix容错保护)
源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在上一节我们已经使用OpenFeign完成了服务间的调用.想一下,假如我们一个服务链 ...
- Linux中使用QT读取并显示温度传感器数值
环境: Ubuntu 16.04 64 bit QT5.13.1/QT5.12 原理 对于Linux来说一切都是文件,温度传感器也是个文件,如果要获取某个温度传感器的数值,可以读取相应的文件,Linu ...