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);
    }
}

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