mybatis和spring 两框架相结合-代码的具体实现

本文详细介绍了如何将mybatis和spring两个框架结合,通过项目结构、pojo、mapper、service包的配置,以及resources中的beans.xml、db.properties和mybatis-config.xml等内容,最后展示了test的实现结果。
摘要由CSDN通过智能技术生成

mybatis和spring 两框架相结合-代码的具体实现

1.总项目结构
-
2.pojo包
在这里插入图片描述
3.mapper包
在这里插入图片描述
mapper.xml
在这里插入图片描述
4.service包
在这里插入图片描述
实现类

package com.wj.service.impl;

import com.wj.domain.Student;
import com.wj.mapper.IStudentMapper;
import com.wj.service.IStudentService;

import java.util.List;

/**
 * 描述:
 *
 * @Author: 枭雄
 * @Create By: 2021/3/10 19:17
 */
public class StudentServiceImpl implements IStudentService {
    private IStudentMapper iStudentMapper;
    @Override
    public List<Student> findAll() {
        return iStudentMapper.findAll();
    }

    @Override
    public int save(Student student) {
        return iStudentMapper.save(student);
    }

    @Override
    public int update(Student student) {
        return iStudentMapper.update(student);
    }

    @Override
    public int delete(Integer id) {
        return iStudentMapper.delete(id);
    }

    @Override
    public Student findById(Integer id) {
        return iStudentMapper.findById(id);
    }


    public IStudentMapper getiStudentMapper() {
        return iStudentMapper;
    }

    public void setiStudentMapper(IStudentMapper iStudentMapper) {
        this.iStudentMapper = iStudentMapper;
    }
}

5.resources参数配置包

在这里插入图片描述
5.1beans.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: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"

>
<!--    配置数据源-->
<!--1.加载db.properties,通过文件加载器-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--2.配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
<!--    配置mybatis的SqlSessionFactoyr工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--    注入数据源-->
    <property name="dataSource" ref="dataSource"></property>
<!--    配置别名-->
    <property name="typeAliasesPackage" value="com.wj.domain"/>
</bean>


    <!--配置动态代理DAO-->
 <bean id="mapperScannerConfiger" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--     批量注册-->
     <property name="basePackage" value="com.wj.mapper"/>
 </bean>


    <!--    配置业务层-->
    <bean id="studentService" class="com.wj.service.impl.StudentServiceImpl">
        <property name="iStudentMapper" ref="IStudentMapper"/>
    </bean>
</beans>

5.2db.properties配置

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/stu?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
jdbc.username=root
jdbc.password=root

5.3mybatis-config。xml 不需要内容,单配置文件需要存在

6.pom.xml

<?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_mybatis_together</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
   <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.13.1</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>5.3.4</version>
       </dependency>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.47</version>
       </dependency>
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>3.4.6</version>
       </dependency>
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
           <version>1.2.5</version>
       </dependency>
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis-spring</artifactId>
           <version>1.3.2</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>4.3.23.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
           <version>1.2.17</version>
       </dependency>
   </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

7.test及其实现结果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值