美文网首页
2018-07-12 Spring和Mybatis的整合 入门案

2018-07-12 Spring和Mybatis的整合 入门案

作者: 培根好吃 | 来源:发表于2018-07-12 22:10 被阅读0次

MapperFactoryBean中已经写了这两行代码


image.png

就相当于


image.png
image.png

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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:jdbc.properties"/>
    
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <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>
    
    <!-- Mybatis的工厂 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 核心配置文件的位置 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    </bean>
    
    <!-- Dao原始Dao -->
    <bean id="userDao" class="com.ryan.sm.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    </bean>
    <!-- Mapper动态代理开发 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
        <property name="mapperInterface" value="com.ryan.sm.mapper.UserMapper"/>
    </bean>
    
    <!-- Mapper动态代理开发   扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 基本包 -->
        <property name="basePackage" value="com.ryan.sm.mapper.UserMapper"/>
    </bean>
</beans>


<?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>
       <!-- <typeAlias type="com.ryan.mybatis.pojo.User"  alias="User"/> -->
       <package name="com.ryan.sm"/>
    </typeAliases>
    
    
    <!-- Mapper的位置  Mapper.xml 写Sql语句的文件的位置 -->
    <mappers>
      <!-- <mapper resource="sqlmap/User.xml"/> -->
      
      <!-- 如果要换成class,那么1.java文件和User.xml文件在同一个包下 2. java文件名和xml文件名相同 -->
      <!-- <mapper class="com.ryan.mybatis.UserDao"/> -->
      <!-- 如果有很多表,那么xml就会有很多。所以项目一般用packege -->
      <package name="com.ryan.sm"/>

    </mappers>
</configuration>



<?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">
<mapper  namespace="com.ryan.sm.mapper.UserMapper">

   <!-- 通过ID查询一个用户 -->
    <select id="findUserById" parameterType="Integer" resultType="com.ryan.sm.mybatis.pojo.User">
        select * from user where id = #{v}
    </select>
</mapper>

代码

package com.ryan.sm.mapper;

import com.ryan.sm.mybatis.pojo.User;

public interface UserMapper {

    
    public User findUserById(Integer id);
}


package com.ryan.sm.junit;

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

import com.ryan.sm.mapper.UserMapper;
import com.ryan.sm.mybatis.pojo.User;

public class TestMapper {
@Test
public void demo1() {
    ApplicationContext cpApp = new ClassPathXmlApplicationContext("applicationContext.xml");
    /* 
     * 获得的MapperBean的两种方法
     * UserMapper mapper = cpApp.getBean("MapperBean") ;  */
    UserMapper mapper = cpApp.getBean(UserMapper.class);
    User user = mapper.findUserById(10);
    System.out.println(user);
    
}
}


jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=123456

相关文章

网友评论

      本文标题:2018-07-12 Spring和Mybatis的整合 入门案

      本文链接:https://www.haomeiwen.com/subject/kkcdpftx.html