- JDBC -mysql数据库
- connection
- Mybatis
- Spring相关的
- junit
- aop织入
- Mybatis-Spring 【new】
4.1编写实体类
import lombok.Data;
@Data //使用这个Data注解会自动给我们装配get set 构造器方法
public class student {
private int id;
private String name;
private int sex;
private String student_class; //班级
}
4.2编写核心配置文件 ——配置SQL的文件类型 账号密码
4.3编写接口
//定一个接口
public interface StudentMapper {
public List selectStudent();
}
4.4编写Mapper.xml文件 --也就是编写SQL语句
4.5测试
public class Mytest {
public static void main(String[] args) throws IOException {
//三步曲
InputStream in = Resources.getResourceAsStream("Mybatis-config.xml");
//使用流创建一个session工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
//用工厂创建一个Session ,自动提交事务
SqlSession session = sessionFactory.openSession(true);
StudentMapper mapper = session.getMapper(StudentMapper.class); //使用反射获取Student对象
List students = mapper.selectStudent();
for (student s: students){
System.out.println(s.toString());
}
}
}
特别注意的是【重要】
使用pom.xml包文件需要导入mybatis的过滤,不然系统找到mapper文件
src/main/java
**/*.xml
5、Mybatis-Spring
Mybatis-Spring会帮助你将MyBatis代码无缝地整合到Spring中。它将允许MyBatis参与到Spring的事务管理中,创建映射器mapper和SqlSession并注入到bean中,以及将Mybatis的异常转换为Spring的DataAccessException。最终,可以做到应用代码不依赖MyBatis,Spring或Mybatis-Spring
使用的步骤如下:
5.1编写数据源
5.2SqlSessionFactory
5.3SqlSessionTemplate
SqlSessionTemplate是MyBatis-Spring的核心。作为SQLSession的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的SQLSession。SQLSessionTemplate是线程安全的,可以被多个DAO或映射器所共享使用。
5.4需要给接口加实现类【新加】
public class StudentMapperImpl implements StudentMapper{
//在原来我们的所有的操作,都使用sqlSession来执行,现在我们使用SqlSessionTemplate
private SqlSessionTemplate sqlSession;
//定义一个Set注入
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List selectStudent() {
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List students = mapper.selectStudent();
return students;
}
}
5.5将自己写的实现类注入到spring中,前面配置文件都在Spring-dao.xml配置文件当中
5.6测试
public static void main(String[] args) {
//获取Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取Bean对象
StudentMapper studentMapper = context.getBean("StudentMapper", StudentMapper.class);
List students = studentMapper.selectStudent();
//输出内容
for (student s : students){
System.out.println(s);
}
}
注意使用Spring整合Mybatis之后,就不需要使用Mybatis来创建SessionFactroy了。
6.使用SqlSessionDaoSupport整合MybatisSqlSessionDaoSupport是一个抽象的支持类,用来为你提供SqlSession。调用getSqlSession()方法你会得到一个SqlSessionTemplate,之后可以用于执行SQL方法。实现类继承SqlSsessionDaoSupport类。
6.1实现类继承SqlSessionDaoSupoort
public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper{
public List selectStudent() {
//获取SqlSession,不在用SqlSessionTemplate来创建SQLSession了
return getSqlSession().getMapper(StudentMapper.class).selectStudent();
}
}
Spring配置
绑定接口
测试类
public class MyTest2 {
public static void main(String[] args) {
//获取Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取Bean对象
StudentMapper studentMapper = context.getBean("StudentMapper2", StudentMapper.class);
List students = studentMapper.selectStudent();
//输出内容
for (student s : students){
System.out.println(s);
}
}
}



