配置基础ObjectSQLManager和SQLService
package com.objectsql.demo; import com.objectsql.ObjectSQLManager; import com.objectsql.SQLService; import com.objectsql.SQLServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class ObjectSQLConfig { @Bean public ObjectSQLManager objectSQLManager(DataSource dataSource){ return new ObjectSQLManager(dataSource); } @Bean public SQLService sqlService(ObjectSQLManager objectSQLManager){ return new SQLServiceImpl(objectSQLManager); } }
执行测试用例,生成entity以及service代码
package com.objectsql.demo; import com.objectsql.SQLService; import com.objectsql.generator.CodeGenerator; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class DemoApplicationTests { @Autowired private SQLService sqlService; @Test void contextLoads() { String code = CodeGenerator.generateEntity("T_TEST", sqlService, true, null, null); System.out.println(code); } }
生成的代码(含有service)
import com.objectsql.annotation.RdColumn; import com.objectsql.annotation.RdId; import com.objectsql.annotation.RdTable; import java.io.Serializable; /** IService: import com.objectsql.BaseService; public interface ITestService extends BaseService<Test> { } ServiceImplement: import com.objectsql.BaseServiceImpl; import com.objectsql.ObjectSQLManager; public class TestServiceImpl extends BaseServiceImpl<Test> implements ITestService { public TestServiceImpl(ObjectSQLManager objectSQLManager){ super(objectSQLManager); } } */ @RdTable(name = "T_TEST", comment = "测试") public class Test implements Serializable { private static final long serialVersionUID = 1L; /** 编号;自增数字 */ public static final String T_ID = "ID"; /** 名称;姓名 */ public static final String T_NAME = "NAME"; @RdId @RdColumn(name = T_ID, title = "编号", description = "自增数字", precision = 10, order = 10, nullable = false) private Integer id; @RdColumn(name = T_NAME, title = "名称", description = "姓名", length = 100, order = 20) private String name; public Integer getId(){ return this.id; } public void setId(Integer id){ this.id = id; } public String getName(){ return this.name; } public void setName(String name){ this.name = name; } }
import com.objectsql.BaseService; public interface ITestService extends BaseService<Test> { }
import com.objectsql.BaseServiceImpl; import com.objectsql.ObjectSQLManager; public class TestServiceImpl extends BaseServiceImpl<Test> implements ITestService { public TestServiceImpl(ObjectSQLManager objectSQLManager){ super(objectSQLManager); } }
最终将service加入配置
import com.objectsql.ObjectSQLManager; import com.objectsql.SQLService; import com.objectsql.SQLServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class ObjectSQLConfig { @Bean public ObjectSQLManager objectSQLManager(DataSource dataSource){ return new ObjectSQLManager(dataSource); } @Bean public SQLService sqlService(ObjectSQLManager objectSQLManager){ return new SQLServiceImpl(objectSQLManager); } @Bean public ITestService testService(ObjectSQLManager objectSQLManager){ return new TestServiceImpl(objectSQLManager); } }