JavaEE學習日誌(一百一十三): 品優購項目準備,MyBatis逆向工程

項目工程結構

在這裏插入圖片描述

配置項目

創建目錄結構
在這裏插入圖片描述
創建數據庫表:省略

配置9個tomcat

在這裏插入圖片描述

idea優化

  1. 找到idea安裝目錄,我的是在D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.2\bin目錄中
  2. 找到idea64.exe.vmoptions文件
  3. 更改配置
-Xms512m
-Xmx1024m
-XX:ReservedCodeCacheSize=300m
-XX:SoftRefLRUPolicyMSPerMB=100

tomcat優化

給tomcat的內存越大,速度越快;內存越小,每次能夠啓動的tomcat越多

給每一個tomcat添加配置
在這裏插入圖片描述

-Xms128m -Xmx128m -XX:PermSize=64m -XX:MaxPermSize=128m

配置項目

一、父項目版本鎖定

<properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <spring.version>4.2.4.RELEASE</spring.version>
        <pagehelper.version>4.0.0</pagehelper.version>
        <servlet-api.version>2.5</servlet-api.version>
        <dubbo.version>2.8.4</dubbo.version>
        <zookeeper.version>3.4.7</zookeeper.version>
        <zkclient.version>0.1</zkclient.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <druid.version>1.0.9</druid.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
        <freemarker.version>2.3.23</freemarker.version>
        <activemq.version>5.11.2</activemq.version>
        <security.version>3.2.3.RELEASE</security.version>
        <solrj.version>4.10.3</solrj.version>
        <ik.version>2012_u6</ik.version>
    </properties>

    <!--依賴管理-->
    <dependencyManagement>
        <dependencies>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jms</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- dubbo相關 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.28</version>
            </dependency>
            <dependency>
                <groupId>javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.11.0.GA</version>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.10</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>
            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis.spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.miemiedev</groupId>
                <artifactId>mybatis-paginator</artifactId>
                <version>${mybatis.paginator.version}</version>
            </dependency>
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- 連接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>org.csource.fastdfs</groupId>
                <artifactId>fastdfs</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- 文件上傳組件 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${commons-fileupload.version}</version>
            </dependency>
            <!-- 緩存 -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.8.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.7.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>${freemarker.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-all</artifactId>
                <version>${activemq.version}</version>
            </dependency>
            <!-- 身份驗證 -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-web</artifactId>
                <version>4.1.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
                <version>4.1.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>2.3.2</version>
                <exclusions>
                    <exclusion>
                        <groupId>javax.servlet</groupId>
                        <artifactId>javax.servlet-api</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-cas</artifactId>
                <version>4.1.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.jasig.cas.client</groupId>
                <artifactId>cas-client-core</artifactId>
                <version>3.3.3</version>
                <!-- 排除log4j包衝突 -->
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>log4j-over-slf4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- solr客戶端 -->
            <dependency>
                <groupId>org.apache.solr</groupId>
                <artifactId>solr-solrj</artifactId>
                <version>${solrj.version}</version>
            </dependency>
            <dependency>
                <groupId>com.janeluo</groupId>
                <artifactId>ikanalyzer</artifactId>
                <version>${ik.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>4.4.4</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.3</version>
            </dependency>
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
            <dependency>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
                <version>1.4.01</version>
            </dependency>
        </dependencies>

    </dependencyManagement>

二、子項目最頂層項目common添加依賴

<dependencies>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 連接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <!-- 文件上傳組件 -->
        <dependency>
            <groupId>org.csource.fastdfs</groupId>
            <artifactId>fastdfs</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>

        <!-- 緩存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <!-- dubbo相關 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jasig.cas.client</groupId>
            <artifactId>cas-client-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>log4j-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- activeMq -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-client</artifactId>
            <version>5.13.4</version>
        </dependency>
        <!-- freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>
        <!-- github -->
        <dependency>
            <groupId>com.github.wxpay</groupId>
            <artifactId>wxpay-sdk</artifactId>
            <version>0.0.3</version>
        </dependency>
        <!-- solr -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
            <version>1.5.5.RELEASE</version>
        </dependency>

    </dependencies>

三、添加配置文件
省略

MyBatis逆向工程

mybatis官方推出逆向工程,用來連接數據庫,根據表結構生成pojo實體類,生成映射文件和接口文件,但只要單表的增刪改查。多表的增刪改查需要手動寫。

自動生成pojo和dao

注意:mybatis生成這些文件的方式是追加,不是覆蓋。
如果想要重新生成,需要將原先的文件全部刪除,然後重新運行,不然會造成文件損壞,無法使用。
jar包
在這裏插入圖片描述

逆向工程的配置文件

<?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="testTables" targetRuntime="MyBatis3">
	
		<!-- JavaBean 實現 序列化 接口 -->
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin">
		</plugin>
		<!-- genenat entity時,生成toString -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
        <!-- 自定義查詢指定字段  -->
        <plugin type="org.mybatis.generator.plugins.field.FieldsPlugin" />
        <!-- 開啓支持內存分頁   可生成 支持內存分佈的方法及參數  
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
        -->
        <!-- generate entity時,生成hashcode和equals方法-->
		<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
		 
		<!-- 此處是將Example改名爲Criteria 當然 想改成什麼都行~    -->      
        <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">  
	        <property name="searchString" value="Example$" />
	        <!-- 替換後
	        <property name="replaceString" value="Criteria" />  
	         -->
	        <property name="replaceString" value="Query" />
        </plugin>  
		<!-- 此處是將UserMapper.xml改名爲UserDao.xml 當然 想改成什麼都行~ --> 
        <plugin type="org.mybatis.generator.plugins.rename.RenameSqlMapperPlugin">  
	        <property name="searchString" value="Mapper" />
	        <property name="replaceString" value="Dao" />
        </plugin>  
		<!-- 此處是將UserMapper改名爲UserDao 接口 當然 想改成什麼都行~  -->   
        <plugin type="org.mybatis.generator.plugins.rename.RenameJavaMapperPlugin">  
	        <property name="searchString" value="Mapper$" />
	        <property name="replaceString" value="Dao" />
        </plugin>  
 
		
		<commentGenerator type="org.mybatis.generator.plugins.comment.MyCommentGenerator">
			<!-- 是否去除自動生成的註釋 true:是 : false:否 
			<property name="suppressAllComments" value="true" />
			-->
		</commentGenerator>
		
		<!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/pyg331" userId="root"
			password="root">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
			userId="yycg"
			password="yycg">
		</jdbcConnection> -->

		<!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer,爲 true時把JDBC DECIMAL 和 
			NUMERIC 類型解析爲java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>


		<!-- targetProject:生成PO類的位置 -->
		<javaModelGenerator targetPackage="cn.itcast.core.pojo"
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema作爲包的後綴 -->
			<property name="enableSubPackages" value="false" />
			<!-- 從數據庫返回的值被清理前後的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="cn.itcast.core.dao" 
			targetProject=".\resources">
			<!-- enableSubPackages:是否讓schema作爲包的後綴 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="cn.itcast.core.dao" 
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema作爲包的後綴 -->
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		

		<!-- 指定數據庫表 -->
		<table schema="" tableName="tb_order" domainObjectName="order.Order"/>
		<table schema="" tableName="tb_order_item" domainObjectName="order.OrderItem"/>
		<table schema="" tableName="tb_address" domainObjectName="address.Address"/>
		<table schema="" tableName="tb_areas" domainObjectName="address.Areas"/>
		<table schema="" tableName="tb_cities" domainObjectName="address.Cities"/>
		<table schema="" tableName="tb_provinces" domainObjectName="address.Provinces"/>
		<table schema="" tableName="tb_content" domainObjectName="ad.Content"/>
		<table schema="" tableName="tb_content_category" domainObjectName="ad.ContentCategory"/>
		<table schema="" tableName="tb_pay_log" domainObjectName="log.PayLog"/>
		<table schema="" tableName="tb_seller" domainObjectName="seller.Seller"/>
		<table schema="" tableName="tb_user" domainObjectName="user.User"/>
		<table schema="" tableName="tb_brand" domainObjectName="good.Brand"/>
		<table schema="" tableName="tb_goods" domainObjectName="good.Goods"/>
		<table schema="" tableName="tb_goods_desc" domainObjectName="good.GoodsDesc"/>
		<table schema="" tableName="tb_specification" domainObjectName="specification.Specification"/>
		<table schema="" tableName="tb_specification_option" domainObjectName="specification.SpecificationOption"/>
		<table schema="" tableName="tb_type_template" domainObjectName="template.TypeTemplate"/>
		<table schema="" tableName="tb_freight_template" domainObjectName="template.FreightTemplate"/>
		<table schema="" tableName="tb_item_cat" domainObjectName="item.ItemCat"/>
		<table schema="" tableName="tb_item" domainObjectName="item.Item"/>
		<table schema="" tableName="tb_seckill_goods" domainObjectName="seckill.SeckillGoods"/>
		<table schema="" tableName="tb_seckill_order" domainObjectName="seckill.SeckillOrder"/>
		
	</context>
</generatorConfiguration>

逆向工程執行的代碼:需要先創建對應的包

import java.io.File;
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 GeneratorSqlmap {

	public void generator() throws Exception{

		List<String> warnings = new ArrayList<>();
		boolean overwrite = true;

//		File configFile = new File("D:\\intellijWorkSpace\\pyg_parent\\generator\\generatorConfig.xml");
		File configFile = new File("generatorConfig.xml");
		System.out.println(configFile.getAbsolutePath());
		System.out.println(System.getProperty("user.dir"));
		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();
		}
		
	}

}

測試

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring/application*.xml"})
public class TestBrandDao {
    @Autowired
    private BrandDao brandDao;

    @Test
    public void testFindBrandById() {
        Brand brand = brandDao.selectByPrimaryKey(1L);
        System.out.println(brand);
    }

    @Test
    public void testFindBrandAll() {
        //查詢所有
        List<Brand> brands = brandDao.selectByExample(null);
        System.out.println(brands);
    }

    @Test
    public void testFindBrandByWhere() {
        //創建查詢對象
        BrandQuery brandQuery = new BrandQuery();
        //設置查詢的字段名,如果不寫默認爲*
        brandQuery.setFields("id,name");
        //不設置默認爲false,不去重
        brandQuery.setDistinct(true);
        //設置排序,不設置默認不排序
        brandQuery.setOrderByClause("id desc");

        //創建where查詢條件對象
        BrandQuery.Criteria criteria = brandQuery.createCriteria();
        //查詢id爲1的
        criteria.andIdEqualTo(1L);
        //根據name字段模糊查詢
        criteria.andNameLike("%想%");
        //根據首字母字段模糊查詢
        criteria.andFirstCharLike("%L%");
        List<Brand> brands = brandDao.selectByExample(brandQuery);
        System.out.println(brands);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章