Java学习路线-60:spring 整合 mybatis
发布日期:2021-07-01 06:11:38 浏览次数:2 分类:技术文章

本文共 5526 字,大约阅读时间需要 18 分钟。

整合示例

1、依赖

pom.xml

org.mybatis
mybatis
3.5.4
org.mybatis
mybatis-spring
2.0.4
org.springframework
spring-jdbc
5.2.6.RELEASE

2、配置

(1)beans.xml

(2)mybatis-config.xml

(3)StudentMapper.xml

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 + '\'' + ", 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
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
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
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); }}

配置修改

注解方式

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

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

转载地址:https://pengshiyu.blog.csdn.net/article/details/106895427 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java学习路线-61:MyBatis声明式事务
下一篇:Java 注解 Annotation自定义实战

发表评论

最新留言

不错!
[***.144.177.141]2024年04月22日 01时20分26秒