ä¸åç®æ è¿è¡æ¶ä»£ç çæååºæ¬ä½¿ç¨
ç®å½
- ä¸åç®æ è¿è¡æ¶ä»£ç çæååºæ¬ä½¿ç¨
-
- ä¸åç®æ è¿è¡æ¶å¯¹æ¯
- åå¤æ°æ®è¡¨
- MyBatis3DynamicSql
-
- generatorConfig.xml
- çæ产ç©
- æµè¯çæ代ç
- MyBatis3Kotlin
- MyBatis3
-
- generatorConfig.xml
- çæ产ç©
- æµè¯çæ代ç
- MyBatis3Simple
-
- generatorConfig.xml
- çæ产ç©
- æµè¯çæ代ç
- å®æ´ä»£ç
- åè
MyBatis Generatorï¼MBGï¼æ ¹æ®å ¶é ç½®æ¹å¼çæä¸åæ ·å¼ç代ç ãè¿æ¯éè¿å¨
<context>
é ç½®å ç´ ä¸æå®
targetRuntime
å±æ§æ¥æ§å¶ç
æ¬æ¬¡ä»£ç çææ¯éè¿Maven pluginè¿è¡MyBatis Generatorï¼MBGï¼,该æ¹å¼ç使ç¨åèæ¬ç³»åå ¶ä»æç«
ä¸åç®æ è¿è¡æ¶å¯¹æ¯
Target Runtime | 说æ |
---|---|
MyBatis3DynamicSql | é»è®¤å¼ çæjava代ç ä¸çæXML-ä» ç¨MyBatis3注解 çæç模å对象æ¯âå¹³é¢âç-没æåç¬ç主é®å¯¹è±¡ çæç代ç ä¾èµäºMyBatiså¨æSQLåº çæç代ç éç¸å¯¹è¾å° çæç代ç 为æ¥è¯¢æé æä¾äºæ大ççµæ´»æ§ |
MyBatis3Kotlin | çæKotlin代ç ä¸çæXML-ä» ç¨MyBatis3注解 çæç模å对象æ¯âå¹³é¢âç-没æåç¬ç主é®å¯¹è±¡ çæç代ç ä¾èµäºMyBatiså¨æSQLåº çæç代ç éç¸å¯¹è¾å° çæç代ç 为æ¥è¯¢æé æä¾äºæ大ççµæ´»æ§ |
MyBatis3 | è¿æ¯æåçè¿è¡æ¶ãå¨MBG 1.3.6çä¹åï¼MBGç大å¤æ°ç¨æ³é½ä½¿ç¨è¿ç§æ ·å¼ç代ç ã çæJava代ç çæ没æXMLçMyBatis3å ¼å®¹ç带注解çæ¥å£æè çæMyBatis3 å ¼å®¹çXML çæç模å对象å¯è½å ·æä¸ä¸ªå±æ¬¡ç»æï¼å ¶ä¸å å«åç¬ç主é®å¯¹è±¡å/æ带æBLOBå段çåç¬å¯¹è±¡ çæç代ç 没æå¤é¨ä¾èµå ³ç³» çæç代ç éé常大 çæç代ç å ·ææéçæ¥è¯¢æé åè½ï¼å¹¶ä¸é¾ä»¥æ©å± |
MyBatis3Simple | è¿æ¯MyBatis3è¿è¡æ¶çç®åçæ¬ã çæJava代ç çæ没æXMLçMyBatis3å ¼å®¹ç带注解çæ¥å£æè çæMyBatis3 å ¼å®¹çXML çæç模å对象æ¯âå¹³é¢âç-没æåç¬ç主é®å¯¹è±¡ çæç代ç 没æå¤é¨ä¾èµå ³ç³» çæç代ç éç¸å¯¹è¾å° 没æçæâby exampleâæâselectiveâçæ¹æ³ çæç代ç ä¸å å«ç¨äºå¨ææ¥è¯¢æé çæ¹æ³ï¼å¹¶ä¸å¾é¾æ©å± |
åå¤æ°æ®è¡¨
oa_staff
CREATE TABLE `oa_staff` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL COMMENT 'åå',
`gender` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'æ§å«ï¼0->ç·ï¼1->女',
`birthday` date DEFAULT NULL,
`address` varchar(80) DEFAULT NULL COMMENT 'ä½å',
`native_place` varchar(80) DEFAULT NULL COMMENT 'ç±è´¯',
`hiredate` date DEFAULT NULL COMMENT 'å
¥èæ¥æ',
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='å工信æ¯'
MyBatis3DynamicSql
è¿ä¸ªæ¯é»è®¤çé ç½®å¼
generatorConfig.xml
src\main\resources\generatorConfig.xml
<!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="dsql" targetRuntime="MyBatis3DynamicSql">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.56.101:3306/db_example?characterEncoding=utf8&autoReconnect=true&serverTimezone=PRC"
userId="root" password="123456"/>
<javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"/>
<table tableName="oa_staff">
<generatedKey column="id" sqlStatement="MySql" />
</table>
</context>
</generatorConfiguration>
å ³é®ï¼
<context id="dsql" targetRuntime="MyBatis3DynamicSql">
çæ产ç©
1ãä¸è¡¨ç»æå¹é çJavaç±»
package com.example.model;
import java.util.Date;
import javax.annotation.Generated;
public class OaStaff {
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.044+08:00", comments="Source field: oa_staff.id")
private Long id;
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.047+08:00", comments="Source field: oa_staff.name")
private String name;
.
.
.
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.047+08:00", comments="Source field: oa_staff.hiredate")
public Date getHiredate() {
return hiredate;
}
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.048+08:00", comments="Source field: oa_staff.gmt_modified")
public void setGmtModified(Date gmtModified) {
this.gmtModified = gmtModified;
}
}
çç¥å¤§é¨åçæç代ç
2ãç¨äºå¯ç¨å¨æéæ©ãæ´æ°åå é¤çç±»
package com.example.mapper;
import java.sql.JDBCType;
import java.util.Date;
import javax.annotation.Generated;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
public final class OaStaffDynamicSqlSupport {
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.05+08:00", comments="Source Table: oa_staff")
public static final OaStaff oaStaff = new OaStaff();
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.051+08:00", comments="Source field: oa_staff.id")
public static final SqlColumn<Long> id = oaStaff.id;
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.051+08:00", comments="Source field: oa_staff.name")
public static final SqlColumn<String> name = oaStaff.name;
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.051+08:00", comments="Source field: oa_staff.gender")
public static final SqlColumn<Byte> gender = oaStaff.gender;
.
.
.
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.052+08:00", comments="Source field: oa_staff.gmt_create")
public static final SqlColumn<Date> gmtCreate = oaStaff.gmtCreate;
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.052+08:00", comments="Source field: oa_staff.gmt_modified")
public static final SqlColumn<Date> gmtModified = oaStaff.gmtModified;
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.051+08:00", comments="Source Table: oa_staff")
public static final class OaStaff extends SqlTable {
public final SqlColumn<Long> id = column("id", JDBCType.BIGINT);
public final SqlColumn<String> name = column("name", JDBCType.VARCHAR);
public final SqlColumn<Byte> gender = column("gender", JDBCType.TINYINT);
public final SqlColumn<Date> birthday = column("birthday", JDBCType.DATE);
.
.
.
public final SqlColumn<Date> gmtCreate = column("gmt_create", JDBCType.TIMESTAMP);
public final SqlColumn<Date> gmtModified = column("gmt_modified", JDBCType.TIMESTAMP);
public OaStaff() {
super("oa_staff");
}
}
}
çç¥å¤§é¨åçæç代ç
3ãJavaæ å°å¨æ¥å£
package com.example.mapper;
import static com.example.mapper.OaStaffDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
import com.example.model.OaStaff;
import java.util.List;
import java.util.Optional;
import javax.annotation.Generated;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
.
.
.
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
.
.
.
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
@Mapper
public interface OaStaffMapper {
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.06+08:00", comments="Source Table: oa_staff")
BasicColumn[] selectList = BasicColumn.columnList(id, name, gender, birthday, address, nativePlace, hiredate, gmtCreate, gmtModified);
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.052+08:00", comments="Source Table: oa_staff")
@SelectProvider(type=SqlProviderAdapter.class, method="select")
long count(SelectStatementProvider selectStatement);
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.053+08:00", comments="Source Table: oa_staff")
@DeleteProvider(type=SqlProviderAdapter.class, method="delete")
int delete(DeleteStatementProvider deleteStatement);
.
.
.
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.057+08:00", comments="Source Table: oa_staff")
@UpdateProvider(type=SqlProviderAdapter.class, method="update")
int update(UpdateStatementProvider updateStatement);
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.057+08:00", comments="Source Table: oa_staff")
default long count(CountDSLCompleter completer) {
return MyBatis3Utils.countFrom(this::count, oaStaff, completer);
}
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.057+08:00", comments="Source Table: oa_staff")
default int delete(DeleteDSLCompleter completer) {
return MyBatis3Utils.deleteFrom(this::delete, oaStaff, completer);
}
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.058+08:00", comments="Source Table: oa_staff")
default int deleteByPrimaryKey(Long id_) {
return delete(c ->
c.where(id, isEqualTo(id_))
);
}
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.058+08:00", comments="Source Table: oa_staff")
default int insert(OaStaff record) {
return MyBatis3Utils.insert(this::insert, record, oaStaff, c ->
c.map(id).toProperty("id")
.map(name).toProperty("name")
.map(gender).toProperty("gender")
.map(birthday).toProperty("birthday")
.map(address).toProperty("address")
.map(nativePlace).toProperty("nativePlace")
.map(hiredate).toProperty("hiredate")
.map(gmtCreate).toProperty("gmtCreate")
.map(gmtModified).toProperty("gmtModified")
);
}
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.059+08:00", comments="Source Table: oa_staff")
default int insertSelective(OaStaff record) {
return MyBatis3Utils.insert(this::insert, record, oaStaff, c ->
c.map(id).toProperty("id")
.map(name).toPropertyWhenPresent("name", record::getName)
.map(gender).toPropertyWhenPresent("gender", record::getGender)
.map(birthday).toPropertyWhenPresent("birthday", record::getBirthday)
.map(address).toPropertyWhenPresent("address", record::getAddress)
.map(nativePlace).toPropertyWhenPresent("nativePlace", record::getNativePlace)
.map(hiredate).toPropertyWhenPresent("hiredate", record::getHiredate)
.map(gmtCreate).toPropertyWhenPresent("gmtCreate", record::getGmtCreate)
.map(gmtModified).toPropertyWhenPresent("gmtModified", record::getGmtModified)
);
}
.
.
.
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.062+08:00", comments="Source Table: oa_staff")
default Optional<OaStaff> selectByPrimaryKey(Long id_) {
return selectOne(c ->
c.where(id, isEqualTo(id_))
);
}
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.062+08:00", comments="Source Table: oa_staff")
default int update(UpdateDSLCompleter completer) {
return MyBatis3Utils.update(this::update, oaStaff, completer);
}
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.062+08:00", comments="Source Table: oa_staff")
static UpdateDSL<UpdateModel> updateAllColumns(OaStaff record, UpdateDSL<UpdateModel> dsl) {
return dsl.set(id).equalTo(record::getId)
.set(name).equalTo(record::getName)
.set(gender).equalTo(record::getGender)
.set(birthday).equalTo(record::getBirthday)
.set(address).equalTo(record::getAddress)
.set(nativePlace).equalTo(record::getNativePlace)
.set(hiredate).equalTo(record::getHiredate)
.set(gmtCreate).equalTo(record::getGmtCreate)
.set(gmtModified).equalTo(record::getGmtModified);
}
.
.
.
@Generated(value="org.mybatis.generator.api.MyBatisGenerator", date="2020-05-03T21:35:35.064+08:00", comments="Source Table: oa_staff")
default int updateByPrimaryKeySelective(OaStaff record) {
return update(c ->
c.set(name).equalToWhenPresent(record::getName)
.set(gender).equalToWhenPresent(record::getGender)
.set(birthday).equalToWhenPresent(record::getBirthday)
.set(address).equalToWhenPresent(record::getAddress)
.set(nativePlace).equalToWhenPresent(record::getNativePlace)
.set(hiredate).equalToWhenPresent(record::getHiredate)
.set(gmtCreate).equalToWhenPresent(record::getGmtCreate)
.set(gmtModified).equalToWhenPresent(record::getGmtModified)
.where(id, isEqualTo(record::getId))
);
}
}
çç¥å¤§é¨åçæç代ç
æµè¯çæ代ç
package com.example;
import com.example.mapper.OaStaffMapper;
import com.example.model.OaStaff;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import static com.example.mapper.OaStaffDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
public class OaStaffTableTest {
private final Logger log = LoggerFactory.getLogger(OaStaffTableTest.class);
static SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession;
private OaStaffMapper oaStaffMapper;
@Before
public void init() throws IOException {
String resource = "MapperConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
oaStaffMapper = sqlSession.getMapper(OaStaffMapper.class);
}
@Test
public void testInsert() {
OaStaff oaStaff = new OaStaff();
oaStaff.setName("lisi");
oaStaff.setGender(Byte.parseByte("0"));
oaStaffMapper.insert(oaStaff);
sqlSession.commit();
}
@Test
public void testSelect() {
Optional<OaStaff> oaStaffOptional = oaStaffMapper.selectByPrimaryKey(1L);
OaStaff staff = oaStaffOptional.get();
log.info("id = {}, name= {}, addr = {}", staff.getId(), staff.getName(), staff.getAddress());
List<OaStaff> select = oaStaffMapper.select(c -> c.where(id, isEqualTo(1L)));
for (OaStaff s : select) {
log.info("name={}, id = {}, addr = {}", s.getName(), s.getId(), s.getAddress());
}
}
@Test
public void testUpdate() {
Optional<OaStaff> oaStaffOptional = oaStaffMapper.selectByPrimaryKey(1L);
OaStaff oaStaff = oaStaffOptional.get();
oaStaff.setAddress("å京å¸æµ·æ·åº");
oaStaffMapper.updateByPrimaryKey(oaStaff);
oaStaffMapper.update(c -> c.set(address).equalTo("å京å¸æé³åº")
.set(gender).equalTo(Byte.parseByte("1"))
.where(id, isEqualTo(2L)));
sqlSession.commit();
}
@Test
public void testDelete() {
oaStaffMapper.deleteByPrimaryKey(40L);
oaStaffMapper.delete(c -> c.where(name, isEqualTo("libiao"), and(gender, isEqualTo(Byte.parseByte("1")))).or(address, isEqualTo("广å·å¸")));
}
@After
public void destroy() {
sqlSession.close();
}
}
MapperConfig.xml
å 容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.56.101:3306/db_example?characterEncoding=utf8&autoReconnect=true&serverTimezone=PRC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.example.mapper"/>
</mappers>
</configuration>
MyBatis3Kotlin
ç±äºæ¬äººå·¥ä½ç¨æ²¡æç¨å°
Kotlin
è¯è¨ï¼çæéç¨äº
Kotlin
çé¨åç¥è¿ã
MyBatis3
generatorConfig.xml
src\main\resources\generatorConfig.xml
<!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="myBatis3" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.56.101:3306/db_example?characterEncoding=utf8&autoReconnect=true&serverTimezone=PRC"
userId="root" password="123456"/>
<javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"/>
<table tableName="oa_staff">
<generatedKey column="id" sqlStatement="MySql" />
</table>
</context>
</generatorConfiguration>
çæ产ç©
1ãä¸è¡¨ç»æå¹é çJavaç±»
package com.example.model;
import java.util.Date;
public class OaStaff {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.id
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
private Long id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.name
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
private String name;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.gender
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
private Byte gender;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.birthday
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
private Date birthday;
.
.
.
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column oa_staff.gmt_create
*
* @param gmtCreate the value for oa_staff.gmt_create
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
public void setGmtCreate(Date gmtCreate) {
this.gmtCreate = gmtCreate;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column oa_staff.gmt_modified
*
* @return the value of oa_staff.gmt_modified
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
public Date getGmtModified() {
return gmtModified;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column oa_staff.gmt_modified
*
* @param gmtModified the value for oa_staff.gmt_modified
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
public void setGmtModified(Date gmtModified) {
this.gmtModified = gmtModified;
}
}
çç¥å¤§é¨åçæç代ç
2ãç¨äºå¯ç¨å¨æéæ©ãæ´æ°åå é¤çç±»ï¼
Example
ï¼
package com.example.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
public class OaStaffExample {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
protected String orderByClause;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
protected boolean distinct;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
protected List<Criteria> oredCriteria;
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
public OaStaffExample() {
oredCriteria = new ArrayList<>();
}
.
.
.
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<>();
}
.
.
.
public Criteria andGmtModifiedNotBetween(Date value1, Date value2) {
addCriterion("gmt_modified not between", value1, value2, "gmtModified");
return (Criteria) this;
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table oa_staff
*
* @mbg.generated do_not_delete_during_merge Sun May 03 22:55:28 CST 2020
*/
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
.
.
.
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
çç¥å¤§é¨åçæç代ç
3ãJavaæ å°å¨æ¥å£
package com.example.mapper;
import com.example.model.OaStaff;
import com.example.model.OaStaffExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface OaStaffMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
long countByExample(OaStaffExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
int deleteByExample(OaStaffExample example);
.
.
.
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
int updateByPrimaryKeySelective(OaStaff record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 22:55:28 CST 2020
*/
int updateByPrimaryKey(OaStaff record);
}
çç¥å¤§é¨åçæç代ç
4ãä¸MyBatis3å ¼å®¹çSQLæ å°XMLæ件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.OaStaffMapper">
<resultMap id="BaseResultMap" type="com.example.model.OaStaff">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 22:55:28 CST 2020.
-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="gender" jdbcType="TINYINT" property="gender" />
<result column="birthday" jdbcType="DATE" property="birthday" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="native_place" jdbcType="VARCHAR" property="nativePlace" />
<result column="hiredate" jdbcType="DATE" property="hiredate" />
<result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate" />
<result column="gmt_modified" jdbcType="TIMESTAMP" property="gmtModified" />
</resultMap>
<sql id="Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 22:55:28 CST 2020.
-->
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
.
.
.
<update id="updateByPrimaryKey" parameterType="com.example.model.OaStaff">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 22:55:28 CST 2020.
-->
update oa_staff
set name = #{name,jdbcType=VARCHAR},
gender = #{gender,jdbcType=TINYINT},
birthday = #{birthday,jdbcType=DATE},
address = #{address,jdbcType=VARCHAR},
native_place = #{nativePlace,jdbcType=VARCHAR},
hiredate = #{hiredate,jdbcType=DATE},
gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
çç¥å¤§é¨åçæç代ç
æµè¯çæ代ç
package com.example;
import com.example.mapper.OaStaffMapper;
import com.example.model.OaStaff;
import com.example.model.OaStaffExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class OaStaffTableTest {
static final Logger log = LoggerFactory.getLogger(OaStaffTableTest.class);
SqlSessionFactory sqlSessionFactory;
SqlSession sqlSession;
OaStaffMapper oaStaffMapper;
@Before
public void init() throws IOException {
String resource = "MapperConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
oaStaffMapper = sqlSession.getMapper(OaStaffMapper.class);
}
@Test
public void testInsert() {
OaStaff staff = new OaStaff();
staff.setName("å¼ ä¸");
staff.setAddress("ç«æ");
staff.setGender(new Byte("1"));
Date now = new Date();
staff.setGmtCreate(now);
staff.setGmtModified(now);
oaStaffMapper.insert(staff);
sqlSession.commit();
}
@Test
public void testSelect() {
OaStaff staff = oaStaffMapper.selectByPrimaryKey(3L);
log.info("id = {}, name={} addr= {}, create= {}", staff.getId(), staff.getName(), staff.getAddress(), staff.getGmtCreate());
OaStaffExample example = new OaStaffExample();
example.or()
.andNameLike("l%");
List<OaStaff> oaStaffs = oaStaffMapper.selectByExample(example);
for (OaStaff s : oaStaffs) {
log.info("id = {}, name={} addr= {}, create= {}", s.getId(), s.getName(), s.getAddress(), s.getGmtCreate());
}
}
@Test
public void testUpdate() {
OaStaff staff = oaStaffMapper.selectByPrimaryKey(3L);
Date now = new Date();
staff.setGmtModified(now);
// oaStaffMapper.updateByPrimaryKey(staff); // ok
OaStaffExample example = new OaStaffExample();
example.or()
.andNameLike("l%");
OaStaff s = new OaStaff();
s.setAddress("第ä¸åº");
// oaStaffMapper.updateByExample(s, example); // error! ææå段å
¨é¨æ´æ°ï¼ès.id为空ï¼ä¸»é®ä¸å
许为空导è´æ´æ°å¤±è´¥
oaStaffMapper.updateByExampleSelective(s, example); // ok åªæ´æ´æ° s ä¸ä¸ä¸ºç©ºçå段
}
@Test
public void testDelete() {
oaStaffMapper.deleteByPrimaryKey(390L);
OaStaffExample example = new OaStaffExample();
example.or()
.andNameLike("l%");
oaStaffMapper.deleteByExample(example);
}
@After
public void destroy() {
sqlSession.close();
}
}
MapperConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.56.101:3306/db_example?characterEncoding=utf8&autoReconnect=true&serverTimezone=PRC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.example.mapper"/>
</mappers>
</configuration>
MyBatis3Simple
generatorConfig.xml
src\main\resources\generatorConfig.xml
<!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="simple" targetRuntime="MyBatis3Simple">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.56.101:3306/db_example?characterEncoding=utf8&autoReconnect=true&serverTimezone=PRC"
userId="root" password="123456"/>
<javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"/>
<table tableName="oa_staff">
<generatedKey column="id" sqlStatement="MySql" />
</table>
</context>
</generatorConfiguration>
çæ产ç©
1ãä¸è¡¨ç»æå¹é çJavaç±»
package com.example.model;
import java.util.Date;
public class OaStaff {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.id
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private Long id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.name
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private String name;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.gender
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private Byte gender;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.birthday
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private Date birthday;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.address
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private String address;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.native_place
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private String nativePlace;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.hiredate
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private Date hiredate;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.gmt_create
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private Date gmtCreate;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column oa_staff.gmt_modified
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
private Date gmtModified;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column oa_staff.id
*
* @return the value of oa_staff.id
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
public Long getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column oa_staff.id
*
* @param id the value for oa_staff.id
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
public void setId(Long id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column oa_staff.name
*
* @return the value of oa_staff.name
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
public String getName() {
return name;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column oa_staff.name
*
* @param name the value for oa_staff.name
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
public void setName(String name) {
this.name = name;
}
.
.
.
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column oa_staff.gmt_modified
*
* @param gmtModified the value for oa_staff.gmt_modified
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
public void setGmtModified(Date gmtModified) {
this.gmtModified = gmtModified;
}
}
çç¥å¤§é¨åçæç代ç
2ãJavaæ å°å¨æ¥å£
package com.example.mapper;
import com.example.model.OaStaff;
import java.util.List;
public interface OaStaffMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
int deleteByPrimaryKey(Long id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
int insert(OaStaff record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
OaStaff selectByPrimaryKey(Long id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
List<OaStaff> selectAll();
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table oa_staff
*
* @mbg.generated Sun May 03 23:34:18 CST 2020
*/
int updateByPrimaryKey(OaStaff record);
}
3ãä¸MyBatis3å ¼å®¹çSQLæ å°XMLæ件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.OaStaffMapper">
<resultMap id="BaseResultMap" type="com.example.model.OaStaff">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 23:34:18 CST 2020.
-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="gender" jdbcType="TINYINT" property="gender" />
<result column="birthday" jdbcType="DATE" property="birthday" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="native_place" jdbcType="VARCHAR" property="nativePlace" />
<result column="hiredate" jdbcType="DATE" property="hiredate" />
<result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate" />
<result column="gmt_modified" jdbcType="TIMESTAMP" property="gmtModified" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 23:34:18 CST 2020.
-->
delete from oa_staff
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.example.model.OaStaff">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 23:34:18 CST 2020.
-->
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into oa_staff (id, name, gender,
birthday, address, native_place,
hiredate, gmt_create, gmt_modified
)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{gender,jdbcType=TINYINT},
#{birthday,jdbcType=DATE}, #{address,jdbcType=VARCHAR}, #{nativePlace,jdbcType=VARCHAR},
#{hiredate,jdbcType=DATE}, #{gmtCreate,jdbcType=TIMESTAMP}, #{gmtModified,jdbcType=TIMESTAMP}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.example.model.OaStaff">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 23:34:18 CST 2020.
-->
update oa_staff
set name = #{name,jdbcType=VARCHAR},
gender = #{gender,jdbcType=TINYINT},
birthday = #{birthday,jdbcType=DATE},
address = #{address,jdbcType=VARCHAR},
native_place = #{nativePlace,jdbcType=VARCHAR},
hiredate = #{hiredate,jdbcType=DATE},
gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 23:34:18 CST 2020.
-->
select id, name, gender, birthday, address, native_place, hiredate, gmt_create, gmt_modified
from oa_staff
where id = #{id,jdbcType=BIGINT}
</select>
<select id="selectAll" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun May 03 23:34:18 CST 2020.
-->
select id, name, gender, birthday, address, native_place, hiredate, gmt_create, gmt_modified
from oa_staff
</select>
</mapper>
æµè¯çæ代ç
package com.example;
import com.example.mapper.OaStaffMapper;
import com.example.model.OaStaff;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
public class OaStaffTableTest {
private static final Logger log = LoggerFactory.getLogger(OaStaffTableTest.class);
private SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession;
private OaStaffMapper oaStaffMapper;
@Before
public void init() throws IOException {
String resource = "MapperConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
oaStaffMapper = sqlSession.getMapper(OaStaffMapper.class);
}
@Test
public void testInsert() {
OaStaff staff = new OaStaff();
staff.setName("èµµæ¦");
staff.setAddress("æç");
staff.setGender(Byte.valueOf("1"));
Date now = new Date();
staff.setGmtCreate(now);
staff.setGmtModified(now);
oaStaffMapper.insert(staff);
sqlSession.commit();
}
@Test
public void testSelect() {
OaStaff staff = oaStaffMapper.selectByPrimaryKey(4L);
log.info("id = {}, name={} addr= {}, create= {}", staff.getId(), staff.getName(), staff.getAddress(), staff.getGmtCreate());
}
@Test
public void testUpdate() {
OaStaff staff = oaStaffMapper.selectByPrimaryKey(4L);
Date now = new Date();
staff.setGmtModified(now);
oaStaffMapper.updateByPrimaryKey(staff);
}
@Test
public void testDelete() {
oaStaffMapper.deleteByPrimaryKey(390L);
}
@After
public void destroy() {
sqlSession.close();
}
}
MapperConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.56.101:3306/db_example?characterEncoding=utf8&autoReconnect=true&serverTimezone=PRC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.example.mapper"/>
</mappers>
</configuration>
å®æ´ä»£ç
https://gitee.com/tobybiao/MBG-examples
åè
MyBatis Generator Quick Start Guide