mysql mysql-connector-java 8.0.18 junit junit 4.12 org.mybatis mybatis 3.4.6
2.在resouces文件下新建xml文件db.propertiessrc/main/resources ***.xml false src/main/java ***.xml false
写配置文件
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf-8 username=root password=DRsXT5ZJ6Oi55LPQ3.在resouces文件下新建xml文件mybatis-config-xml
4.创建一个MybatisUtils工具类
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
5.创建xml文件XxxMapper.xml映射dao层接口
6.添加日志select * from user
5.测试
@Test
public void test() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
List userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
2.增删改查
1.select
2.delete
3.updatedelete from USER where id = #{id};
4.insertupdate USER set name = #{name},pwd = #{pwd} where id = #{id};
5.模糊查询 6.分页查询insert into USER (id,name ,pwd) values (#{id},#{name},#{pwd});
3.起别名
3.1具体的某个文件
3.2给包名起别名
注,用别名的时候直接用文件名,全小写
3.3用注解起别名@Alias("author")
注,直接在类上注解
4.解决实体属性名与数据库列名不一致问题 1.建一个resultMap标签2.引用//property实体类里的,column数据库里的
然后在引用它的语句中设置 resultMap 属性就行了(注意我们去掉了 resultType 属性)。比如:
5.使用注解 5.1在接口上写注解public interface UserMapper {
// 使用注解
@Select("select * from user")
List getUserListAnnotate();
}
5.2进行绑定
6.association和collection
association用于对象,关联
collection用于集合
6.1一对多
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private Teacher teacher;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int id;
private String name;
}
第一种查询
select * from student
第二种查询
6.2多对一
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int id;
private String name;
private List student;
}
第一种查询
第二种查询
7.动态查询
7.1模糊查询if标签
接口
//查询 ListgetBlogIf(Map map);
if
7.2更新数据set标签select * from blog and title like concat('%',#{title},'%') and author like concat('%',#{author}.'%')
接口
set标签
7.3Forechupdate blog where id = #{id} title = #{title}, author = #{author}, views = #{views},
forech
select * from blog id = #{id}
测试
@Test
public void queryForech() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
ArrayList arrayList = new ArrayList();
arrayList.add(1);
arrayList.add(2);
HashMap hashMap = new HashMap();
hashMap.put("ids",arrayList);
mapper.queryForeach(hashMap);
sqlSession.close();
}
8.二级缓存
8.1在mybatis-config.xml中开启全局缓存
8.1添加局部缓存,在xxMapper.xml中添加



