Spring 框架和 Mybatis 框架的整合案例

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"
       xmlns:p="http://www.springframework.org/schema/p"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--加载配置文件-->
    <context:property-placeholder location="classpath:*.properties"/>

    <!--配置druid连接池信息-->
    <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>

    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

    <!--spring整合mybatis后控制的创建连接用的对象-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.itheima.domain"/>
    </bean>

    <!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    </bean>
</beans>

jdbc.properties 配置文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db2
jdbc.username=root
jdbc.password=root

Mybatis 的映射配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!--Mybatis的DTD约束-->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.itheima.dao.UserDao">

    <insert id="save" parameterType="user">
        INSERT INTO user VALUES (#{id},#{user_id},#{username},#{age},#{sex},#{phone});
    </insert>

    <update id="update" parameterType="user">
        UPDATE user SET username=#{username}, age=#{age}, sex=#{sex}, phone=#{phone} WHERE user_id=#{user_id}
    </update>

    <delete id="delete" parameterType="String">
        DELETE FROM user WHERE user_id=#{user_id}
    </delete>

    <select id="findAll" resultType="user">
        SELECT * FROM user
    </select>

    <select id="findById" resultType="user" parameterType="String">
        SELECT * FROM user WHERE user_id = #{user_id}
    </select>
</mapper>

编写数据访问层和业务逻辑层代码
(这里省略了实体类想编写)

package com.itheima.dao;

import com.itheima.domain.User;

import java.util.List;

public interface UserDao {

    int save(User user);

    int update(User user);

    int delete(String user_id);

    List<User> findAll();

    User findById(String user_id);
}
package com.itheima.service;

import com.itheima.domain.User;

import java.util.List;

public interface UserService {

    int save(User user);

    int update(User user);

    int delete(String user_id);

    List<User> findAll();

    User findById(String user_id);

}
package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public int save(User user) {
        int result = userDao.save(user);
        return result;
    }

    @Override
    public int update(User user) {
        int result = userDao.update(user);
        return result;
    }

    @Override
    public int delete(String user_id) {
        int result = userDao.delete(user_id);
        return result;
    }

    @Override
    public List<User> findAll() {
        List<User> users = userDao.findAll();
        return users;
    }

    @Override
    public User findById(String user_id) {
        User user = userDao.findById(user_id);
        return user;
    }
}

编写测试代码进行测试

package com.itheima.test;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean(UserServiceImpl.class);
        User user = userService.findById("1001");
        System.out.println(user);
    }
}

可以看到数据库中的数据
在这里插入图片描述

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页