| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4612 人关注过本帖
标题:spring mybatis配置
只看楼主 加入收藏
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
package test;

import com.pojo.Article;
import com.pojo.Category;
import com.pojo.User;

import dao.Interface.CategoryMapper;
import dao.Interface.IUserOperation;

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class userserviceimp0Test {
    private static ApplicationContext ctx;  
    static
    {  
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
    }        
      
    public static void main(String[] args)  
    {  
        IUserOperation mapper = (IUserOperation)ctx.getBean("userMapper");
        //测试id=1的用户查询,根据数据库中的情况,可以改成你自己的.
        System.out.println("得到用户id=1的用户信息");
        User user = mapper.selectUserByID(1);
        CategoryMapper categoryMapper=(CategoryMapper)ctx.getBean("CategoryMapper");
        Category category=categoryMapper.selectByPrimaryKey(1);
        //System.out.println(category.toString());
        System.out.println(user.getUserAddress());
        
        //得到文章列表测试
        System.out.println("得到用户id为1的所有文章列表");
      
    }  
 
   
}
2013-11-11 17:32
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
package test;


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

import server.Interface.IUserService;

public class userserviceimp123test {

    public void userServiceTest() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        IUserService userService = (IUserService) context.getBean("UserServiceImpl4");
        //IUserDao userService = (IUserDao) context.getBean("userMapper");
        System.out.println(userService.selectUserByID(2).getUserAddress());
    }
   
    public static void main(String[] args) {

        userserviceimp123test testUser = new userserviceimp123test();
        testUser.userServiceTest();
        

    }
}
2013-11-11 17:32
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.
    xmlns:xsi="http://www. xmlns:aop="http://www.
    xmlns:tx="http://www. xmlns:context="http://www.
    xsi:schemaLocation="  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.
    default-autowire="byName" default-lazy-init="false">

    <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->

    <bean id="dataSource" class="org.
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3309/mybatis?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="123" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource属性指定要用到的连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!--注册Mapper方式一、二、三:configLocation属性指定mybatis的核心配置文件 -->
        <property name="configLocation" value="config/Configuration.xml" />
        <!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名, 且在同一路径下,那么可以不配置该选项 -->
        <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件, mapperLocations和configLocation有一个即可,
            当需要为实体类指定别名时,可指定configLocation属性, 再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
        <!--<property name="mapperLocations" value="com/pojo/UseMapper.xml"/> -->
    </bean>

    <!-- 注册Mapper方式一: -->
    <!-- 不须要有映射文件configLocation即,需要@注解的IUserOperation接口,MapperFactoryBean将@注解的接口生成映射对象 -->
    <!-- 要有映射文件,需要没有@注解的IUserOperation接口,MapperFactoryBean将映射文件跟接口对象关联 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--mapperInterface属性指定映射器接口,用于生成映射器对象-->
        <property name="mapperInterface" value="dao.Interface.IUserOperation" />
    </bean>
    <bean id="CategoryMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--mapperInterface属性指定映射器接口,用于生成映射器对象 -->
        <property name="mapperInterface" value="dao.Interface.CategoryMapper" />
    </bean>
    <!-- 将获得的接口注入到ServiceImpl类 -->
    <bean id="UserServiceImpl1" class="server.serverImpl.userserviceimp1">
        <property name="userDao" ref="userMapper"></property>
    </bean>

   

    <!-- 注册Mapper方式二配置如下: -->
    <!-- 不须要有映射文件,需要@注解的IUserOperation接口 ,MapperScannerConfigurer将@注解的接口生成映射对象-->
    <!-- 须要有映射文件,需要没有@注解的IUserOperation接口 ,MapperScannerConfigurer将映射文件跟接口对象关联-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 使用自动扫描包的方式来注册各种Mapper ,将IUserOperation接口跟映射文件关联 -->
        <property name="basePackage" value="dao.Interface" />
    </bean>
    <bean id="UserServiceImpl2" class="server.serverImpl.userserviceimp1">
        <property name="userDao" ref="IUserOperation"></property>
    </bean>
   

    <!-- 注解mapper方式三:-->
    <!-- 必须要mapper映射文件 -->
    <!-- 如果mapper映射文件中采用class则自动扫描接口包中注解生成映射 -->
    <!-- 如果mapper映射文件中采用resource则自动扫描映射文件生成映射 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    <bean id="UserServiceImpl3" class="server.serverImpl.userserviceimp2">
        <!--ServiceImpl类采用注入 sqlSessionTemplate获取mapper映射文件中的方法 -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="UserServiceImpl4" class="server.serverImpl.userserviceimp3">
        <!-- ServiceImpl类采用继承SqlSessionDaoSupport类,获取mapper映射文件中的方法 -->
        <property name="sqlSession" ref="sqlSessionTemplate"></property>
    </bean>


</beans>
2013-11-12 09:47
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.
    xmlns:xsi="http://www. xmlns:aop="http://www.
    xmlns:tx="http://www. xmlns:context="http://www.
    xsi:schemaLocation="  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.  
            http://www. http://www.
    default-autowire="byName" default-lazy-init="false">

    <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->

    <bean id="dataSource" class="org.
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3309/mybatis?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="123" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource属性指定要用到的连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!--注册Mapper方式一、二、三:configLocation属性指定mybatis的核心配置文件 -->
        <property name="configLocation" value="config/Configuration.xml" />
        <!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名, 且在同一路径下,那么可以不配置该选项 -->
        <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件, mapperLocations和configLocation有一个即可,
            当需要为实体类指定别名时,可指定configLocation属性, 再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
        <!--<property name="mapperLocations" value="com/pojo/UseMapper.xml"/> -->
    </bean>

    <!-- 注册Mapper方式一: -->
    <!-- 不须要有映射文件configLocation即,需要@注解的IUserOperation接口,MapperFactoryBean将@注解的接口生成映射对象 -->
    <!-- 要有映射文件,需要没有@注解的IUserOperation接口,MapperFactoryBean将映射文件跟接口对象关联 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--mapperInterface属性指定映射器接口,用于生成映射器对象-->
        <property name="mapperInterface" value="dao.Interface.IUserOperation" />
    </bean>
    <bean id="CategoryMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--mapperInterface属性指定映射器接口,用于生成映射器对象 -->
        <property name="mapperInterface" value="dao.Interface.CategoryMapper" />
    </bean>
    <!-- 将获得的接口注入到ServiceImpl类 -->
    <bean id="UserServiceImpl1" class="server.serverImpl.userserviceimp1">
        <property name="userDao" ref="userMapper"></property>
    </bean>

   

    <!-- 注册Mapper方式二配置如下: -->
    <!-- 不须要有映射文件,需要@注解的IUserOperation接口 ,MapperScannerConfigurer将@注解的接口生成映射对象-->
    <!-- 须要有映射文件,需要没有@注解的IUserOperation接口 ,MapperScannerConfigurer将映射文件跟接口对象关联-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 使用自动扫描包的方式来注册各种Mapper ,将IUserOperation接口跟映射文件关联 -->
        <property name="basePackage" value="dao.Interface" />
    </bean>
    <bean id="UserServiceImpl2" class="server.serverImpl.userserviceimp1">
        <property name="userDao" ref="IUserOperation"></property>
    </bean>
   

    <!-- 注解mapper方式三:-->
    <!-- 必须要mapper映射文件 -->
    <!-- 如果mapper映射文件中采用class则自动扫描接口包中注解生成映射 -->
    <!-- 如果mapper映射文件中采用resource则自动扫描映射文件生成映射 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    <bean id="UserServiceImpl3" class="server.serverImpl.userserviceimp2">
        <!--ServiceImpl类采用注入 sqlSessionTemplate获取mapper映射文件中的方法 -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="UserServiceImpl4" class="server.serverImpl.userserviceimp3">
        <!-- ServiceImpl类采用继承SqlSessionDaoSupport类,获取mapper映射文件中的方法 -->
        <property name="sqlSession" ref="sqlSessionTemplate"></property>
    </bean>


</beans>
2013-11-12 09:48
快速回复:spring mybatis配置
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013204 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved