添加依賴
<dependency> | |||||||||||||||||||
<groupId>com.baomidou</groupId> | |||||||||||||||||||
<artifactId>mybatis-plus</artifactId> | |||||||||||||||||||
<version>2.3</version> | |||||||||||||||||||
</dependency> |
配置掃描文件路徑:
@MapperScan(value = {"com.baomidou.mybatisplus.samples.quickstart.mapper"}) | |||||||||||||||||||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> | |||||||||||||||||||
<property name="basePackage" value="com.baomidou.mybatisplus.samples.quickstart.springmvc.mapper"/> | |||||||||||||||||||
</bean> |
配置mapers文件加載路徑:
mybatis-plus.mapper-locations=classpath*:mappers/*.xml |
通過XML文件配置屬性:
需要添加加載配置文件註解:@ContextConfiguration(locations = {"classpath*:config.xml"}) 以下是config.xml的內容: | |||||||||||||||||||
<!-- H2 Data Source Config 可以是mysql、h2database等 --> | |||||||||||||||||||
<bean id="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" > | |||||||||||||||||||
<property name="databaseName" value=""/> | |||||||||||||||||||
<property name="url" value=""/> | |||||||||||||||||||
<property name="username" value=""/> | |||||||||||||||||||
<property name="password" value=""/> | |||||||||||||||||||
</bean> | |||||||||||||||||||
<!-- SqlSessionFactory Config --> | |||||||||||||||||||
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> | |||||||||||||||||||
<property name="dataSource" ref="dataSource"/> | |||||||||||||||||||
<property name="mapperLocations" value="classpath:mappers/*.xml"/> | |||||||||||||||||||
以下非必填配置 | |||||||||||||||||||
<property name="globalConfig" ref="globalConfig"/> | |||||||||||||||||||
<property name="plugins"> | |||||||||||||||||||
<array> | |||||||||||||||||||
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></bean> | |||||||||||||||||||
</array> | |||||||||||||||||||
</property> | |||||||||||||||||||
</bean> | |||||||||||||||||||
<bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig"> | |||||||||||||||||||
<property name="dbConfig" ref="dbConfig"/> | |||||||||||||||||||
</bean> | |||||||||||||||||||
<bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig"> | |||||||||||||||||||
<property name="keyGenerator" ref="keyGenerator"/> | |||||||||||||||||||
</bean> | |||||||||||||||||||
<bean id="keyGenerator" class="com.baomidou.mybatisplus.extension.incrementer.H2KeyGenerator"/> |
簡單調用示例:
聲明Mapper | |||||||||||||||||||
<mapper namespace="com.quickstart.mapper.UserMapper"> | |||||||||||||||||||
<select id="selectMaxAge" resultType="java.lang.Integer"> | |||||||||||||||||||
select max(age) from person | |||||||||||||||||||
</select> | |||||||||||||||||||
</mapper> | |||||||||||||||||||
@Mapper | |||||||||||||||||||
public interface UserMapper extends BaseMapper<User> {Integer selectMaxAge();} | |||||||||||||||||||
執行Mapper | |||||||||||||||||||
@Resource | |||||||||||||||||||
private UserMapper userMapper; | |||||||||||||||||||
@Test | |||||||||||||||||||
public void testSelect() { | |||||||||||||||||||
List<User> userList = userMapper.selectList(null); | |||||||||||||||||||
Integer integer = userMapper.selectMaxAge(); | |||||||||||||||||||
} |
聲明Server | |||||||||||||||||||
@Service | |||||||||||||||||||
public interface UserServer extends IService<User> {List listAll();} | |||||||||||||||||||
@Service | |||||||||||||||||||
public class UserServerImpl extends ServiceImpl<UserMapper , User> implements UserServer { | |||||||||||||||||||
@Resource | |||||||||||||||||||
CustomMapper customMapper; | |||||||||||||||||||
@Override | |||||||||||||||||||
public List listAll() { | |||||||||||||||||||
return customMapper.selectList(null); | |||||||||||||||||||
} | |||||||||||||||||||
} | |||||||||||||||||||
執行Service | |||||||||||||||||||
@Autowired | |||||||||||||||||||
UserServer userServer; | |||||||||||||||||||
public List test() { | |||||||||||||||||||
userServer.selectList(null); | |||||||||||||||||||
userServer.listAll(); | |||||||||||||||||||
} |
代碼生成器簡介
MyBatis-Plus 從 3.0.3 之後移除了代碼生成器與模板引擎的默認依賴,需要手動添加相關依賴:添加 代碼生成器 依賴 | |||||||||||||||||||
<dependency> | |||||||||||||||||||
<groupId>com.baomidou</groupId> | |||||||||||||||||||
<artifactId>mybatis-plus-generator</artifactId> | |||||||||||||||||||
<version>3.3.1.tmp</version> | |||||||||||||||||||
</dependency> | |||||||||||||||||||
#Velocity(默認) | |||||||||||||||||||
添加 模板引擎 依賴,MyBatis-Plus 支持 Velocity(默認)、Freemarker、Beetl,用戶可以選擇自己熟悉的模板引擎,如果都不滿足您的要求,可以採用自定義模板引擎。 | |||||||||||||||||||
<dependency> | |||||||||||||||||||
<groupId>org.apache.velocity</groupId> | |||||||||||||||||||
<artifactId>velocity-engine-core</artifactId> | |||||||||||||||||||
<version>2.2</version> | |||||||||||||||||||
</dependency> | |||||||||||||||||||
#Freemarker | |||||||||||||||||||
<dependency> | |||||||||||||||||||
<groupId>org.freemarker</groupId> | |||||||||||||||||||
<artifactId>freemarker</artifactId> | |||||||||||||||||||
<version>2.3.30</version> | |||||||||||||||||||
</dependency> | |||||||||||||||||||
#Beetl | |||||||||||||||||||
<dependency> | |||||||||||||||||||
<groupId>com.ibeetl</groupId> | |||||||||||||||||||
<artifactId>beetl</artifactId> | |||||||||||||||||||
<version>3.1.3.RELEASE</version> | |||||||||||||||||||
</dependency> |
// 代碼生成器 | |||||||||||||||||||
AutoGenerator mpg = new AutoGenerator(); | |||||||||||||||||||
// 全局配置 | |||||||||||||||||||
GlobalConfig gc = new GlobalConfig(); | |||||||||||||||||||
//當前文件夾 | |||||||||||||||||||
String projectPath = System.getProperty("user.dir"); | |||||||||||||||||||
gc.setOutputDir(projectPath + "/mybatis-plus-sample-generator/src/main/java"); | |||||||||||||||||||
gc.setAuthor("Arnold"); | |||||||||||||||||||
// 實體類是否添加Swagger2註解 | |||||||||||||||||||
// gc.setSwagger2(true); | |||||||||||||||||||
gc.setOpen(false); | |||||||||||||||||||
//默認不覆蓋,如果文件存在,將不會再生成,配置true就是覆蓋 | |||||||||||||||||||
gc.setFileOverride(true); | |||||||||||||||||||
mpg.setGlobalConfig(gc); | |||||||||||||||||||
// 數據源配置 | |||||||||||||||||||
DataSourceConfig dsc = new DataSourceConfig(); | |||||||||||||||||||
dsc.setUrl("jdbc:mysql://localhost:3306/activity?characterEncoding=utf8&serverTimezone=UTC"); | |||||||||||||||||||
dsc.setDriverName("com.mysql.jdbc.Driver"); | |||||||||||||||||||
dsc.setUsername("root"); | |||||||||||||||||||
dsc.setPassword("root"); | |||||||||||||||||||
mpg.setDataSource(dsc); | |||||||||||||||||||
// 包配置 | |||||||||||||||||||
PackageConfig pc = new PackageConfig(); | |||||||||||||||||||
pc.setModuleName(scanner("模塊名")); | |||||||||||||||||||
pc.setParent("com.baomidou.mybatisplus.samples.generator"); | |||||||||||||||||||
mpg.setPackageInfo(pc); | |||||||||||||||||||
// 自定義屬性注入 | |||||||||||||||||||
InjectionConfig cfg = new InjectionConfig() { | |||||||||||||||||||
@Override public void initMap() { // to do nothing} | |||||||||||||||||||
}; | |||||||||||||||||||
List<FileOutConfig> focList = new ArrayList<>(); | |||||||||||||||||||
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { | |||||||||||||||||||
@Override public String outputFile(TableInfo tableInfo) { | |||||||||||||||||||
// 自定義輸入文件名稱 | |||||||||||||||||||
return projectPath + "/mybatis-plus-sample-generator/src/main/resources/mapper/" + pc.getModuleName() | |||||||||||||||||||
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; | |||||||||||||||||||
} }); | |||||||||||||||||||
cfg.setFileOutConfigList(focList); | |||||||||||||||||||
mpg.setCfg(cfg); | |||||||||||||||||||
mpg.setTemplate(new TemplateConfig().setXml(null)); | |||||||||||||||||||
// 策略配置 | |||||||||||||||||||
StrategyConfig strategy = new StrategyConfig(); | |||||||||||||||||||
strategy.setNaming(NamingStrategy.underline_to_camel); | |||||||||||||||||||
strategy.setColumnNaming(NamingStrategy.underline_to_camel); | |||||||||||||||||||
// strategy.setSuperEntityClass("com.xxx.BaseEntity") 實體類的父類沒有則不寫; | |||||||||||||||||||
strategy.setEntityLombokModel(true); | |||||||||||||||||||
// strategy.setSuperControllerClass("com.xxx.BaseController")Controller的父類沒有則不寫; | |||||||||||||||||||
strategy.setInclude(scanner("表名")); | |||||||||||||||||||
strategy.setSuperEntityColumns("id"); | |||||||||||||||||||
strategy.setControllerMappingHyphenStyle(true); | |||||||||||||||||||
strategy.setTablePrefix(pc.getModuleName() + "_"); | |||||||||||||||||||
mpg.setStrategy(strategy); | |||||||||||||||||||
// 選擇 freemarker 引擎需要指定如下加,注意 pom 依賴必須有! | |||||||||||||||||||
mpg.setTemplateEngine(new FreemarkerTemplateEngine()); | |||||||||||||||||||
mpg.execute(); | |||||||||||||||||||
public static String scanner(String tip) { | |||||||||||||||||||
Scanner scanner = new Scanner(System.in); | |||||||||||||||||||
StringBuilder help = new StringBuilder(); | |||||||||||||||||||
help.append("請輸入" + tip + ":"); | |||||||||||||||||||
System.out.println(help.toString()); | |||||||||||||||||||
if (scanner.hasNext()) { | |||||||||||||||||||
String ipt = scanner.next(); | |||||||||||||||||||
if (StringUtils.isNotEmpty(ipt)) { | |||||||||||||||||||
return ipt; | |||||||||||||||||||
} | |||||||||||||||||||
} | |||||||||||||||||||
throw new MybatisPlusException("請輸入正確的" + tip + "!"); | |||||||||||||||||||
} |