- Spring和MyBatis整合时,MyBatis的事务是自动提交的
项目架构
第一步 创建做为数据载体的domain
public class Student {
public String id;
public String name;
public int age;
public String sex;
public Student() {
}
public Student(String id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
}
第二步 创建与数据交互的dao对象
public interface StudentDao {
List<Student> selectAll();
}
创建对象的同时完成 dao.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.StudentDao">
<select id="selectAll" resultType="domain.Student">
select * from student;
</select>
</mapper>
第三步 创建service接口和对象
service接口
public interface StudentService {
void selectAll();
}
接口实现类
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao){
this.studentDao = studentDao;
}
public void selectAll(){
System.out.println("Executing selectAll-----------------------");
List<Student> students = studentDao.selectAll();
for(Student stu:students){
System.out.println(stu.name);
}
}
}
第四步 创建MyBatis主配置文件
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/><!--开启日志-->
</settings>
<typeAliases>
<package name="dao"/>
</typeAliases>
<mappers>
<package name="dao"/>
</mappers>
</configuration>
第五步 创建spring的配置文件:声明mybatis的对象交给spring创建
- 数据源Datasource
- SqlSessionFactory
- Dao对象
- 声明自定义的service
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--引入配置文件-->
<context:property-placeholder location="jdbc.properties"/>
<!--Druid连接池-->
<bean id="myDateSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--连接池配置信息-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
</bean>
<!--SqlSessionFactory对象-->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--赋值 连接池和MyBatis主配置文件-->
<property name="dataSource" ref="myDateSource"/>
<property name="configLocation" value="classpath:Mybatis.xml"/>
</bean>
<!--创建dao对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--sqlSessionFactory赋值-->
<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/>
<!--将dao包下所有dao对象交给Spring容器管理-->
<property name="basePackage" value="dao"/>
</bean>
<!--创建service对象-->
<bean id="myStudentService" class="service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>
第六步 创建测试类
public class myTest {
@Test
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService service = (StudentService) ac.getBean("myStudentService");
service.selectAll();
}
}
总结:项目流程就是创建好Spring 容器,Dao对象等,然后通过获取代表Spring容器的ApplicationContext对象,通过该对象获取service对象通过service.selectAll() 进行业务的进行,而在service中会获取与数据库交互的StudentDao,然后对数据库的数据进行查询,并返回。