zoukankan      html  css  js  c++  java
  • Mybatis,Spring整合

    Mybatis,Spring整合

    步骤:

    1. 导入相关jar包
      • junit
      • mybatis
      • mysql
      • spring
      • aspectjweaver
      • mybatis-spring

    回忆mybatis

    1. 编写实体类
    2. 编写核心配置文件
    3. 编写接口
    4. 编写Mapper.xml
    5. 测试

    mybatis-spring

    spring-dao.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--    spring提供的jdbc-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=ture&amp;characterEncoding=UTF-8"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </bean>
        <!--    sqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:Mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:cn/pinked/mapper/*.xml"/>
        </bean>
        <!--    sqlSessionTemplate,就是原来的sqlSession-->
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>
    </beans>
    

    applicationContext.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    	<!--    导入-->
        <import resource="spring-dao.xml"/>
    
        <bean id="userMapper" class="cn.pinked.mapper.UserMapperImpl">
            <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
        </bean>
    
    </beans>
    

    userMapperImpl.java

    public class UserMapperImpl implements UserMapper {
        private SqlSessionTemplate sqlSessionTemplate;
    
        public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
            this.sqlSessionTemplate = sqlSessionTemplate;
        }
    
        public List<User> selectUser() {
            UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
            return  mapper.selectUser();
        }
    }
    

    SqlSessionDaoSupport

    UserMapperImpl2.java

    public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
        public List<User> selectUser() {
            SqlSession sqlSession = getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }
    

    applicationContext.xml

    <bean id="userMapper2" class="cn.pinked.mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
  • 相关阅读:
    使用Silverlight 实现工作流流程定义
    中国国内航线信息的可视化
    笨办法学R编程(5)
    笨办法学R编程(4)
    笨办法学R编程(3)
    香山杯部分WP
    PWNABLE 3x17
    PWNABLE dubblesort
    PWNABLE applestore
    pyc文件修复出题经历
  • 原文地址:https://www.cnblogs.com/pinked/p/12207849.html
Copyright © 2011-2022 走看看