1. Download SSM framework required jar package
2. Create a data table, the use of reverse engineering according Mybatis table generating entity classes (pojo), SQL mapping file (mapper.xml) and dynamic proxy interfaces (mapper.java)
(1) preparing a data table
(2) Create a Java project (name Custom)
(3) Download mybatis-generator-core-1.3.2-bundle.zip, unzip the file, the MyBatis-Generator-Core-1.3.2.jar , ojdbc.jar and Mybatis depend mybatis-3.3.1.jar added the project build directory (build path)
docs file index.html relevant knowledge generated automatically
(4) create and write configuration files in src
generator.xml
<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE generatorConfiguration the PUBLIC "- // mybatis.org//DTD MyBatis Generator 1.0 // EN the Configuration" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context ID = "My" targetRuntime = "MyBatis 3"> <commentGenerator> <Property name = "suppressAllComments" value = " to true " /> // automatic generation of entity classes, together with comments </ commentGenerator> <-! account password database link address -> <JdbcConnection driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver" the connectionURL = "JDBC: SQLServer: //192.168.10.104:1433;databaseName=sa"10.104:1433;databaseName=sa" userId="sa" password="123"/> <-! Model class generating storage location -> <javaModelGenerator targetPackage = " org.zy.Model " . "\ the src" = targetProject> <Property name = "enableSubPackages" = value "to true" /> <Property name = "trimStrings" value = " to true " /> // perform database query results of trim operation, delete the first string of blanks </ javaModelGenerator> <-! generate a map file location -> < sqlMapGenerator targetPackage = " org.zy.mapper " targetProject =. "\ src"> sql mapping file path where the package targetPackage:: targetPackage sql generate location mapping file generating a location map file sql </ sqlMapGenerator> <-! generated Mapper classes storage position -> <javaClientGenerator targetPackage="org.zy.mapper" targetProject=".\src" type="XMLMAPPER"> </javaClientGenerator> <table tableName="表1"</table>
<table tableName="表2"</table>
.....
</context>
</generatorConfiguration>
The following Java code execution
public class aa{ public static void main(String []args){ File f=new File("src/generator.xml"); List<String> warnings=new ArraytList<String>(); ConfigurationParser cp=new ConfigurationParser(warnings); Configuration config=cp.parseConfiguration(f); DefaultShellCallBack callBack=new DefaultShellCallBack(true); MyBatisGenerator generator=new MyBatisGenerator(config,callBack,warnings); generator.generate(null); } }
3. Configure Notes
@Component public class ElectricOrder{ //属性 ...属性的set和get体方法 } @Repository public class ElectricOrderDao { @Autowired(required =false) ElectricOrderMapper electricOrderMapper; } @Service public class ElectricOrderService { @Autowired(required =false) ElectricOrderDao electricOrderDao; } @Controller public class ElectricOrderService { @Autowired private ElectricOrderService electricOrderService;
spring configure the scanner
<! - automatic scanning, support the use of annotation mode, automatic assembly the bean -> <context: component- Scan Base - Package = "com.zy.mapper" />
<context:component-scan base-package="com.zy.dao" />
<context:component-scan base-package="com.zy.service" />
<context:component-scan base-package="com.zy.pojo" />