mybatis-plus 学习笔记
2024-10-20 20:35:28
一、首先是POM
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
</dependency>
<dependency><!--添加MySQL驱动依赖 -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <!--注意,MybatisPlus Generator默认使用Velocity,不添加此依赖会报错-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency> <!--注意,在Generator时需要,但是在运行Application时请将此注释掉-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.0.4</version>
</dependency> </dependencies>
未添加Velocity依赖会抛出:Caused by: java.lang.ClassNotFoundException: org.apache.velocity.context.Context
运行application,却未注释mybatis-plus-generator,会抛出:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [/Users/zhenghao/git/ampmybatisplus/target/classes/cn/jsfund/amp/mp/mapper/UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.NoSuchMethodError: com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.setConfiguration(Lcom/baomidou/mybatisplus/core/MybatisConfiguration;)V
二、代码生成
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; /**
* @Description:
* @Date 2019/2/21 22:05
*/
public class MPGenerator {
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
GlobalConfig cfg = new GlobalConfig(); cfg.setActiveRecord(true).setAuthor("重八")
.setOutputDir(projectPath + "/src/main/java")
.setFileOverride(true)
.setIdType(IdType.AUTO)
.setServiceName("%sService")
.setBaseResultMap(true)
.setBaseColumnList(false); DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/db?useSSL=false");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123"); StrategyConfig stConfig = new StrategyConfig();
stConfig.setCapitalMode(true)
.setNaming(NamingStrategy.underline_to_camel)
.setRestControllerStyle(true)
.setEntityLombokModel(true)
.setInclude("user_info"); PackageConfig pkConfig = new PackageConfig();
pkConfig
.setParent("com.baomidou.xxx")
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("entity")
.setXml("xml"); AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(cfg)
.setDataSource(dsc)
.setStrategy(stConfig)
.setPackageInfo(pkConfig); //6. 执行
ag.execute(); }
}
以上是mysql的生成,如果使用Oracle数据库的话,schema,table,colume全部需要大写,否则会抛异常;例如:
dsc.setSchemaName("SCOTT");
三、配置:
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver ## Mybatis-plus 配置 mybatis-plus.mapper-locations=classpath:xml/*.xml
mybatis-plus.typeAliasesPackage=classpath:xml/*.xml
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
最新文章
- 多功能前台交互效果插件superSlide
- 初探JavaScript(二)——JS如何动态操控HTML
- 第十七课:js数据缓存系统的原理
- turing 项目引用
- Code Review的一些典型内容
- IOS game
- [LINQ]查询关键字
- 返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller
- MPLS VPN随堂笔记3
- 风趣的JavaScript面向对象入门课程一
- 乌龟棋dp
- postman的安装和使用
- day-01(html)
- JAVA追加写入文本文件
- Python学习笔记第二十周
- Netty 高性能之道 - Recycler 对象池的复用
- 合并果子(NOIP2004)
- python装饰器,其实就是对闭包的使用。
- spring-mvc----数据库数据到页面错误--tomcat启动不了
- Spring -- spring结合aop 进行 tx&;aspectj事务管理配置方法