Spring(四)spring整合mybatis框架
1、整合思想
使用Spring的ioc技术,把mybatis框架中的对象都交给spring创建和管理。Spring是容器,包含项目中的各种对象,例如:Service、Dao、工具类等对象。开发人员从spring中获取对象,就不用同时面对两个或多个框架,只需面对一个spring框架进行开发即可。
2、实现步骤
-
在pom文件中加入Spring依赖、Mybatis依赖、Spring和Mybayis整合依赖、druid连接池依赖等
-
编写数据库对应实体类、接口、mapper文件、service层文件等
-
Spring主配置文件
-
Mybatis配置文件
3、整合实现示例
着重看代码中注释。
- 数据库字段
- 实体类Student
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
public Student() {
}
public Student(Integer id, String name, String email, Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
- 接口StudentDao
public interface StudentDao {
int insertStudent(Student student); //插入数据
List<Student> selectStudent(); //查询表中数据
}
- Mapper文件
<?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="cn.krain.dao.StudentDao">
<!--在mybatis配置文件中已经通过typeAliases设置了别名, 因此resultType可直接使用student-->
<select id="selectStudent" resultType="student">
select * from student order by id desc
</select>
<insert id="insertStudent">
insert into student values (#{id}, #{name}, #{email}, #{age})
</insert>
</mapper>
- StudentService
public interface StudentService {
int addStudent(Student student);
List<Student> selectStudent();
}
- StudentServiceImpl
Service层,实现StudentService接口。
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = null;
public StudentServiceImpl(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
int num = studentDao.insertStudent(student);
return num;
}
@Override
public List<Student> selectStudent() {
List<Student> list = null;
list = studentDao.selectStudent();
return list;
}
}
- Spring.xml
使用Druid数据库连接池,DruidGitHub地址
<?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">
<!--使用druid数据库连接池-->
<!--声明数据源对象-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!--使用set注入为属性赋值-->
<property name="url" value="jdbc:mysql://localhost:3306/DB_Name"/><!--setURL()-->
<property name="username" value=""/>
<property name="password" value=""/>
<property name="maxActive" value=""/> <!--同时存在的数据库连接池最大个数-->
</bean>
<!--声明的是Mybatis中提供的SqlSessionFactoryBean类, 在这个类的内部创建SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--使用set注入,将数据库连接池赋给dataSource-->
<property name="dataSource" ref="myDataSource"/>
<!--告诉spring Mybatis主配置文件的位置, 使用configLocation的属性Resource类型读取Mybatis主配置文件-->
<property name="configLocation" value="classpath:Mybatis.xml"/>
</bean>
<!--创建dao对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定SqlSessionFactory对象的id-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定dao类的包名,spring会扫描该包下所有的dao接口, 使每个接口都执行getMapper(),获取dao对象 将dao对象放入spring容器中,对象名默认为接口名首字母小写-->
<property name="basePackage" value="cn.krain.dao"/>
</bean>
<!--创建service对象-->
<bean id="studentService" class="cn.krain.service.Impl.StudentServiceImpl">
<!--使用set注入,为studentServiceImpl中的studentDao属性赋值-->
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>
- Mybatis.xml
<?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>
<!--为实体类设置别名-->
<typeAliases>
<!--name:实体类所在的包名 批量处理,自动起别名,名称为类名首字母小写-->
<package name="cn.krain.domain"/>
</typeAliases>
<!--指定Sql映射文件(SQL Mapper)-->
<mappers>
<package name="cn.krain.dao"/>
</mappers>
</configuration>
- 测试类Test
public class MyTest {
@Test
public void Test01(){
//输出所有创建的对象
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
String names[] = ac.getBeanDefinitionNames();
for(String name:names){
System.out.println("创建的对象:"+name);
}
}
@Test
public void TestServiceSelect(){
//查询所有数据
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
StudentService service = (StudentService) ac.getBean("studentService");
List<Student> list = service.selectStudent();
for (Student st:list){
System.out.println(st);
}
}
@Test
public void TestServiceInsert(){
//插入数据
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
StudentService service = (StudentService) ac.getBean("studentService");
Student student = new Student();
student.setId(7);
student.setName("khan");
student.setEmail("khan@qq.com");
student.setAge(34);
int num = service.addStudent(student);
System.out.println(num);
}
}
- 执行结果
输出Spring容器所创建的所有对象:
查询数据库中信息:
插入一条数据:
插入成功后返回一个整形数字,表示更新数据的个数。