Please indicate the source for reprinting:
As mentioned earlier: Spring+SpringMVC+MyBatis in-depth learning and construction (9)-MyBatis and Spring integration
Use the mapper of the official website to automatically generate The tool mybatis-generator-core-1.3.2 is used to generate po classes and mapper mapping files.
Mybatis requires programmers to write sql statements themselves. Mybatis officially provides reverse engineering that can automatically generate the code required for mybatis execution for a single table (mapper.java, mapper .xml, po...)
In actual enterprise development, the commonly used reverse engineering method is:
Generate java code from the database table.
In order to prevent later modification and expansion of the database table, Due to requirements modification and other reasons, the PO and mapper coverage automatically generated by the update are incorrect. We create a new specifically reverse-generated project generatorSqlmapCustom, and then copy the automatically generated po, mapper, etc. to the project as required.
It is recommended to use java program method without relying on development tools.
Configure the detailed information of mapper generation in generatorConfig.xml, pay attention to the following points:
(1) Add the database table to be generated ;
(2) The package path where the po file is located;
(3) The package path where the mapper file is located.
The configuration file is as follows:
public class GeneratorSqlmap {public void generator() throws Exception{ Listwarnings = new ArrayList ();boolean overwrite = true;//指定 逆向工程配置文件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(); } } }
Generated code:
Copy the Mapper.xml file to mapper Directory
Copy the Mapper.xml file to the mapper directory
Note: The mapper.xml file and the mapper.java file are in the same directory and The file names are the same.
= ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"=(ItemsMapper) applicationContext.getBean("itemsMapper" ="手机" =ItemsExample.Criteria criteria="笔记本3"Listlist= =itemsMapper.selectByPrimaryKey(1 Items items=itemsMapper.selectByPrimaryKey(1"水杯"