MyBatis entry (7) --- reverse engineering, mybatis Reverse Engineering
I. Reverse Engineering
1.1 Overview
Mybatis requires the SQL code compiled by the program number.
Mybatis provides reverse engineering to automatically generate the Code required to execute mybatis for a single table.
(Mapper, java, maper. xml, po ...)
Generally, the process is generated from the database to the java code.
Ii. Import the jar package
2.1. mybatis-generator
3. Configure xml
3.1. generatorConfig. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE generatorConfigurationPUBLIC "-// mybatis.org//DTD MyBatis Generator Configuration 1.0 // EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <! -- Specify the database jar package --> <classPathEntry location = "G:/jar/mysql-connector-java-5.1.37-bin.jar"/> <context id = "DB2Tables" targetRuntime = "MyBatis3"> <commentGenerator> <! -- Whether to remove automatically generated comments true: Yes: false: No --> <property name = "suppressAllComments" value = "true"/> </commentGenerator> <! -- JDBC connection configuration, driver, url, user, password --> <jdbcConnection driverClass = "com. mysql. jdbc. driver "connectionURL =" jdbc: mysql: // localhost: 3306/mybatis? Character = utf8 "userId =" root "password =" root "> </jdbcConnection> <! -- The default value is false. The jdbc decimal and NUMERIC types are parsed to Integer. If the value is true, the jdbc decimal and NUMERIC types are parsed to java. math. bigDecimal --> <javaTypeResolver> <property name = "forceBigDecimals" value = "false"/> </javaTypeResolver> <! -- TargetProject: location where the PO class is generated --> <javaModelGenerator targetPackage = "com. mybatis. po "targetProject = ". \ src "> <property name =" enableSubPackages "value =" false "/> <property name =" trimStrings "value =" true "/> </javaModelGenerator> <! -- TargetProject: location where the mapper ing file is generated --> <sqlMapGenerator targetPackage = "com. pb. mybatis. mapping" targetProject = ". \ src"> <! -- EnableSubPackages: whether to use schema as the package suffix --> <property name = "enableSubPackages" value = "false"/> <! -- Space before and after the value returned from the database --> <property name = "trimStrings" value = "true"/> </sqlMapGenerator> <! -- TargetPackage: Location generated by the ER interface --> <javaClientGenerator type = "XMLMAPPER" targetPackage = "com. pb. mybatis. mapper" targetProject = ". \ src"> <! -- EnableSubPackages: whether to use schema as the package suffix --> <property name = "enableSubPackages" value = "false"/> </javaClientGenerator> <! -- Specify the table to be generated --> <table tableName = "author"> </table> <table tableName = "blog"> </table> <table tableName = "posts"> </table> </context> </generatorConfiguration>
4. run java program generation
4.1 java program
Import java. io. file; import java. io. IOException; import java. util. arrayList; import java. util. list; import org. mybatis. generator. api. myBatisGenerator; import org. mybatis. generator. config. configuration; import org. mybatis. generator. config. xml. configurationParser; import org. mybatis. generator. exception. XMLParserException; import org. mybatis. generator. internal. defaultShellCallback; public class GeneratorSqlmap {public void generator () throws Exception {List <String> warnings = new ArrayList <String> (); boolean overwrite = true; // specify the reverse engineering configuration File configFile = new File ("generatorConfig. xml "); ConfigurationParser cp = new ConfigurationParser (warnings); Configuration config = cp. parseConfiguration (configFile); DefaultShellCallback callback = new DefaultShellCallback (overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator (config, callback, warnings); myBatisGenerator. generate (null);} public static void main (String [] args) throws Exception {try {GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap (); generatorSqlmap. generator ();} catch (Exception e) {e. printStackTrace ();}}}
V. Test
5.1 Testing
Package com. pb. ssm. mapper; import static org. junit. assert. fail; import java. util. date; import java. util. list; import javax. crypto. cipher; import org. junit. before; import org. junit. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import com. pb. ssm. po. author; import com. pb. ssm. po. authorExample; import com. pb. ssm. po. authorExample. criteria; public class AuthorMapperTest {private ApplicationContext applicationContext; private AuthorMapper authorMapper; @ Before public void setUp () throws Exception {applicationContext = new ClassPathXmlApplicationContext ("ApplicationContext. xml "); authorMapper = (AuthorMapper) applicationContext. getBean ("authorMapper");} // query the number of records by condition @ Test public void testCountByExample () {AuthorExample example = new AuthorExample (); // you can add conditions, by default, all Criteria criteria = example are queried. createCriteria (); // Add a condition to describe criteria. andAuthorBioIsNotNull (); int num = authorMapper. countByExample (example); System. out. println ("num =" + num);} // Delete @ Test public void testDeleteByExample () {AuthorExample example = new AuthorExample () according to the conditions, by default, all Criteria criteria = example are queried. createCriteria (); criteria. andauthorusernameto to ("programmer"); int num = authorMapper. deleteByExample (example); System. out. println ("num =" + num);} // Delete @ Test public void testDeleteByPrimaryKey () {int num = authorMapper according to the primary key ID. deleteByPrimaryKey (18); System. out. println ("num =" + num);} // insert @ Test public void testInsert () {Author author = new Author (); author. setAuthorUsername ("test again"); author. setAuthorPassword ("admin123"); author. setAuthorEmail ("admin1234@qq.com"); int num = authorMapper. insert (author); System. out. println ("num =" + num); // This method is inserted. The auto-increment Database ID is not returned by default. If necessary, you can manually add the System. out. println ("inserted ID" + author. getAuthorId ();} // insert @ Test public void testInsertSelective () {Author author = new Author (); author. setAuthorUsername ("test again"); author. setAuthorPassword ("admin123"); author. setAuthorEmail ("admin1234@qq.com"); author. setRegisterTime (new Date (); int num = authorMapper. insert (author); System. out. println ("num =" + num); // This method is inserted. The auto-increment Database ID is not returned by default. If necessary, you can manually add the System. out. println ("inserted ID" + author. getAuthorId ();} // custom condition query @ Test public void testSelectByExample () {// declare an object AuthorExample authorExample = new AuthorExample (); // create a criteria object to add conditions and connect Criteria criteria = authorExample. createCriteria (); // manually add % criteria. andAuthorUsernameLike ("% Zhang San %"); List <Author> list = authorMapper. selectByExample (authorExample); System. out. println (list. size ();} // query @ Test public void testSelectByPrimaryKey () {Author author = authorMapper. selectByPrimaryKey (6); System. out. println (author. getAuthorUsername () + "... "+ author. getAuthorBio () ;}@ Test public void testUpdateByExampleSelective () {fail ("Not yet implemented") ;}@ Test public void testUpdateByExample () {fail ("Not yet implemented") ;}@ Test public void testUpdateByPrimaryKeySelective () {fail ("Not yet implemented");} @ Test public void testUpdateByPrimaryKey () {fail ("Not yet implemented ");}}