Spring_2023_11_23_3 Spring整合mybatis----注解方式

发布时间 2023-11-23 17:52:42作者: Kbaor

Spring整合mybatis----注解方式

2023-11-23 17:18:29 星期四

a) 依赖的引入

 <!--spring基础依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--spring连接数据库-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--添加mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>
        <!--mybatis与spring整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.11.0</version>
        </dependency>

b) Spring核心配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">


    <!--扫描注解包-->
    <context:component-scan base-package="com.bboy.mapper"/>
    <context:component-scan base-package="com.bboy.service.impl"/>
    <!--数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/qinmanage"/>
        <property name="username" value="root"/>
        <property name="password" value="127003"/>
    </bean>
    <!--SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--实体类别名配置-->
        <property name="typeAliasesPackage" value="com.bboy.pojo"/>
    </bean>
    <!-- 配置Mapper,自动扫描Mapper接口,并为其注入SqlSessionFactory -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定mapper接口的位置-->
        <property name="basePackage" value="com.bboy.mapper"/>
        <!--指定sqlSessionFactory的名字-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

</beans>

c) Mapper

package com.bboy.mapper;

import com.bboy.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper

public interface StudentMapper {
    @Select("select * from t_student")
    public List<Student> listStudents();
    @Select("select * from t_student where id = #{id}")
    public Student getStudentById(@Param("id") int id);
}

d) Service

1.StudentService

package com.bboy.service;

import com.bboy.pojo.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentService {
    public List<Student> listStudents();
    public Student getStudentById(@Param("id") int id);
}

2.StudentServiceImpl

package com.bboy.service.impl;

import com.bboy.mapper.StudentMapper;
import com.bboy.pojo.Student;
import com.bboy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/23 0023 17:27:29
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

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

    @Override
    public List<Student> listStudents() {
        return studentMapper.listStudents();
    }

    @Override
    public Student getStudentById(int id) {
        return studentMapper.getStudentById(id);
    }
}

e) Demo

package com.bboy.demo;

import com.bboy.pojo.Student;
import com.bboy.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/23 0023 17:26:41
 */
public class Demo {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService studentService = (StudentService) context.getBean("studentService");
        List<Student> students = studentService.listStudents();
        System.out.println(students);
        System.out.println("====================================================");


        Student student = studentService.getStudentById(4);
        System.out.println(student);

    }

}

测试结果

image