中文文档链接:mybatis中文网
mybatis项目 环境搭建导入三个依赖
mysql驱动、mybatis、junit
xml配置mysql mysql-connector-java org.mybatis mybatis junit junit 4.12
工具类//com.mysql.jdbc.Driver //jdbc:mysql://localhost:3306/mybatis?userSSL=true
private static SqlSessionFactory ssf;
static{try{
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
ssf = new SqlSessionFactoryBuilder().build(is);
}catch(IOException e){
e.printStackTrace();
}}
static SqlSession getSqlSession(){
SqlSession ss = ssf.openSession();
return ss;
}
代码
当表中的字段太多,而我们只需要修改某些字段或添加某些字段,其余的不管,可以把传入的参数从对象改为map,只在map中放入你所需要的键值。map传递参数,直接在sql中取key,对象传递参数,直接在sql中取属性,只有一个基本数据类型时可以省略
实体类 mapper.xmlselect * from Blog where id = #{id}
namespace的值等于绑定一个对应的Dao/mapper接口,全限定名
sql中的id等于接口的方法名
增删改需要提交事务
dao接口(mapper) service controller 测试通过sqlSession.getMapper(xxx.class)得到mapper/dao对象
资源导出失败,the error may exist in xxxxxxxmaven由于约定大于配置,配置文件无法被导出或生效,解决方案:
在build中配置resources,防止我们资源导出失败的问题
模糊查询src/main/resources **/*.xml true
1、Java代码执行的时候,传递通配符%%
2、在sql拼接中使用通配符,eg:select * from user where name like “%”#{name}“%”



