ssm框架(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_39611196/article/details/84571923

在进行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);
	}
}


 

猜你喜欢

转载自blog.csdn.net/github_39611196/article/details/84571923