Skip to content

Commit

Permalink
feat(scaffold): suppport mysql and mybatis intergration
Browse files Browse the repository at this point in the history
  • Loading branch information
taccisum committed Jul 2, 2019
1 parent 0e94e98 commit 41febc5
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 1 deletion.
22 changes: 22 additions & 0 deletions generators/app/handler/application_yaml_th.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ class ApplicationYamlTemplateHandler extends AbstractTemplateHandler {
}
}

switch (this.props.db) {
case 'mysql': {
debug(`configure application-${env}.yaml for mysql`);
configurers.mysql.configureApplicationYaml(yamlDoc, env);
break;
}
default: {
break;
}
}

switch (this.props.orm) {
case 'mybatis': {
debug(`configure application-${env}.yaml for mybatis`);
configurers.mybatis.configureApplicationYaml(yamlDoc, env);
break;
}
default: {
break;
}
}

const destTpl = _.template(fileUtils.tmplToFileName(this.tmpl));
this.generator.fs.write(
this.generator.destinationPath(destTpl(this.props)),
Expand Down
31 changes: 31 additions & 0 deletions generators/app/handler/configurer/mybatis_configurer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const _ = require('lodash');

module.exports = {
key: 'mybatis',
fn: {
configureProviderPomDependencies (optionalDependencies) {
optionalDependencies.push({
dependency: [
{ groupId: 'org.mybatis.spring.boot' },
{ artifactId: 'mybatis-spring-boot-starter' },
{ version: '2.0.1' }
]
})
},
configureApplicationYaml (yaml, env) {
switch (env) {
case 'default': {
_.merge(yaml, {
mybatis: {
'mapper-locations': 'classpath:mapper/*'
}
});
break;
}
default: {
break;
}
}
}
}
}
41 changes: 41 additions & 0 deletions generators/app/handler/configurer/mysql_configurer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const _ = require('lodash');

module.exports = {
key: 'mysql',
fn: {
configureProviderPomDependencies (optionalDependencies) {
optionalDependencies.push({
dependency: [
{ groupId: 'mysql' },
{ artifactId: 'mysql-connector-java' }
]
})
},
configureApplicationYaml (yaml, env) {
switch (env) {
case 'default': {
_.merge(yaml, {
spring: {
datasource: {
'driver-class-name': 'com.mysql.jdbc.Driver'
}
}
});
break;
}
default: {
_.merge(yaml, {
spring: {
datasource: {
username: 'root',
password: 'root',
url: 'jdbc:mysql://127.0.0.1:3306/db_name'
}
}
});
break;
}
}
}
}
}
22 changes: 22 additions & 0 deletions generators/app/handler/provider_pom_th.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ class ProviderPomTemplateHandler extends AbstractTemplateHandler {
}
}

switch (this.props.db) {
case 'mysql': {
debug('configure provider pom dependencies for mysql');
configurers.mysql.configureProviderPomDependencies(optionalDependencies);
break;
}
default: {
break;
}
}

switch (this.props.orm) {
case 'mybatis': {
debug('configure provider pom dependencies for mybatis');
configurers.mybatis.configureProviderPomDependencies(optionalDependencies);
break;
}
default: {
break;
}
}

const tpl = _.template(this.generator.fs.read(this.generator.templatePath(this.tmpl)));
const content = prettifyXml(tpl(_.merge(
{}, this.props, {
Expand Down
51 changes: 50 additions & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,44 @@ const obj = {
],
message: '请选择你使用的数据库'
},
option: { desc: '数据库', type: String, default: 'none' }
option: { desc: '数据库', type: String, default: 'none' },
child: {
dbPool: {
prompting: {
type: 'list',
choices: [
'druid',
'hikari',
'c3p0',
'none'
],
message: '请选择你使用的数据库连接池'
},
option: { desc: '数据库连接池', type: String, default: 'none' },
callbacks: {
trigger (answers) {
return answers.db === 'mysql';
}
}
},
orm: {
prompting: {
type: 'list',
choices: [
'mybatis',
'mybatis-plus',
'none'
],
message: '请选择你使用的ORM框架'
},
option: { desc: 'ORM框架', type: String, default: 'none' },
callbacks: {
trigger (answers) {
return answers.db === 'mysql';
}
}
}
}
},
discovery: {
prompting: {
Expand All @@ -49,6 +86,13 @@ const obj = {
default: false
},
option: { desc: '生成demo', type: Boolean, default: false }
},
lombok: {
prompting: {
type: 'confirm',
message: '是否使用lombok开发(默认true)'
},
option: { desc: '使用lombok', type: Boolean, default: true }
}
// configservice: {
// prompting: {
Expand Down Expand Up @@ -94,5 +138,10 @@ module.exports = require('yo-power-generator').getGenerator(obj, {
props.basePackage = props.groupId;
}
props.basePath = props.basePackage.replace(/\./g, '/');

props.conditions = {};
if (props.orm) {
props.conditions[props.orm] = true;
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ${basePackage}.controller;

import ${basePackage}.domain.DemoDo;
import ${basePackage}.service.CrudDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("demo/mybatis")
public class CrudDemoController {
@Autowired
private CrudDemoService service;

@GetMapping("/")
public List<DemoDo> listAll() {
return service.listAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ${basePackage}.domain;

public class DemoDo {
private Long id;
private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ${basePackage}.mapper;

import ${basePackage}.domain.DemoDo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DemoMapper {
@Select("SELECT * FROM demo")
List<DemoDo> selectAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ${basePackage}.service;

import ${basePackage}.domain.DemoDo;

import java.util.List;

public interface CrudDemoService {
List<DemoDo> listAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ${basePackage}.service.impl;

import ${basePackage}.domain.DemoDo;
import ${basePackage}.mapper.DemoMapper;
import ${basePackage}.service.CrudDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CrudDemoServiceImpl implements CrudDemoService {
@Autowired
private DemoMapper mapper;

@Override
public List<DemoDo> listAll() {
return mapper.selectAll();
}
}
41 changes: 41 additions & 0 deletions generators/test/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,45 @@ describe('optional dependencies', () => {
assert(yaml.safeLoad(fs.readFileSync('foo-service-provider/src/main/resources/application-local.yml')).eureka.client);
});
});

describe('mysql', () => {
// it('should ', () => {

// });

describe('mybatis', () => {
before(() => {
return helpers
.run(path.join(__dirname, '../../app'))
.withPrompts({
groupId: 'com.deepexi',
artifactId: 'foo-service',
basePackage: 'com.deepexi.foo',
db: 'mysql',
orm: 'mybatis',
demo: true
})
.then(() => {
})
});

it('should have dependency', () => {
assert.fileContent([
['foo-service-provider/pom.xml', /<groupId>org.mybatis.spring.boot<\/groupId>/],
['foo-service-provider/pom.xml', /<artifactId>mybatis-spring-boot-starter<\/artifactId>/]
])
});

it('should have properties', () => {
});

it('should exist demo files', () => {
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/controller/CrudDemoController.java')
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/service/CrudDemoService.java')
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/service/impl/CrudDemoServiceImpl.java')
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/mapper/DemoMapper.java')
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/domain/DemoDo.java')
});
});
});
});

0 comments on commit 41febc5

Please sign in to comment.