spring整合mybatis的步骤

项目整体结构
spring整合mybatis的步骤_第1张图片

1、在数据库中新建user表。
spring整合mybatis的步骤_第2张图片
2、新建一个maven工程或者maven模块(spring-mybatis-01)。
spring整合mybatis的步骤_第3张图片
3、在pom.xml导入相关依赖。

<!--统一设置spring相关依赖版本号-->
    <properties>
        <spring.version>5.3.18</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring 对JDBC 数据访问-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--mybatis核心依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>

        <!--spring和mybatis整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>

        <!--Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>

4、IDEA连接数据库
spring整合mybatis的步骤_第4张图片

5、实体类User.java(pojo层)

package com.wen.pojo;

import java.io.Serializable;

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

/**
 * 
 * @TableName user
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {

    private Integer id;
    private String name;
    private String pwd;


}

6、创建接口UserMapper(mapper层)

package com.wen.mapper;

import com.wen.pojo.User;

public interface UserMapper{
     //根据id查询
     User selectByPrimaryKey(Integer id);

}

7、创建UserMapper.xml文件`


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wen.mapper.UserMapper">

    <select id="selectByPrimaryKey" resultType="com.wen.pojo.User">
        select * from mybatis.user where id=#{id}
    select>

mapper>

8、新建Userservice(service层)

package com.wen.service;

import com.wen.pojo.User;

/**
*
*/
public interface UserService {
    User selectByPrimaryKey(Integer id);
}

9、UserServiceImpl(service层)

import com.wen.mapper.UserMapper;
import com.wen.pojo.User;
import com.wen.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
*
*/
@Service
public class UserServiceImpl  implements UserService{
    //声明一个userMapper,使mapper和service关联
    @Resource
    private UserMapper userMapper;
    @Override
    public User selectByPrimaryKey(Integer id) {
        //service调用mapper层方法
        return userMapper.selectByPrimaryKey(id);
    }
}

10、jdbc配置文件db.properties

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF8
jdbc.username = root
jdbc.password = 123456

11、mybatis的核心配置文件


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    settings>
    
    <typeAliases>
        <package name="com.wen.pojo"/>
    typeAliases>
    
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    mappers>
    
configuration>

12、spring核心配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="com.wen.service"/>
    
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}">property>
        <property name="url" value="${jdbc.url}">property>
        <property name="username" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    bean>
    
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.wen.mapper"/>
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    bean>

beans>

13、编写测试类

import com.wen.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean(UserService.class);
        System.out.println( userService.selectByPrimaryKey(1));
    }
}

14、测试结果
spring整合mybatis的步骤_第5张图片

你可能感兴趣的:(spring,java,spring)