代码示例:
Step1. 创建一个普通java工程
Step2. 配置sqlMapConfig.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configyration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configyration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="驱动"/>
<property name="url" value="数据库地址"/>
<property name="username" value="用户名"/>
<property name="password" value="密码"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sqlmap/EbizCode.xml" />
</mappers>
</configyration>
Step2.2. 配置映射文件
注:整合ssm框架后可自动生成基本功能,部分特殊查询需自己进行编写
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<mapper namespace="ebizCode">
<select id="getEbizCodeByType" parameterType="String" resultType="com.zyl.mybatis.demo.pojo.EbizCode">
select * from ebiz_code c where c.code_type= #{codeType}
</select>
<select id="getEbizCodeByName" parameterType="String" resultType="com.zyl.mybatis.demo.pojo.EbizCode">
select * from ebiz_code c where c.code_type like '%#{value}%'
</select>
</mapper>
Step2.3. 配置POJO
/**
* pojo
* @author suk1M
* 随便写的就不加注释了,然而不加注释不是一个好习惯
*/
public class EbizCode {
private BigDecimal id;
private String codeType;
private String code;
private String codeLabel;
private BigDecimal showOrder;
private String parentCode;
private Date createdDate;
private Date modifiedDate;
private String createdUser;
private String modifiedUser;
private Short isDelete;
private String standByFlag1;
private String standByFlag2;
private String standByFlag3;
private String standByFlag4;
private String standByFlag5;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getCodeType() {
return codeType;
}
public void setCodeType(String codeType) {
this.codeType = codeType == null ? null : codeType.trim();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code == null ? null : code.trim();
}
public String getCodeLabel() {
return codeLabel;
}
public void setCodeLabel(String codeLabel) {
this.codeLabel = codeLabel == null ? null : codeLabel.trim();
}
public BigDecimal getShowOrder() {
return showOrder;
}
public void setShowOrder(BigDecimal showOrder) {
this.showOrder = showOrder;
}
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode == null ? null : parentCode.trim();
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public String getCreatedUser() {
return createdUser;
}
public void setCreatedUser(String createdUser) {
this.createdUser = createdUser == null ? null : createdUser.trim();
}
public String getModifiedUser() {
return modifiedUser;
}
public void setModifiedUser(String modifiedUser) {
this.modifiedUser = modifiedUser == null ? null : modifiedUser.trim();
}
public Short getIsDelete() {
return isDelete;
}
public void setIsDelete(Short isDelete) {
this.isDelete = isDelete;
}
public String getStandByFlag1() {
return standByFlag1;
}
public void setStandByFlag1(String standByFlag1) {
this.standByFlag1 = standByFlag1 == null ? null : standByFlag1.trim();
}
public String getStandByFlag2() {
return standByFlag2;
}
public void setStandByFlag2(String standByFlag2) {
this.standByFlag2 = standByFlag2 == null ? null : standByFlag2.trim();
}
public String getStandByFlag3() {
return standByFlag3;
}
public void setStandByFlag3(String standByFlag3) {
this.standByFlag3 = standByFlag3 == null ? null : standByFlag3.trim();
}
public String getStandByFlag4() {
return standByFlag4;
}
public void setStandByFlag4(String standByFlag4) {
this.standByFlag4 = standByFlag4 == null ? null : standByFlag4.trim();
}
public String getStandByFlag5() {
return standByFlag5;
}
public void setStandByFlag5(String standByFlag5) {
this.standByFlag5 = standByFlag5 == null ? null : standByFlag5.trim();
}
}
Step3. 编写demo示例
/**
* testDemo
* @author suk1M
*
*/
public class MybatisDemo {
public static void main(String[] args) throws IOException {
String resource = "sqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
SqlSession ss = ssf.openSession();
EbizCode ec = ss.selectOne("ebizCode.getEbizCodeByType", "firstChannel");
System.out.println(ec);
ss.close();
}
}