MyBatis Plus 引入需要注意的点
引入mybatis plus 的包(我的版本是比较低的)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
在数据源的xml中添加一下
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--对应的xml 文件所有位置-->
<property name="mapperLocations" value="classpath*:mapper/**/*.xml" />
<!--对应的实体类 文件所有位置-->
<property name="typeAliasesPackage" value="com.xxx.show.model.*"/>
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor" class="com.xxx.show.model.pagination.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
</bean>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--对应的DAO接口文件位置-->
<property name="basePackage" value="com.xxx.show.mapper.*"/>
</bean>
上面的此版本的PaginationInterceptor插件存在一个问题 就是在分页查询时候是用完
Connection 没有进行释放,所以我将源码复制了一份进行修改使用完之后进行关闭连接
在2.0.5版本已解决这个问题,可以将mybatis plus 升级成2.0.5 即可
以上配置完之后就可以
MP自动生产代码
public class MysqlGenerator {
/*
*
*
* 读取控制台内容
*
/**
* 代码生成 示例代码
*/
public static void main(String[] args) {
//1. 全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(true); // 是否支持AR模式
config.setAuthor("wujp@zjhcsoft.com"); // 作者
config.setOutputDir("D:\\"); // 生成路径这里是是你生成的Java代码的的路径
config.setFileOverride(true); // 文件覆盖
/*.setIdType(IdType.AUTO)*/ // 主键策略
config.setServiceName("%sService"); // 设置生成的service接口的名字的首字母是否为I
// IEmployeeService
config.setBaseResultMap(true);
config.setBaseColumnList(true);
//2. 数据源配置
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.setDbType(DbType.MYSQL);
dsConfig.setDriverName("com.mysql.jdbc.Driver");
dsConfig.setUrl("jdbc:mysql://xxxx:3306/accept_db?useUnicode=true&characterEncoding=utf-8");
dsConfig.setUsername("terminal");
dsConfig.setPassword("terminal$123");
//3. 策略配置
StrategyConfig stConfig = new StrategyConfig();
stConfig.setCapitalMode(true); //全局大写命名
/*stConfig.setDbColumnUnderline(true); // 指定表名 字段名是否使用下划线*/
stConfig.setNaming(NamingStrategy.underline_to_camel); // 数据库表映射到实体的命名策略
String[] prefix = {"t_"};
String[] tables = {
"t_shipping"
};
stConfig.setTablePrefix(prefix);
stConfig.setInclude(tables);
//4. 包名策略配置
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent("com.xxxx.show");
pkConfig.setMapper("mapper.shipping"); //新增Mapper的文件夹
pkConfig.setService("service.shipping"); //新增Service的文件夹
pkConfig.setController("controller"); //新增controller的文件夹
pkConfig.setEntity("model.shipping"); //新增beans的文件夹
pkConfig.setXml("mapper.shipping"); //新增mapper.xml
//5. 整合配置
AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(config);
ag.setDataSource(dsConfig);
ag.setStrategy(stConfig);
ag.setPackageInfo(pkConfig);
//6. 执行
ag.execute();
}
}
生成完之后得serviceImpl需要进行重写
创建一个BaseServiceImpl 重写serviceImpl文件的 BaseMapper 注入方法
Mapper 采用抽象方法,由子类传递只用
如果不进行Impl重写会出现 在启动时候
No unique bean of type [com.baomidou.mybatisplus.mapper.BaseMapper] is defined: expected single matching bean but found 7:
serviceImpl实现类的调整非常的重要