一、逆向工程
1.1、概述
mybatis需要程式號碼自己寫的SQL。
mybatis官方提供了逆向工程,可以針對單表自動產生mybatis執行所需的程式碼
(mapper,java,maper.xml,po...)
一般都是由資料庫到java程式碼, 的生成流程
二、導入jar套件
2.1、mybatis-generator
三、設定xml
3.1、generatorConfig.xml
br/>PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 導入java.io.File;導入java.io.IOException;導入java.util.ArrayList;導入java.util.List;導入org.mybatis.generator.api.MyBatisGenerator;導入org.mybatis.generator.config.Configuration ;導入org.mybatis.generator.config.xml.ConfigurationParser;導入org.mybatis.generator.exception.XMLParserException;導入org.mybatis.generator.internal.DefaultShellCallback;公共類別中異常拋出一個異常異常值 5.1、測試類 package com.pb.ssm.mapper;導入靜態org.junit.Assert.fail;導入java.util.Date;導入java.util.List;導入javax.crypto.Cipher;導入org.junit.Before;導入org .junit.Test;導入org.springframework.context.applicationContext;導入org.springframework.context.support.ClassPathXmlApplicationContext;導入com.pb.ssm.po.Author;導入com.pb.ssm.po.AuthorExample;導入com. pb.ssm.po.AuthorExample.Criteria;公共類別AuthorMapperTest { 私有ApplicationContext applicationContext; private AuthorMapper horMapper;
"http://mybatis. org/dtd/mybatis-generator-config_1_0.dtd">
java
java . ator type="XMLMAPPER"
targetPackage="com.pb.mybatis.mapper" targetProject=".src">
table>
table>
table
列表警告= new ArrayList
File configFile = new File("gener) new ConfigurationParser(warnings );
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback 回呼= new DefaultShellCallback(overwion);
回調、警告);
myBatisGenerator.generate (null);
}
¢ public static void main(String[] args) throws Exception { try {
GeneratorSqlmap GeneratorSqlmap = neweratorSqlmap()。 ;
} catch (例外e) {
e. printStackTrace();
}
}
五、測試
applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
authorMapper=( AuthorMapper) applicationContext.getBean("authorMapper");
}
//根據條件查詢記錄數 @Test public void testCountByExample(cample( // 可加條件,不加條件預設查詢全部
Criteria criteria=example.createCriteria(); //加條件,介紹不是空的 criteria.andAuthorBioNotNull(); System.out.println("num="+num);
}
//依刪除條件 @Test public void testDeleteByExample() {
全部
Criteria criteria=example.createCriteria();
criteria. andAuthorUsernameEqualTo("程式設計師"); int num=authorMapper.deleteByExample(example);
System.out.println("num="+); //根據主鍵ID刪除 @Test public void testDeleteByPrimaryKey() { int num=author Mapper.deleteByPrimaryKey( 18);
system.out.println(「 num =」+num);
} //@@test @test public void testinsert(){
} //@@test @test public void testinsert(){
= new fureat = new furear = new furear(); setAuthorusername(“” ");
author.setAuthorPassword("admin123");
hor.
int num=authorMapper .insert(author);
System.out.println("num="+num); //這個方法插入,預設不會將資料庫自增加ID返回,如果需要,可以手動增加
"插入後的ID"+author.getAuthorId());
} //插入 @測試public void testInsertext(){
)(撰寫作者= 新研究)撰寫作者= new 筆.setauthorpassword(「 admin123」);
rutian.setauthoremail(「 admin1234@qqqq.com」) );
author.setRegisterTime(new Date())); int num=authorMapper.insert(author);
System.out .println("num="+num); //這個方法插入,預設不會將資料庫自增加ID返回,如果需要,可以手動增加
System.out.println("插入後的ID"+author.getAuthorId ());
} //自訂條件查詢 @Test public void testSelectByExample() { 錯誤(); //建立criteria物件新增條件與連結
Criteria criteria=authorExample.createCriteria (); //需手動加%
criteria.andAuthorUsernameLike("%張三%");
List
System.out.println(list.size());
. ) {
Author author= authorMapper. selectByPrimaryKey(6);
System.out.println(author.getAuthorUsername()+"..."+author.getAuthorBio());
}
); fail("Not yet implemented ");
}
@Test public void testUpdateByExample() {
fail("Not yet impleed"); UpdateByPrimaryKeySelective() {
fail("Not yet implemented");
}
@Test public void testUpdateByPrimaryKey() {
fail("Not yet implemented");
P中文網(www.php.cn)!