1、需要导入包:
- JDBC -mysql数据库
- connection
- Mybatis
- Spring相关的
- junit
- aop织入
- Mybatis-Spring 【new】
2、编写配置文件
3、测试
4.回忆Mybatis
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的文件类型 账号密码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- value中的值也可以用property值来代替:比如${driver}-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatistest?useUnicode=true&characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 如果想用xml和注解一起生效-->
<mappers>
<!-- 绑定接口-->
<mapper class="com.jia.mapper.StudentMapper"/>
</mappers>
</configuration>
4.3编写接口
//定一个接口
public interface StudentMapper {
public List<student> selectStudent();
}
4.4编写Mapper.xml文件 --也就是编写SQL语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--编写mapper文件-->
<!--namespace 配置接口位置-->
<mapper namespace="com.jia.mapper.StudentMapper">
<!-- id对应接口中的方法-->
<select id="selectStudent" resultType="com.jia.bean.student">
select * from student;
</select>
</mapper>
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<student> students = mapper.selectStudent();
for (student s: students){
System.out.println(s.toString());
}
}
}
特别注意的是【重要】
使用pom.xml包文件需要导入mybatis的过滤,不然系统找到mapper文件
<!-- mybatis静态过滤-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
5、Mybatis-Spring
Mybatis-Spring会帮助你将MyBatis代码无缝地整合到Spring中。它将允许MyBatis参与到Spring的事务管理中,创建映射器mapper和SqlSession并注入到bean中,以及将Mybatis的异常转换为Spring的DataAccessException。最终,可以做到应用代码不依赖MyBatis,Spring或Mybatis-Spring
使用的步骤如下:
5.1编写数据源
<!-- 有了这个Spring就几乎不需要使用Mybatis文件了-->
<!--配置DataSource文件 DataSource使用 Spring的数据源替换Mybatis数据源 C3p0 dbcp druid
我们这里使用的是Spring提供的JDBC-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatistest?useUnicode=true&characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
5.2SqlSessionFactory
<!--配置SQLSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 创建SqlSessionFactory工厂需要导入Spring配置的数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 绑定Mybatis配置文件,也就是Mapper文件-->
<property name="configLocation" value="classpath:Mybatis-config.xml"/>
<!-- 配置mapper文件-->
<property name="mapperLocations" value="classpath:com/jia/mapper/*.xml"/>
</bean>
5.3SqlSessionTemplate
SqlSessionTemplate是MyBatis-Spring的核心。作为SQLSession的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的SQLSession。SQLSessionTemplate是线程安全的,可以被多个DAO或映射器所共享使用。
<!-- 创建SqlSession -->
<!-- SqlSessionTemplate:就是我们使用的sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 源码只能使用构造器来注入值,没有set方法。
三种注入方式,使用index索引,name名字,type使用类型(不建议使用)-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
5.4需要给接口加实现类【新加】
public class StudentMapperImpl implements StudentMapper{
//在原来我们的所有的操作,都使用sqlSession来执行,现在我们使用SqlSessionTemplate
private SqlSessionTemplate sqlSession;
//定义一个Set注入
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<student> selectStudent() {
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<student> students = mapper.selectStudent();
return students;
}
}
5.5将自己写的实现类注入到spring中,前面配置文件都在Spring-dao.xml配置文件当中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<import resource="Spring-dao.xml"/>
<!-- 导入StudentMapper bean-->
<bean id="StudentMapper" class="com.jia.mapper.StudentMapperImpl">
<!-- 引入SQLSession-->
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
5.6测试
public static void main(String[] args) {
//获取Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取Bean对象
StudentMapper studentMapper = context.getBean("StudentMapper", StudentMapper.class);
List<student> students = studentMapper.selectStudent();
//输出内容
for (student s : students){
System.out.println(s);
}
}
注意使用Spring整合Mybatis之后,就不需要使用Mybatis来创建SessionFactroy了。
6.使用SqlSessionDaoSupport整合Mybatis
SqlSessionDaoSupport是一个抽象的支持类,用来为你提供SqlSession。调用getSqlSession()方法你会得到一个SqlSessionTemplate,之后可以用于执行SQL方法。实现类继承SqlSsessionDaoSupport类。
6.1实现类继承SqlSessionDaoSupoort
public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper{
public List<student> selectStudent() {
//获取SqlSession,不在用SqlSessionTemplate来创建SQLSession了
return getSqlSession().getMapper(StudentMapper.class).selectStudent();
}
}
Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<!-- 有了这个Spring就几乎不需要使用Mybatis文件了-->
<!--配置DataSource文件 DataSource使用 Spring的数据源替换Mybatis数据源 C3p0 dbcp druid
我们这里使用的是Spring提供的JDBC-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatistest?useUnicode=true&characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置SQLSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 创建SqlSessionFactory工厂需要导入Spring配置的数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 绑定Mybatis配置文件,也就是Mapper文件-->
<property name="configLocation" value="classpath:Mybatis-config.xml"/>
<!-- 配置mapper文件-->
<property name="mapperLocations" value="classpath:com/jia/mapper/*.xml"/>
</bean>
</beans>
绑定接口
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<!--<import resource="Spring-dao.xml"/>-->
<!-- 导入SqlSessionDaoSupport的Spring配置,继承了SqlSessionDaoSupport这个类就不需要配置SQL SessionTemplate来创建SqlSession-->
<import resource="Spring-dao-SqlSessionDaoSupport.xml"/>
<!-- 导入StudentMapper bean-->
<!--<bean id="StudentMapper" class="com.jia.mapper.StudentMapperImpl">-->
<!-- <!– 引入SQLSession–>-->
<!-- <property name="sqlSession" ref="sqlSession"/>-->
<!--</bean>-->
<bean id="StudentMapper2" class="com.jia.mapper.StudentMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
测试类
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<student> students = studentMapper.selectStudent();
//输出内容
for (student s : students){
System.out.println(s);
}
}
}