目录
4、编写StudentDao接口和对应的mapper映射文件
6、编写service接口和相应的实现类(目的是通过service调用dao接口中的方法)
7.4定义Mapper扫描配置器MapperScannerConfigurer
实现步骤
1、在数据库中创建一张表ssm
后面需要在jdbc.properties中配置数据库
jdbc=mysql://localhost:3306/?user=root
jdbc.username=root
jdbc.password=1234567
2、新建一个maven项目
2.1在pom.xml文件中加入maven依赖
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- spring事务依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- mybatis和spring集成依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- 阿里的连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 配置文件-->
<build>
<resources>
<resource>
<directory>src/main/java</directory> <!--所在的目录-->
<includes> <!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
3、编写实体类Student
package entity;
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
4、编写StudentDao接口和对应的mapper映射文件
package dao;
import entity.Student;
import java.util.List;
public interface StudentDao {
int insertStudent(Student student);
List<Student> selectAllStudent();
}
<?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">
<!-- 插入学生信息,确保通过Java代码进行参数校验来避免SQL注入 -->
<insert id="insertStudent" parameterType="entity.Student">
insert into student(name,age) values(#{name},#{age})
</insert>
<!-- 选择所有学生,考虑实际项目中可能的数据量,建议在Java代码中进行分页处理 -->
<select id="selectAllStudent" resultType="entity.Student">
select name, age from student <!-- 仅选择需要的字段以提高性能 -->
</select>
</mapper>
5、编写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>
<mappers>
<package name="dao"/>
</mappers>
</configuration>
6、编写service接口和相应的实现类(目的是通过service调用dao接口中的方法)
package Service;
import entity.Student;
import java.util.List;
public interface StudentService {
int addStudent(Student student);
List<Student> queryStudent();
}
package Service;
import dao.StudentDao;
import entity.Student;
import java.util.List;
public class StudentServiceImpl implements StudentService{
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
return studentDao.insertStudent(student);
}
@Override
public List<Student> queryStudent() {
return studentDao.selectAllStudent();
}
}
7、编写Spring配置文件
这里使用阿里的连接池、以及外部属性配置文件来读取数据库连接的相关信息。
先给出外部属性配置文件:jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=12345678
7.1加载外部属性配置文件
<!-- 加载外部属性配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
7.2声明数据源
<!-- 声明数据源DataSource -->
<bean id="myDataSource" 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}"/>
</bean>
7.3注册SqlSessionFactoryBean
<!-- 声明SqlSessionFactoryBean,在这个类的内部,创建SqlSessionFactory对象,之后就可以获取SqlSession对象 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 指定mybatis主配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
7.4定义Mapper扫描配置器MapperScannerConfigurer
<!-- 声明MapperScannerConfigurer -->
<!--
MapperScannerConfigurer作用:
循环basePackage所表示的包,把包中的每个接口都找到,调用SqlSession.getMapper(XXXDao.class)
把每个dao接口都创建出对应的dao代理对象,将dao代理对象放在容器中
对于StudentDao接口,其代理对象为 studentDao
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定SqlSessionFactory对象的名称 -->
<property name="sqlSessionFactoryBeanName" value="factory"/>
<!-- 指定基本包,dao接口所在的包名 -->
<property name="basePackage" value="com.bjpowernode.dao"/>
</bean>
7.5向Service中注入相关的接口名
<!-- 声明service -->
<bean id="studentService" class="com.bjpowernode.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
总结
将 MyBatis 与 Spring 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring 来管理。
所以,只需要将 SqlSessionFactory 的对象生成器 SqlSessionFactoryBean 注入在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合。
使用mybatis,需要创建mybatis框架中的某些对象,使用这些对象,就可以使用mybatis提供的功能了。
对于mybatis执行sql语句,需要用到的对象有:
1)SqlSessionFactory对象,只有创建了SqlSessionFactory对象,才能调用openSession()方法得到SqlSession对象。
2)dao接口的代理对象,例如StudentDao接口,需要的代理对象为:SqlSeesion.getMapper(StudentDao.class)。
3)数据源DataSource对象,使用一个更强大、功能更多的连接池对象代替mybatis自己的PooledDataSource。