整合Mybatis 与 Spring3

上午参考了一些文章,整合了spring+mybatis,现在共享一下。把代码帖出来,如果有什么疑问,到mybatis官网下载相关文档来看一下就可以了。如果没有基础,请看我之前发的文章。

1.实体bean

    package org.hyn.bean;  
      
    public class User {  
        private int id;  
        private String name;  
        private int age;  
    ...//长长的set get 方法  

2.Mapper及配置文件


    package org.hyn.maper;  
      
    import org.hyn.bean.User;  
      
    public interface UserMapper {  
        public void insertUser(User user);  
      
        public User getUser(String name);  
    }  


<?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="org.hyn.maper.UserMapper">  
    <insert id="insertUser" parameterType="org.hyn.bean.User">  
        insert into user(name,age) values(#{name},#{age})   
        </insert>  
    <select id="getUser" resultType="org.hyn.bean.User"  
        parameterType="java.lang.String">  
        select * from user where name=#{name}   
       </select>  
  
    <!-- 当使用该Mybatis与Spring整合的时候,该文件必须和相应的Mapper接口文件同名,并在同一路径下 -->  
</mapper>  

3.spring配置文件


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="myDataSource" />  
      
        <!-- <property name="configLocation" value=""/> -->  
        <!--  
            该属性用来指定MyBatis的XML配置文件路径,跟Spring整合时,编写MyBatis映射文件的目的无非是配置一下typeAlias、setting之类的  
            元素。不用在其中指定数据源,或者事务处理方式。就算配置了也会被忽略。因为这些都是使用Spring中的配置  
            。当然如果你不打算添加typeAlias 之类的设置的话,你连MyBatis的配置文件都不用写,更不用配置这个属性了  
        -->  
      
        <!--<property name="mapperLocations" value="src/UserMapper.xml"/>-->  
        <!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名,且在同一路径下,那么可以不配置该选项-->  
    </bean>  
      
    <!--  
        注册Mapper方式一 <bean id="userMapper"  
        class="org.mybatis.spring.mapper.MapperFactoryBean"> <property  
        name="mapperInterface"  
        value="org.hyn.maper.UserMapper"/> <property  
        name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>  
    -->  
      
    <!-- 注册Mapper方式二:也可不指定特定mapper,而使用自动扫描包的方式来注册各种Mapper ,配置如下:-->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="org.hyn.maper" />  
    </bean>  

4.测试类


    @Test  
    public void testGetUser() {  
        ApplicationContext aContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");  
        UserMapper userMapper = aContext.getBean(UserMapper.class);  
        User user = userMapper.getUser("张三");  
        System.out.println(user.toString());  
    }  
      
    @Test  
    public void testAddUser(){  
        ApplicationContext aContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");  
        UserMapper userMapper = aContext.getBean(UserMapper.class);  
        User user = new User();  
        user.setName("张三");  
        user.setAge(18);  
        userMapper.insertUser(user);  
        System.out.println("添加成功");  
    }  


原文地址:https://www.cnblogs.com/dqsweet/p/4927754.html