Spring整合myBatis(Mapper代理方式)

一 概述

  • 1 spring管理mapper代理对象< bean>,代理对象从spring容器中获得
  • 计算机运行代码,先写代码,再去写配置文件

二 实例

Spring整合myBatis(Mapper代理方式)_第1张图片

1.导jar包

2.配置文件

(1)mybatis配置

  • SqlMapConfig.xml


<configuration>

configuration>
  • 映射文件配置(sql语句)


<mapper namespace="com.blueSky.dao.UserRepository">

    <select id="findAllUser" resultType="com.blueSky.pojo.User">
        SELECT * 
        from user 
    select>


mapper>

(2)spring配置

  • applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    <context:property-placeholder location="classpath:db.properties" />
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml">property>
    bean>

    
    
    
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
        <property name="basePackage" value="com.blueSky.dao"/>
    bean>
beans>

3.写代码

  • (1).先写pojo类(参数类型,返回类型)
package com.blueSky.pojo;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class User implements Serializable {
    private Integer id;
    private String username;// 用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址
    private List orders;
    public List getOrders() {
        return orders;
    }
    public void setOrders(List orders) {
        this.orders = orders;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", sex=" + sex
                + ", birthday=" + birthday + ", address=" + address + "]";
    }

}
  • (2).编写mapper接口
package com.blueSky.dao;

import java.util.List;

import com.blueSky.pojo.User;

public interface UserRepository {
    public List findAllUser();
}
  • (3).配置映射文件(四个对应),映射文件与接口一个包,文件名必须一致


<mapper namespace="com.blueSky.dao.UserRepository">

    <select id="findAllUser" resultType="com.blueSky.pojo.User">
        SELECT * 
        from user 
    select>


mapper>

4.测试

package test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.blueSky.dao.UserRepository;
import com.blueSky.pojo.User;

public class smTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        UserRepository userRepository = applicationContext.getBean(UserRepository.class); //根据spring容器中的对象类型进行匹配
        List allUser = userRepository.findAllUser();
        for (User user : allUser) {
            System.out.println(user.toString());
        }
    }

}

你可能感兴趣的:(mybatis,mybatis整合spring,java,后台框架,spring,mybatis)