• mybatis笔记<二> 整合spring


    mybatis与spring整合需要添加几个jar包,mybatis-spring, spring-context, spring-jdbc

    1. spring ioc只要一个jar包就ok

    2. 我用了c3p3数据库连接池

    跟笔记<一>比,需要修改的地方

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.gxf.mybatis</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.6</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.11</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
            <!-- c3p0 -->
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
            <!-- spring-jdbc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>3.0.4.RELEASE</version>
            </dependency>
    
    
        </dependencies>
    
    </project>

    添加beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mapperLocations" value="classpath*:mapper/*.xml"/>
        </bean>
        <bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <property name="mapperInterface" value="com.gxf.mybatis.mapper.PersonMapper" />
            <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        </bean>
    
    
    
    
        <bean id="dataSource"
              class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="${jdbc.driverClassName}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
        <context:property-placeholder location="jdbc.properties"/>
    
    </beans>

    jdbc.properties

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost/db_test
    jdbc.username=root
    jdbc.password=luckygxf

    测试类main方法

    package com.gxf.mybatis.util;
    
    import com.gxf.mybatis.mapper.PersonMapper;
    import com.gxf.mybatis.mapper.PersonMapper1;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class PersonTableOps {
        private static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryHelper.getSqlSessionFactory();
        private static PersonMapper personMapper;
    
        public static void main(String[] args) {
            testSpringMybatis();
        }
    
    
        private static void testSpringMybatis(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            personMapper = (PersonMapper) applicationContext.getBean("personMapper");
            Person person = personMapper.selectPerson(1);
            System.out.println(person);
        }
    
        /**
         * test mybatis select method
         * */
        private static void testSelectOnes(){
            //        selectOne1();
    //        selectOne2();
            selectOne3();
        }
    
        /**
         * 第一种方式
         * */
        public static void selectOne1(){
            SqlSession session = sqlSessionFactory.openSession();
            try {
                Person person = session.selectOne(
                        "com.gxf.mybatis.mapper.PersonMapper.selectPerson", 1);
                System.out.println(person);
            } finally {
                session.close();
            }
        }
    
        /**
         * 第二种方式
         * */
        public static void selectOne2(){
            SqlSession session = sqlSessionFactory.openSession();
            try {
                PersonMapper mapper = session.getMapper(PersonMapper.class);
                Person person = mapper.selectPerson(1);
                System.out.println(person);
            } finally {
                session.close();
            }
    
        }
    
        /**
         * 第三种方式
         * */
        public static void selectOne3(){
            SqlSession session = sqlSessionFactory.openSession();
            try {
                sqlSessionFactory.getConfiguration().addMapper(PersonMapper1.class);
                PersonMapper1 mapper = session.getMapper(PersonMapper1.class);
                Person person = mapper.selectPerson(1);
                System.out.println(person);
            } finally {
                session.close();
            }
        }
    
        public static SqlSessionFactory getSqlSessionFactory() {
            return sqlSessionFactory;
        }
    
        public static void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
            PersonTableOps.sqlSessionFactory = sqlSessionFactory;
        }
    
        public PersonMapper getPersonMapper() {
            return personMapper;
        }
    
        public void setPersonMapper(PersonMapper personMapper) {
            this.personMapper = personMapper;
        }
    }

    以前没有想到的

    1. spring ioc 一个jar包搞定

    2. 使用配置文件,占位符

    接下来要看下mybatis源码,还有spring整合到一起的源码,出个笔记

  • 相关阅读:
    解决命令行乱码问题(中文乱码)
    Outlook 修改归档文件顶层目录名
    (职员)2015-11-11 星期三 日志
    职员)2015-11-10 星期二 日志
    职员)2015-11-09 星期一 日志
    (职员)2015-11-05 星期四 日志
    (职员)2015-11-04 星期三 日志
    (职员)2015-11-03 星期二 日志
    (职员)2015-11-02 星期一 日志
    (职员)2015-10-31 星期六 周志
  • 原文地址:https://www.cnblogs.com/luckygxf/p/9509269.html
Copyright © 2020-2023  润新知