mysqltextmybatis⼊库_MyBatis(⼀):配置并使⽤MyBatis具体是什么东东,这些在后边在研究吧,本⽂⽬的是为了记录如何使⽤MyBatis。
⾸先,需要下载MyBatis开发所需要⽂件。
解压MyBatis后把其中的Jar包引⼊⼯程中:
在⼯程下边添加配置⽂件:
/p>
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
log4j.properties
# #define DEBUG priority, R
# Logger = INFO, R
# # configure log output type as file
# log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
# #log filename
# log4j.appender.R.file = ./logRecord.log
# # append
# log4j.appender.R.Append = true
# log4j.appender.R.layout = org.apache.log4j.PatternLayout
# log4j.appender.R.layout.ConversionPattern = %n%d%p[%l]%m%n
#
# log4j.appender.R.DatePattern='.' yyyy-MM-dd
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=INFO
apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
这⾥主要⽬的是实现把MyBatis的sql操作打印到控制台中。
db.properties
#oracle.jdbc.driver.OracleDriver |sql.jdbc.Driver
sql.jdbc.Driver
#jdbc:oracle:thin:@localhost:1521:orcl | jdbc:mysql://localhost:3306/mybatis url=jdbc:mysql://localhost:3306/mybatis
name=root
password=123456
新建mysql库:MyBatis,新建表TaskAutoExecutePlan
createdatabases mybatis;create tableTaskAutoExecutePlan(
idvarchar(64) CHARACTER SET utf8mb4 NOT NULL DEFAULT '0', autoExecutePlanTypevarchar(36) NOT NULL default '0',
taskStatusvarchar(36) NOT NULL default '0',
createDatedatetime not null DEFAULT CURRENT_TIMESTAMP, modifyDatedatetime not null DEFA
ULT CURRENT_TIMESTAMP)
新建实体类TaskAutoExecutePlan.java
public classTaskAutoExecutePlan {/*** create databases mybatis;
*
* create table TaskAutoExecutePlan(
* `id` varchar(36) CHARACTER SET utf8mb4 NOT NULL DEFAULT '0',
* 'autoExecutePlanType' integer default '0',
* 'taskStatus' integer default '0',
* 'createDate' datetime DEFAULT CURRENT_TIMESTAMP,
* 'modifyDate' datetime DEFAULT CURRENT_TIMESTAMP)
**/
private String id=null;privateAutoExecutePlanType autoExecutePlanType;privateTaskStatus taskStatus;privateDate createDate;privateDate modifyDate;publicString getId() {returnid;
}public voidsetId(String id) {this.id =id;
}publicAutoExecutePlanType getAutoExecutePlanType() {returnautoExecutePlanType;
}public voidsetAutoExecutePlanType(AutoExecutePlanType autoExecutePlanType) {this.autoExecutePlanType
=autoExecutePlanType;
}publicTaskStatus getTaskStatus() {returntaskStatus;
}public voidsetTaskStatus(TaskStatus taskStatus) {this.taskStatus =taskStatus;
}publicDate getCreateDate() {returncreateDate;
}public voidsetCreateDate(Date createDate) {ateDate =createDate;
}publicDate getModifyDate() {returnmodifyDate;
}public voidsetModifyDate(Date modifyDate) {difyDate =modifyDate;
}
}
设计到的enum对象包括:
AutoExecutePlanType.java
TaskStatus.java
* 任务状态:
* 2:Todo,待处理状态
* 4:Doing,正在处理状态
* 8:Success,已成功处理
* 16:Fail;已失败
**/
public enumTaskStatus {/*** 2:Todo,待处理状态
**/Todo(2),/*** 4:Doing,正在处理状态
**/Doing(4),/*** 8:Success,已成功处理
**/Success(8),/*** 16:Fail;已失败
**/Fail(16);privateInteger value;private TaskStatus(intvalue){
setValue(value);
}public static TaskStatus valueOf(intvalue){switch(value){case 2:returnTodo;case 4:returnDoing;case 8:returnSuccess;case 16:returnFail;default:return null;
}
}publicInteger getValue() {returnvalue;
}private void setValue(intvalue) {this.value =value;
}
}
为了实现mybatis映射enum类型需要添加⼀个每个enum对象的⼀个mybatis转化处理类.
public class TaskStatusHandler extends BaseTypeHandler{private Classtype;private finalTaskStatus[] enums;/*** 设置配置⽂
件设置的转换类以及枚举类内容,供其他⽅法更便捷⾼效的实现
*
*@paramtype 配置⽂件中设置的转换类*/
public TaskStatusHandler(Classtype) {if (type == null)throw new IllegalArgumentException("Type argument cannot be null");pe =ums =EnumConstants();if (ums == null)throw new
SimpleName() + " does not represent an enum type.");
}
@Overridepublic void setNonNullParameter(PreparedStatement preparedStatement, int i, TaskStatus autoExecutePlanType, JdbcType jdbcType) throwsSQLException {//baseTypeHandler已经帮我们做了parameter的null判断
preparedStatement.setInt(i, Value());
}
@Overridepublic TaskStatus getNullableResult(ResultSet resultSet, String s) throwsSQLException {//根据数据库存储类型决定
mysql下载jar包
获取类型,本例⼦中数据库中存放INT类型
int i =Int(s);if(resultSet.wasNull()) {return null;
}else { //根据数据库中的code值,定位EnumStatus⼦类
returnlocateEnumStatus(i);
}
}
@Overridepublic TaskStatus getNullableResult(ResultSet resultSet, int i) throwsSQLException {//根据数据库存储类型决定获取
类型,本例⼦中数据库中存放INT类型
int index =Int(i);if(resultSet.wasNull()) {return null;
}else { //根据数据库中的code值,定位EnumStatus⼦类
returnlocateEnumStatus(index);
}
}
@Overridepublic TaskStatus getNullableResult(CallableStatement callableStatement, int i) throwsSQLException {//根据数据库存储类型决定获取类型,本例⼦中数据库中存放INT类型
int index =Int(i);if(callableStatement.wasNull()) {return null;
}else{//根据数据库中的code值,定位EnumStatus⼦类
returnlocateEnumStatus(index);
}
}/*** 枚举类型转换,由于构造函数获取了枚举的⼦类enums,让遍历更加⾼效快捷
*
*@paramcode 数据库中存储的⾃定义code属性
*@returncode对应的枚举类*/
private TaskStatus locateEnumStatus(intcode) {for(TaskStatus status : enums)
{Value().equals(Integer.valueOf(code))) {returnstatus;
}
}throw new IllegalArgumentException("未知的枚举类型:" + code + ",请核对" +SimpleName());
}
}
配置TaskAutoExecutePlan映射⽂件:
/p>
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
select * from TaskAutoExecutePlan
select replace(uuid(),'-','') from dualinsert into TaskAutoExecutePlan
(id,autoExecutePlanType,taskStatus,createDate,modifyDate)
values (#{id,jdbcType=VARCHAR},
#{autoExecutePlanType,jdbcType=BIT,tity.AutoExecutePlanTypeHandler},
#{taskStatus,jdbcType=BIT,tity.TaskStatusHandler},
#{createDate,jdbcType=TIMESTAMP},
#{modifyDate,jdbcType=TIMESTAMP})
当涉及到⽇期操作时,需要注视事项:
在MyBatis映射⽂件中要表明映射类型:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论