zoukankan      html  css  js  c++  java
  • 实战(五):与spring联合使用

    1. 首先对前面的工程结构做一点改变,在src_user源代码目录下建立文件夹config ,并将原来的 mybatis 配置文件 Configuration.xml 移动到这个文件夹中,

       并在config 文家夹中建立 spring 配置文件:applicationContext.xml ,这个配置文件里最主要的配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans default-lazy-init="false" default-autowire="byName"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans">
    <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->
    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
    <property value="com.mysql.jdbc.Driver" name="driverClassName" />
    <property value="jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf8"
    name="url" />
    <property value="root" name="username" />
    <property value="123456" name="password" />
    </bean>
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
    <!--dataSource属性指定要用到的连接池 -->
    <property name="dataSource" ref="dataSource" />
    <!--configLocation属性指定mybatis的核心配置文件 -->
    <property value="config/Configuration.xml" name="configLocation" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperFactoryBean" id="userMapper">
    <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 -->
    <property value="com.yihaomen.mybatis.inter.IUserOperation"
    name="mapperInterface" />
    </bean>
    </beans>

    2.然后写测试程序package com.yihaomen.test;import java.util.List;

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

    import com.yihaomen.mybatis.inter.IUserOperation;
    import com.yihaomen.mybatis.model.Article;
    import com.yihaomen.mybatis.model.User;


    public class MybatisSprintTest {
        private static ApplicationContext ctx;  
        static 
        {  
            ctx = new ClassPathXmlApplicationContext("config/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);
            System.out.println(user.getUserAddress()); 
            
            //得到文章列表测试
            System.out.println("得到用户id为1的所有文章列表");
            List<Article> articles = mapper.getUserArticles(1);
            
            for(Article article:articles){
                System.out.println(article.getContent()+"--"+article.getTitle());
            }
        }  
    }
     
    3.所需要的jar包

    commons-collections-3.1.jar
    commons-dbcp-1.2.jar
    commons-logging.jar
    commons-pool.jar
    mybatis-3.2.0-SNAPSHOT.jar
    mybatis-spring-1.1.1.jar
    mysql-connector-java-5.1.22-bin.jar
    org.springframework.aop-3.1.3.RELEASE.jar
    org.springframework.asm-3.1.3.RELEASE.jar
    org.springframework.aspects-3.1.3.RELEASE.jar
    org.springframework.beans-3.1.3.RELEASE.jar
    org.springframework.context-3.1.3.RELEASE.jar
    org.springframework.context.support-3.1.3.RELEASE.jar
    org.springframework.core-3.1.3.RELEASE.jar
    org.springframework.expression-3.1.3.RELEASE.jar
    org.springframework.instrument-3.1.3.RELEASE.jar
    org.springframework.instrument.tomcat-3.1.3.RELEASE.jar
    org.springframework.jdbc-3.1.3.RELEASE.jar
    org.springframework.jms-3.1.3.RELEASE.jar
    org.springframework.orm-3.1.3.RELEASE.jar
    org.springframework.oxm-3.1.3.RELEASE.jar
    org.springframework.spring-library-3.1.3.RELEASE.libd
    org.springframework.test-3.1.3.RELEASE.jar
    org.springframework.transaction-3.1.3.RELEASE.jar
    org.springframework.web-3.1.3.RELEASE.jar
    org.springframework.web.portlet-3.1.3.RELEASE.jar
    org.springframework.web.servlet-3.1.3.RELEASE.jar
    org.springframework.web.struts-3.1.3.RELEASE.jar

  • 相关阅读:
    JavaScript权威设计--Window对象之Iframe(简要学习笔记十四)
    JavaScript权威设计--JavaScript脚本化文档Document与CSS(简要学习笔记十五)
    JavaScript权威设计--CSS(简要学习笔记十六)
    事件处理介绍(简要学习笔记十七)
    事件源,事件对象(简要学习笔记十八)
    XMLHttpRequest(简要学习笔记十九)
    UITextField的placeholder的字体颜色和大小
    UITextField弹出键盘挡住输入框问题
    屏幕适配
    GPUImage的简单使用
  • 原文地址:https://www.cnblogs.com/JSWBK/p/5650955.html
Copyright © 2011-2022 走看看