Java学习路线-60:spring 整合 mybatis

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Java学习路线-60:spring 整合 mybatis

整合示例

1、依赖

pom.xml


<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

2、配置

(1)beans.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";>

<!-- 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/data"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>

<!-- 配置 sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="mybatis-config.xml"/>
</bean>

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

<bean id="StudentDao" class="com.pengshiyu.mybatis.dao.impl.StudentDaoImpl">
<property name="sqlSession" ref="sqlSessionTemplate"/>
</bean>
</beans>

(2)mybatis-config.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>
<settings>
<!-- 打印sql日志 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

<typeAliases>
<package name="com.pengshiyu.mybatis.entity"/>
</typeAliases>

<mappers>
<mapper resource="StudentMapper.xml"/>
</mappers>

</configuration>

(3)StudentMapper.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.pengshiyu.mybatis.entity.StudentMapper">
<select id="selectAllStudent" resultType="Student">
select * from students
</select>
</mapper>

3、实体对象类

package com.pengshiyu.mybatis.entity;

public class Student {
private int id;
private String name;
private Teacher teacher;

public Teacher getTeacher() {
return teacher;
}

public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}

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;
}

@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\&#39;' +
", teacher=" + teacher +
'}';
}
}

4、dao

(1)定义接口

package com.pengshiyu.mybatis.dao;

import com.pengshiyu.mybatis.entity.Student;

import java.util.List;

public interface StudentDao {

public List<Student> selectAllStudent();

}

(2)实现接口

package com.pengshiyu.mybatis.dao.impl;

import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
private SqlSessionTemplate sqlSession;

@Override
public List<Student> selectAllStudent() {
return sqlSession.selectList(
"com.pengshiyu.mybatis.entity.StudentMapper.selectAllStudent");
}

public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
}

5、测试

package com.pengshiyu.mybatis.test;

import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.List;

public class Demo {
public static void main(String[] args) throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

StudentDao studentDao = (StudentDao)context.getBean("StudentDao");
List<Student> students = studentDao.selectAllStudent();

for(Student student: students){
System.out.println(student);
}
}
}

实现类继承 SqlSessionDaoSupport

实现类

package com.pengshiyu.mybatis.dao.impl;

import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import org.mybatis.spring.support.SqlSessionDaoSupport;

public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao {
@Override
public Student getById(int id) {
return getSqlSession().selectOne("com.pengshiyu.mybatis.entity.StudentMapper.getById", id);
}
}

配置修改

<?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";>

<!-- 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/data"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>

<!-- 配置 sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="mybatis-config.xml"/>
</bean>

<!-- 不需要单独配置 sqlSessionTemplate-->
<!-- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">-->
<!-- <constructor-arg index="0" ref="sqlSessionFactory"/>-->
<!-- </bean>-->

<bean id="StudentDao" class="com.pengshiyu.mybatis.dao.impl.StudentDaoImpl">
<!-- 将 sqlSession 注入,改为 sqlSessionFactory 注入-->
<!-- <property name="sqlSession" ref="sqlSessionTemplate"/>-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

</beans>

注解方式

1、DAOMapper

package com.pengshiyu.mybatis.dao;

import com.pengshiyu.mybatis.entity.Student;
import org.apache.ibatis.annotations.Select;

public interface StudentMapper {
@Select("select * from students where id = #{id}")
public Student getById(int id);

}

2、Service

(1)接口

package com.pengshiyu.mybatis.service;

import com.pengshiyu.mybatis.entity.Student;

public interface StudentService {
public Student getById(int id);
}

(2)实现

package com.pengshiyu.mybatis.service.impl;

import com.pengshiyu.mybatis.dao.StudentMapper;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.service.StudentService;

public class StudentServiceImpl implements StudentService {
private StudentMapper studentMapper;

public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}

public Student getById(int id) {
return studentMapper.getById(id);
}
}

3、配置

beans.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";>

<!-- 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/data"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>

<!-- 配置 sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="mybatis-config.xml"/>
</bean>

<bean id="studentMapper"
class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.pengshiyu.mybatis.dao.StudentMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

<bean id="studentService"
class="com.pengshiyu.mybatis.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"/>
</bean>

</beans>

4、测试

package com.pengshiyu.mybatis.test;

import com.pengshiyu.mybatis.dao.StudentMapper;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Demo {
public static void main(String[] args) throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

StudentService studentService = (StudentService) context.getBean("studentService");

Student student = studentService.getById(1);
System.out.println(student);
}
}

指定配置文件目录

beans.xml

<!-- 配置 sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>

<!-- <property name="configLocation" value="mybatis-config.xml"/>-->
<!-- 指定配置文件目录 -->
<property name="mapperLocations" value="classpath:*Mapper.xml"/>

</bean>
            </div>
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
10月前
|
SQL druid Java
java之路 —— SpringBoot整合MyBatis
java之路 —— SpringBoot整合MyBatis
106 0
|
Java 中间件 应用服务中间件
一篇文章带你Java Spring开发入门
一篇文章带你Java Spring开发入门
一篇文章带你Java Spring开发入门
|
Java 关系型数据库 MySQL
Java学习路线-60:spring 整合 mybatis
Java学习路线-60:spring 整合 mybatis
99 0
|
SQL Java 数据库连接
Java学习路线-61:MyBatis声明式事务
Java学习路线-61:MyBatis声明式事务
81 0
|
XML Java 数据库连接
Java学习路线-63:maven整合ssm-spring+struts+mybatis
Java学习路线-63:maven整合ssm-spring+struts+mybatis
84 0
|
Java Spring 容器
Java学习路线-56:Spring与Ioc
Java学习路线-56:Spring与Ioc
63 0
|
设计模式 缓存 开发框架
Java面试准备-Spring篇
Java面试准备-Spring篇
113 0
Java面试准备-Spring篇
|
Java 数据库连接 Spring
《Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题)》电子版地址
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题)
80 0
《Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题)》电子版地址
Java学习路线-65:整合 SSM-Spring+SpringMVC+MyBatis
Java学习路线-65:整合 SSM-Spring+SpringMVC+MyBatis
|
Java Spring 容器
Java学习路线-56:Spring与Ioc(2)
Java学习路线-56:Spring与Ioc
110 0