文档链接:http://mybatis.org/spring/zh/index.html
环境准备:
导入依赖:
注意点:
mybtis去创建sqlSessionFactory代码:
package com.mybatis.study.util;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.InputStream;public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(true); }}
mybatis-spring创建sqlSessionFactory代码:
<?xml version="1.0" encoding="UTF-8"?>
实现类
package com.spring.study.dao;import com.spring.study.bean.User;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.support.SqlSessionDaoSupport;import java.util.List;public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper { //我们的所有操作都使用sqlSession来执行,在原来,现在都使用sqlSessionTemplate; public List