关于spring:SpringMybatis

67次阅读

共计 2678 个字符,预计需要花费 7 分钟才能阅读完成。

  1. 导包

    <dependencies>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.12</version>
         </dependency>
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>5.1.47</version>
         </dependency>
         <dependency>
             <groupId>org.mybatis</groupId>
             <artifactId>mybatis</artifactId>
             <version>3.5.2</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
             <version>5.1.9.RELEASE</version>
         </dependency>
         <!--Spring 操作数据库的话,还须要一个 spring-jdbc-->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-jdbc</artifactId>
             <version>5.1.9.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>org.aspectj</groupId>
             <artifactId>aspectjweaver</artifactId>
             <version>1.8.13</version>
         </dependency>
         <dependency>
             <groupId>org.mybatis</groupId>
             <artifactId>mybatis-spring</artifactId>
             <version>2.0.2</version>
         </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <version>1.18.10</version>
         </dependency>
     </dependencies>
  2. 编写数据源配置

    <!--DataSource: 应用 Spring 的数据源替换 Mybatis 的配置 c3p0 dbcp druid
    咱们这里应用 Spring 提供的 JDBC:org.springframework.jdbc.datasource
    -->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
     <property name="url" value="jdbc:mysql://localhost:3306"/>
     <property name="username" value="root"/>
     <property name="password" value="196840xrk"/>
    </bean>
  3. sqlSessionFactory

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="datasource"/>
     <!-- 绑定 Mybatis 配置文件 -->
     <property name="configLocation" value="classpath:mybatis-config.xml"/>
     <property name="mapperLocations" value="classpath:com/sunfl/mapper/*.xml"/>
    </bean>
  4. sqlSessionTemplate

    <!--SqlSessionTemplate 就是咱们应用的 sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
     <!-- 只能应用结构器注入 sqlSessionFactory,因为它没有 set 办法 -->
     <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
  5. 须要给接口增加实现类

    public class UserMapperImpl implements UserMapper{
    
     // 咱们的所有操作,在原来都应用 sqlSession 来执行,当初都应用 SqlSessionTemple
     private SqlSessionTemplate sqlSession;
    
     public void setSqlSession(SqlSessionTemplate sqlSession) {this.sqlSession = sqlSession;}
    
     public List<User> selectUser() {UserMapper mapper = sqlSession.getMapper(UserMapper.class);
         return mapper.selectUser();}
    }
  6. 将本人写的实现类注入到 spring 中

    <bean id="userMapper" class="com.sunfl.mapper.UserMapperImpl">
     <property name="sqlSession" ref="sqlSession"/>
    </bean>
  7. 测试应用

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
    List<User> userList = userMapper.selectUser();
    for (User user : userList) {System.out.println(user);
    }

正文完
 0