前几天写了一下MyBatis快速入门案例,今天写Spring整合MyBatis,那我们先看看他们的使用又什么不一样吧!
看完后,我们根据下面的步骤来做一下Spring 整合MyBtis的案例。
1.导入相关依赖项(此处采用maven来下载)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-mybaties-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
2.准备数据表
CREATE TABLE IF NOT EXISTS `student`(
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(40) NOT NULL,
`age` INT UNSIGNED,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO student (name, age) VALUES ("xiaoming", "18");
INSERT INTO student (name, age) VALUES ("xiaomei", "20");
3.业务类与接口准备
3.1 代码结构如下:
3.2 Student实体类
package com.yyj.domain;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.3 StudentDao接口
package com.yyj.dao;
import com.yyj.domain.Student;
import java.util.List;
public interface StudentDao {
void save(Student student);
void update(Student student);
void delete(Integer id);
Student findById(Integer id);
List<Student> findAll();
}
3.4 StudentService业务接口
package com.yyj.service;
import com.yyj.domain.Student;
import java.util.List;
public interface StudentService {
void save(Student student);
void update(Student student);
void delete(Integer id);
Student findById(Integer id);
List<Student> findAll();
}
3.5 StudentServiceImpl业务实现类
package com.yyj.service.impl;
import com.yyj.dao.StudentDao;
import com.yyj.domain.Student;
import com.yyj.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void save(Student student) {
studentDao.save(student);
}
@Override
public void update(Student student) {
studentDao.update(student);
}
@Override
public void delete(Integer id) {
studentDao.delete(id);
}
@Override
public Student findById(Integer id) {
return studentDao.findById(id);
}
@Override
public List<Student> findAll() {
return studentDao.findAll();
}
}
4.基础配置文件准备
4.1 StudentDao.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="com.yyj.dao.StudentDao">
<!--根据id查询-->
<select id="findById" resultType="com.yyj.domain.Student" parameterType="int">
select *
from student
where id = #{id}
</select>
<!--查询所有-->
<select id="findAll" resultType="com.yyj.domain.Student">
select *
from student
</select>
<!--保存-->
<insert id="save" parameterType="com.yyj.domain.Student">
insert into student(name, age)
values (#{name}, #{age})
</insert>
<!--删除-->
<delete id="delete" parameterType="int">
delete
from student
where id = #{id}
</delete>
<!--更新-->
<update id="update" parameterType="com.yyj.domain.Student">
update student
set name=#{name},
age=#{age}
where id = #{id}
</update>
</mapper>
4.2 jdbc.properties配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
4.3 applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--加载properties文件信息-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置第三方数据源信息-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置service作为spring的bean,并注入dao-->
<bean id="studentService" class="com.yyj.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
<!--配置service作为spring的bean,注入dao-->
<bean id="accountService" class="com.yyj.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
<!--spring整合mybatis后控制的创建连接用的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.yyj.domain"/>
</bean>
<!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yyj.dao"/>
</bean>
</beans>