<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.11version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>1.2.4version>
dependency>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generatorartifactId>
<version>1.3.7version>
dependency>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.7version>
dependency>
# mybatis 配置
mybatis.type-aliases-package=com.demo.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
# 通用 Mapper 配置
mapper.mappers=com.demo.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
和pom.xml在同级目录
<generatorConfiguration>
<properties resource="datasource.properties">properties>
<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.demo.utils.MyMapper"/>
plugin>
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<commentGenerator>
<property name="suppressAllComments" value="false" />
commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="${db.url}"
userId="${db.username}"
password="${db.password}">
jdbcConnection>
<javaModelGenerator targetPackage="com.demo.pojo" targetProject="src/main/java">
<property name="constructorBased" value="true"/>
javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<javaClientGenerator targetPackage="com.demo.mapper" targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="device" domainObjectName="Device">
<generatedKey column="device_id" sqlStatement="SELECT LAST_INSERT_ID()"/>
table>
<table tableName="print_file" domainObjectName="PrintFile">
<generatedKey column="file_id" sqlStatement="SELECT LAST_INSERT_ID()"/>
table>
context>
generatorConfiguration>
路径 com.demo.utils.MyMapper
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
//TODO
//FIXME 特别注意,该接口不能被扫描到,否则会出错
}
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class GeneratorDisplay {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("src/main/resources/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
datasource.properties
db.url=jdbc:mysql://localhost:3306/gdpt_posmart_office?serverTimezone=UTC&useSSL=false
db.username=root
db.password=root
# 其他参数
https://blog.csdn.net/yinaoxiao7661/article/details/88737604
方式一:
添加
useGeneratedKeys=“true” keyProperty=“fileId” keyColumn=“file_id”
<insert id="myInsert" parameterType="com.gdpt.crawlerprint.pojo.PrintFile"
useGeneratedKeys="true" keyProperty="fileId" keyColumn="file_id">
insert into cp_print_file (user_id, filename,
file_type, upload_date)
values (#{userId,jdbcType=INTEGER}, #{filename,jdbcType=VARCHAR},
#{fileType,jdbcType=BIT}, #{uploadDate,jdbcType=TIMESTAMP})
insert>
获取自增长id
printFileMapper.myInsert(printFile);
int id = 0;
if (printFile.getFileId() != null){
id = printFile.getFileId();
}
方式二:
generatorConfig.xml配置中
<table tableName="print_file" domainObjectName="PrintFile">
<generatedKey column="file_id" sqlStatement="SELECT LAST_INSERT_ID()"/>
table>
获取自增长id
printFileMapper.insert(printFile);
int id = 0;
if (printFile.getFileId() != null){
id = printFile.getFileId();
}
//(1)mapper基础接口
//select接口
List<VirtualIpBean> vipList = vipMapper.select(vipBean);//根据实体中的属性值进行查询,查询条件使用等号
VirtualIpBean vip = vipMapper.selectOne(vipBean);//根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
List<VirtualIpBean> vipList2 = vipMapper.selectAll();//查询全部结果,select(null)方法能达到同样的效果
VirtualIpBean vip2 = vipMapper.selectByPrimaryKey(1);//根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
int count = vipMapper.selectCount(vipBean);//根据实体中的属性查询总数,查询条件使用等号
//insert接口
int a = vipMapper.insert(vipBean);//保存一个实体,null的属性也会保存,不会使用数据库默认值
int a1 = vipMapper.insertSelective(vipBean);//保存实体,null的属性不会保存,会使用数据库默认值
//update接口
int b = vipMapper.updateByPrimaryKeySelective(vipBean);//根据主键更新属性不为null的值
int c = vipMapper.updateByPrimaryKey(vipBean);//根据主键更新实体全部字段,null值会被更新
//delete接口
int d = vipMapper.delete(vipBean);//根据实体属性作为条件进行删除,查询条件使用等号
int e = vipMapper.deleteByPrimaryKey(1);//根据主键字段进行删除,方法参数必须包含完整的主键属性
//(2)Example方法
Example example = new Example(VirtualIpBean.class);
example.createCriteria().andEqualTo("id", 1);
example.createCriteria().andLike("val", "1");
//自定义查询
List<VirtualIpBean> vipList3 = vipMapper.selectByExample(example);