国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

MyBatis入門(七)---逆向工程

2019-11-14 15:11:49
字體:
供稿:網(wǎng)友

一、逆向工程

1.1、概述

mybatis需要程序號自己編寫的SQL。

mybatis官方提供了逆向工程,可以針對單表自動生成mybatis執(zhí)行所需要的代碼

(mapper,java,maper.xml,po...)

一般都是由數(shù)據(jù)庫到java代碼, 的生成過程

二、導入jar包

2.1、mybatis-generator

 

 

三、配置xml

3.1、generatorConfig.xml

 

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration>    <!--指定數(shù)據(jù)庫jar包 -->    <classPathEntry location="G:/jar/MySQL-connector-java-5.1.37-bin.jar" />    <context id="DB2Tables" targetRuntime="MyBatis3">        <commentGenerator>            <!-- 是否去除自動生成的注釋 true:是 : false:否 -->            <PRoperty name="suppressAllComments" value="true" />        </commentGenerator>        <!--JDBC連接配置,driver,url,user,passWord -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver"            connectionURL="jdbc:mysql://localhost:3306/mybatis?character=utf8"            userId="root" password="root">        </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.mybatis.po"            targetProject="./src">            <property name="enableSubPackages" value="false" />            <property name="trimStrings" value="true" />        </javaModelGenerator>                <!-- targetProject:mapper映射文件生成的位置 -->        <sqlMapGenerator targetPackage="com.pb.mybatis.mapping"            targetProject="./src">            <!-- enableSubPackages:是否讓schema作為包的后綴 -->            <property name="enableSubPackages" value="false" />            <!-- 從數(shù)據(jù)庫返回的值被清理前后的空格 -->            <property name="trimStrings" value="true" />        </sqlMapGenerator>    <!-- targetPackage:mapper接口生成的位置 -->        <javaClientGenerator type="XMLMAPPER"            targetPackage="com.pb.mybatis.mapper" targetProject="./src">            <!-- enableSubPackages:是否讓schema作為包的后綴 -->            <property name="enableSubPackages" value="false" />        </javaClientGenerator>        <!--指定要生成的表  -->        <table tableName="author"></table>        <table tableName="blog"></table>        <table tableName="posts"></table>    </context></generatorConfiguration>

 

 

 

 

 

四、運行java程序生成

4.1、java程序

 

import java.io.File;import java.io.IOException;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.exception.XMLParserException;import org.mybatis.generator.internal.DefaultShellCallback;public class GeneratorSqlmap {    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);    }     public static void main(String[] args) throws Exception {        try {            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();            generatorSqlmap.generator();        } catch (Exception e) {            e.printStackTrace();        }            }}

 

 

五、測試

5.1、測試類

 

package com.pb.ssm.mapper;import static org.junit.Assert.fail;import java.util.Date;import java.util.List;import javax.crypto.Cipher;import org.junit.Before;import org.junit.Test;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.pb.ssm.po.Author;import com.pb.ssm.po.AuthorExample;import com.pb.ssm.po.AuthorExample.Criteria;public class AuthorMapperTest {    private ApplicationContext applicationContext;    private AuthorMapper authorMapper;    @Before    public void setUp() throws Exception {        applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");        authorMapper=(AuthorMapper) applicationContext.getBean("authorMapper");    }        //根據(jù)條件查詢記錄數(shù)    @Test    public void testCountByExample() {        AuthorExample example=new AuthorExample();        //可以加條件,不加條件默認查詢?nèi)?/span>        Criteria criteria=example.createCriteria();        //加條件,介紹不是空的        criteria.andAuthorBioIsNotNull();        int num=authorMapper.countByExample(example);        System.out.println("num="+num);    }        //根據(jù)條件刪除    @Test    public void testDeleteByExample() {        AuthorExample example=new AuthorExample();        //可以加條件,不加條件默認查詢?nèi)?/span>        Criteria criteria=example.createCriteria();        criteria.andAuthorUsernameEqualTo("程序員");        int num=authorMapper.deleteByExample(example);        System.out.println("num="+num);            }        //根據(jù)主鍵ID刪除    @Test    public void testDeleteByPrimaryKey() {        int num=authorMapper.deleteByPrimaryKey(18);        System.out.println("num="+num);    }    //插入    @Test    public void testInsert() {                Author author=new Author();        author.setAuthorUsername("再測試一下");        author.setAuthorPassword("admin123");        author.setAuthorEmail("admin1234@QQ.com");                        int num=authorMapper.insert(author);        System.out.println("num="+num);        //這個方法插入,默認不會把數(shù)據(jù)庫自增加ID返回,如果需要,可以手動增加        System.out.println("插入后的ID"+author.getAuthorId());            }    //插入    @Test    public void testInsertSelective() {        Author author=new Author();        author.setAuthorUsername("再測試一下");        author.setAuthorPassword("admin123");        author.setAuthorEmail("admin1234@qq.com");        author.setRegisterTime(new Date());                        int num=authorMapper.insert(author);        System.out.println("num="+num);        //這個方法插入,默認不會把數(shù)據(jù)庫自增加ID返回,如果需要,可以手動增加        System.out.println("插入后的ID"+author.getAuthorId());            }    //自定義 條件查詢    @Test    public void testSelectByExample() {        //聲明一個對象        AuthorExample authorExample=new AuthorExample();        //創(chuàng)建criteria對象添加條件 and 連接        Criteria criteria=authorExample.createCriteria();        //需要手動加%        criteria.andAuthorUsernameLike("%張三%");                List<Author> list=authorMapper.selectByExample(authorExample);        System.out.println(list.size());    }        //根據(jù)主鍵ID查詢    @Test    public void testSelectByPrimaryKey() {         Author author= authorMapper.selectByPrimaryKey(6);        System.out.println(author.getAuthorUsername()+"..."+author.getAuthorBio());    }    @Test    public void testUpdateByExampleSelective() {        fail("Not yet implemented");    }    @Test    public void testUpdateByExample() {        fail("Not yet implemented");    }    @Test    public void testUpdateByPrimaryKeySelective() {        fail("Not yet implemented");    }    @Test    public void testUpdateByPrimaryKey() {        fail("Not yet implemented");    }}

 

 

 

 

 

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 留坝县| 乐亭县| 宝鸡市| 阳新县| 泰安市| 景洪市| 沛县| 辽中县| 乐东| 故城县| 漳浦县| 滨海县| 玉环县| 婺源县| 正阳县| 嘉祥县| 乾安县| 周宁县| 舞阳县| 凤城市| 咸阳市| 蓬安县| 毕节市| 开远市| 佳木斯市| 监利县| 林甸县| 新巴尔虎左旗| 四会市| 芜湖县| 祁东县| 蛟河市| 宣威市| 海宁市| 巫山县| 永靖县| 高台县| 汉阴县| 靖边县| 武鸣县| 青田县|