mybatis學習筆記(18)-mybatis逆向工程

mybatis學習筆記(18)-mybatis逆向工程

標籤: mybatishtml


[TOC]java


mybaits須要程序員本身編寫sql語句,mybatis官方提供逆向工程,能夠針對單表自動生成mybatis執行所須要的代碼(mapper.java,mapper.xml、po..)mysql

企業實際開發中,經常使用的逆向工程方式:由數據庫的表生成java代碼。git

先附上官網連接:程序員

下載逆向工程

這裏其實能夠添加Maven依賴的,由於跟着視頻作的,因此我就建了個普通工程,直接添加了個lib文件夾,把要用的jar包直接copy進來了。github

maven中央倉庫MyBatis-Generator下載地址:【MyBatis Generator Core】spring

使用方法

運行逆向工程

根據官網說的(Running MyBatis Generator)sql

Running MyBatis Generator數據庫

MyBatis Generator (MBG) can be run in the following ways:mybatis

  • From the command prompt with an XML configuration
  • As an Ant task with an XML configuration
  • As a Maven Plugin
  • From another Java program with an XML configuration
  • From another Java program with a Java based configuration

還能夠經過eclipse的插件生成代碼

建議使用java程序方式,不依賴開發工具。

生成代碼配置文件

<?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">
		<commentGenerator>
			<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--數據庫鏈接的信息:驅動類、鏈接地址、用戶名、密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://120.25.162.238:3306/mybatis001?characterEncoding=utf-8" 
			userId="root"
			password="123">
		</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="com.iot.ssm.po"
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema做爲包的後綴 -->
			<property name="enableSubPackages" value="false" />
			<!-- 從數據庫返回的值被清理先後的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.iot.ssm.mapper" 
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema做爲包的後綴 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.iot.ssm.mapper" 
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema做爲包的後綴 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定數據庫表 -->
		<table tableName="items"></table>
		<table tableName="orders"></table>
		<table tableName="orderdetail"></table>
		<table tableName="user"></table>
		<!-- <table schema="" tableName="sys_user"></table>
		<table schema="" tableName="sys_role"></table>
		<table schema="" tableName="sys_permission"></table>
		<table schema="" tableName="sys_user_role"></table>
		<table schema="" tableName="sys_role_permission"></table> -->
		
		<!-- 有些表的字段須要指定java類型
		 <table schema="" tableName="">
			<columnOverride column="" javaType="" />
		</table> -->
	</context>
</generatorConfiguration>

須要注意的位置:

  • javaModelGenerator,生成PO類的位置
  • sqlMapGenerator,mapper映射文件生成的位置
  • javaClientGenerator,mapper接口生成的位置
  • table,指定數據庫表

執行生成程序

public void generator() throws Exception{

	List<String> warnings = new ArrayList<String>();
	boolean overwrite = true;
	//指定逆向工程配置文件
	File configFile = new File("generatorConfig.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);

}

日誌輸出:

2016-02-27 16:29:46,419 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "items"
2016-02-27 16:29:46,477 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "mybatis001..items"
2016-02-27 16:29:46,477 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "name", data type 12, in table "mybatis001..items"
2016-02-27 16:29:46,477 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "price", data type 7, in table "mybatis001..items"
2016-02-27 16:29:46,477 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "detail", data type -1, in table "mybatis001..items"
2016-02-27 16:29:46,477 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "pic", data type 12, in table "mybatis001..items"
2016-02-27 16:29:46,478 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type 93, in table "mybatis001..items"
2016-02-27 16:29:46,503 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orders"
2016-02-27 16:29:46,551 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "mybatis001..orders"
2016-02-27 16:29:46,551 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "user_id", data type 4, in table "mybatis001..orders"
2016-02-27 16:29:46,551 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "number", data type 12, in table "mybatis001..orders"
2016-02-27 16:29:46,551 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type 93, in table "mybatis001..orders"
2016-02-27 16:29:46,551 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "note", data type 12, in table "mybatis001..orders"
2016-02-27 16:29:46,577 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orderdetail"
2016-02-27 16:29:46,630 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "mybatis001..orderdetail"
2016-02-27 16:29:46,630 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "orders_id", data type 4, in table "mybatis001..orderdetail"
2016-02-27 16:29:46,631 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_id", data type 4, in table "mybatis001..orderdetail"
2016-02-27 16:29:46,631 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_num", data type 4, in table "mybatis001..orderdetail"
2016-02-27 16:29:46,656 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "user"
2016-02-27 16:29:46,706 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "mybatis001..user"
2016-02-27 16:29:46,706 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "username", data type 12, in table "mybatis001..user"
2016-02-27 16:29:46,706 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "birthday", data type 91, in table "mybatis001..user"
2016-02-27 16:29:46,706 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "sex", data type 1, in table "mybatis001..user"
2016-02-27 16:29:46,706 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "address", data type 12, in table "mybatis001..user"

生成後的代碼:

逆向工程

使用生成的代碼

須要將生成工程中所生成的代碼拷貝到本身的工程中。

測試ItemsMapper中的方法

package com.iot.ssm.mapper;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.List;

import com.iot.ssm.po.Items;
import com.iot.ssm.po.ItemsExample;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class ItemsMapperTest {

	private ApplicationContext applicationContext;
	
	private ItemsMapper itemsMapper;

	//在setUp這個方法獲得spring容器
	@Before
	public void setUp() throws Exception {
		applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
	}

	//根據主鍵刪除 
	@Test
	public void testDeleteByPrimaryKey() {
		
	}

	//插入
	@Test
	public void testInsert() {
		//構造 items對象
		Items items = new Items();
		items.setName("手機");
		items.setPrice(999f);
		items.setCreatetime(new Date());
		itemsMapper.insert(items);
	}

	//自定義條件查詢
	@Test
	public void testSelectByExample() {
		ItemsExample itemsExample = new ItemsExample();
		//經過criteria構造查詢條件
		ItemsExample.Criteria criteria = itemsExample.createCriteria();
		criteria.andNameEqualTo("筆記本");
		//可能返回多條記錄
		List<Items> list = itemsMapper.selectByExample(itemsExample);
		
		System.out.println(list);
		
	}

	//根據主鍵查詢
	@Test
	public void testSelectByPrimaryKey() {
		Items items = itemsMapper.selectByPrimaryKey(1);
		System.out.println(items);
	}

	//更新數據
	@Test
	public void testUpdateByPrimaryKey() {
		
		//對全部字段進行更新,須要先查詢出來再更新
		Items items = itemsMapper.selectByPrimaryKey(1);
		
		items.setName("手機");
		
		itemsMapper.updateByPrimaryKey(items);
		//若是傳入字段不空爲才更新,在批量更新中使用此方法,不須要先查詢再更新
		//itemsMapper.updateByPrimaryKeySelective(record);
		
	}

}

做者@brianway更多文章:我的網站 | CSDN | oschina

相關文章
相關標籤/搜索