ssm框架(一)
阿新 • • 發佈:2018-11-30
在進行ssm框架開發時,通過資料庫表反向生成資料庫操作程式碼。
步驟:
1、建立bean檔案。
2、建立資料庫和資料庫表。
3、建立mybatisgenerator.xml檔案,並修改相應程式碼:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 配置資料庫連線 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/infosystem?useSSL=false" userId="root" password="752340690"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定javaBean生成的位置 --> <javaModelGenerator targetPackage="com.atguigu.crud.bean" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--指定sql對映檔案生成的位置 --> <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 指定dao介面生成的位置,mapper介面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.crud.dao" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- table指定每個表的生成策略 --> <table tableName="tbl_category" domainObjectName="Category"></table> <table tableName="tbl_datadic" domainObjectName="DataDic"></table> <table tableName="tbl_menu" domainObjectName="Menu"></table> <table tableName="tbl_navigation" domainObjectName="Navigation"></table> <table tableName="tbl_role" domainObjectName="Role"></table> <table tableName="tbl_service" domainObjectName="Service"></table> <table tableName="tbl_user" domainObjectName="User"></table> </context> </generatorConfiguration>
4、生成資料庫操作程式碼。
package com.atguigu.crud.test; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.Reader; 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.internal.DefaultShellCallback; public class MBGTest { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("mybatisgenerator.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); } }