Freemarker how to use template technology exists to automatically generate the database table corresponding domain!

 The basic steps freemarker use template technology:   

    freemarker: is a popular technique [template] ftl
      1. guide packet (FreeMarker is a small frame)
      2. Create a configuration object the Configuration (plus version)
      3. Set loading path
      4. character set (default)
      5. create a template (ftl prepare a template)
      6. prepare data (Map, Object)
      7. data + template = output file (Writer)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Below is a table with the presence of freemarker template generation database technology base case of the domain:

@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration ( "CLASSPATH: the applicationContext.xml")
public class CreateDomainTest {

@Autowired
Private the DataSource the dataSource;

String tabalName = "t_address"; // table generated here by way of example t_address existing database. in case

Need to generate another table, the table name can be changed.
String className = "Address"; // here is generated domain class name. Generating a time table can be other, into a table name corresponding to the class name.

@Test
public void testCreate () throws Exception {
// get the linked object database
Connection Connection = dataSource.getConnection ();
// need to do to prepare the line sql statement
String sql = "from the SELECT *" + tabalName;
// execute ready good sql statement
the PreparedStatement Connection.prepareStatement PS = (sql);
// get the result sets
the resultSet ps.executeQuery RS = ();
// get the raw data parameter
the ResultSetMetaData rs.getMetaData the metaData = ();
// get the current table the number of fields
int = metaData.getColumnCount COUNT ();
// loop through the collection in the appropriate fields, and has prepared a list of installed field we get
List <the Map> list = new new ArrayList <> ();
for ( int i = 1; i <= count; i ++) {
// getColumnClassName get our field types in the table, replaceFirst types of prefixes and to remove the
String type = metaData.getColumnClassName (I) .replaceFirst (, "" "the java.lang.");
// get the fields in the table name field type java.lang.
String name = metaData.getColumnName (I);
// the name of the first letter capitalized first letters of the name
// the substring this field is taken, from that index to the next end labeled, toUpperCase becomes the first letter capitalized
String upperName name.substring = (0,. 1) .toUpperCase () name.substring + (. 1);
the HashMap <String, Object> = new new Map the HashMap <> ();
map.put ( "type", type);
map.put ( "name", name);
map.put ( "upperName", upperName);
List.add (Map);
}

// a ready configuration object
configuration cfg = new configuration (Configuration.VERSION_2_3_28);
// load path in setting a template,
// Directory:Catalog Template: Template Loading: Load
String path = "F: / Navicat Premium 11.1 / software / eclipse / eclipse-workspace-Spring / cms / src / main / webapp / template"; // this address is put domain.java template file path
cfg.setDirectoryForTemplateLoading (new File (path));
// set the template character set
cfg.setDefaultEncoding ( "UTF-. 8");
// get a template object
template template cfg.getTemplate = ( "domain.java");
// prepare the appropriate data ( map, the object)
map the HashMap new new Datamap = <> ();
dataMap.put ( "className", className);
dataMap.put ( "filedList", List);

// build file
// data (dataMap) + template (template ) = output file (OUT)
file = newFile new new file (path, className +. "Java");
FileWriter FileWriter new new OUT = (newFile);
template.process (Datamap, OUT);
out.flush ();
}
}

 

---------------------------------------------------------------------------------------------------------------------------------------

domain.java template content:

public class ${className} {

<#list filedList as filed>
private ${filed.type} ${filed.name} ;
</#list>


<#list filedList as filed>
public ${filed.type} get${filed.upperName}() {
return ${filed.name};
}
public void set${filed.upperName}(${filed.type} ${filed.name}) {
this.${filed.name} = ${filed.name};
}
</#list>

}

Guess you like

Origin www.cnblogs.com/DarryZz04/p/10939551.html