1、创建一个java web 项目,导包
2、创建config文件夹,make Directory as Source Root
2.1创建数据库配置文件jdbc.properties
2.2创建SqlMapConfig.xml
1)引入jdbc.properties文件
2)配置pojo别名,<package>配合pojo类上的@Alias("别名")
3)配置Mybatis运行环境,<enviroments>
4)配置mapper扫描路径
3、创建mapper.xml,跟对应的接口在同一路径下
1)<mapper namespace="接口文件的全路径">
2)#{}代表占位符,相当于JDBC中的?,可以达到防止sql注入的危险;${}代表sql的拼接,在拼接like中的%${value}%,或者表名时候用到
select * from ${prefix}ACT_HI_PROCINST where PROC_INST_ID_ = #{processInstanceId}
4、编写测试类
1)抽取出公共方法,读取SqlMapConfig.xml
package com.itheima.mybaitis.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import sun.plugin2.ipc.InProcEvent;
import java.io.InputStream;
/**
* Created by Damon on 2018/6/28_13:17.
*
* @Descrption:
*/
public class SqlSessionUtils {
private static SqlSessionFactorysqlSessionFactory;
static {
try {
SqlSessionFactoryBuilder sqlSessionFactoryBuilder =new SqlSessionFactoryBuilder();
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
}catch (Exception e){
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
2)创建test类,通过SqlSession.getSession(Mapper.class)实例化接口,调用接口中的方法
5、总结