欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring整合mybatis

程序员文章站 2022-05-25 12:52:22
项目的目录结构如下: 第一步:新建好一个maven项目,引入pom文件的依赖: 第二部:新建pojo类: 第三步:新建UserMapper接口。 package cn.itcast.mybatis.po; public interface UserMapper { public User findU ......

1.创建一个maven工程

2.在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">
    <parent>
  
        <artifactId>spring2</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>spring-mybatis</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
      <!--导入mybatis依赖包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
          <!--导入测试依赖包-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
          <!--导入mysql数据库相关操作包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
          <!--导入mybatis-spring整合包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
          <!--导入spring相关操作依赖包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
          <!--spring要操作数据库,得导入这个包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.19.RELEASE</version>
        </dependency>
          <!--导入面向切面编程AOP依赖包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
          <!--导入set,get注释依赖包-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
    </dependencies>
      <!--编写build文件,防止配置文件无法导出或生效的问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

3.创建数据库表中相应的实体类

package com.zhu.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
}


4.编写接口(需要操作数据库的方法)

package com.zhu.mapper;

import com.zhu.pojo.User;

import java.util.List;

public interface UserMapper {
//查询所有用户
    List<User> selectUser();
}


5.编写接口相对应的配置文件,编写方法对应的sql语句

<?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">
  <!--绑定UserMapper接口-->
<mapper namespace="com.zhu.mapper.UserMapper">
    <select id="selectUser" resultType="User">
        select * from mybatis.user
    </select>
</mapper>

6.配置mybatis核心文件

<?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>
    <typeAliases>
        <package name="com.zhu.pojo"/>
    </typeAliases>

</configuration>

7.配置spring核心文件

<?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-3.0.xsd">
  <!--使用spring的数据源代替mybatis核心文件中的配置-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306"></property>
        <property name="username" value="root"></property>
        <property name="password" value="zhuminghua"></property>
    </bean>
  <!--生成sqlSessionFactory工厂对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
<!--绑定mybatis核心配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:com/zhu/mapper/*.xml"></property>

    </bean>
<!--生成sqlSessionTemplate对象,相当于mybatis中的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <!--使用构造方法注入sqlSessionFactory,因为该类没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>
  <!--生成UserMapperImpl对象-->
    <bean id="UserMapper" class="com.zhu.mapper.UserMapperImpl">
    <!--使用set方法注入sqlSession-->
        <property name="sessionTemplate" ref="sqlSession"></property>
    </bean>

</beans>

8.创建UserMapper接口的实现类

package com.zhu.mapper;

import com.zhu.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sessionTemplate;

    public void setSessionTemplate(SqlSessionTemplate sessionTemplate) {
        this.sessionTemplate = sessionTemplate;
    }

    @Override
    public List<User> selectUser() {
        UserMapper userMapper = sessionTemplate.getMapper(UserMapper.class);
        return  userMapper.selectUser();
    }
}

9.将实现类注入到spring核心文件中,并且将属性赋值

<bean id="UserMapper" class="com.zhu.mapper.UserMapperImpl">
        <property name="sessionTemplate" ref="sqlSession"></property>
    </bean>

10.完成,进行测试

import com.zhu.mapper.UserMapper;
import com.zhu.mapper.UserMapperImpl;
import com.zhu.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


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

public class MyTest {
    @Test
    public void test() throws IOException {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper mapper = context.getBean("UserMapper",UserMapper.class);
        List<User> userList = mapper.selectUser();
        for (User user:userList){
            System.out.println(user);
        }
    }
}

本文地址:https://blog.csdn.net/weixin_45723003/article/details/112002477