Spring整合mybatis

Spring整合mybatis

一、創建一個java工程;

二、導入jar包(32個);

 1、 spring的jar包

2、Mybatis的jar包

3、Spring+mybatis的整合包。

4、Mysql的數據庫驅動jar包。

5、數據庫連接池的jar包。

三、編寫Spring的配置文件

1、數據庫連接及連接池

db.properties配置文件:

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8

jdbc.username=root

jdbc.password=123

 

在applicationContext.xml文件中:

<?xmlversion="1.0" encoding="UTF-8"?>

  <beans xmlns="http://www.springframework.org/schema/beans"

         xmlns:context="http://www.springframework.org/schema/context"

         xmlns:p="http://www.springframework.org/schema/p"

         xmlns:aop="http://www.springframework.org/schema/aop"

         xmlns:tx="http://www.springframework.org/schema/tx"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

         http://www.springframework.org/schema/context

         http://www.springframework.org/schema/context/spring-context-4.0.xsd

         http://www.springframework.org/schema/aop

         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

         http://www.springframework.org/schema/tx

         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

         http://www.springframework.org/schema/util

         http://www.springframework.org/schema/util/spring-util-4.0.xsd">

   

<!--加載數據庫連接的配置文件  -->

    <context:property-placeholderlocation="classpath:db.properties"/>

  <!--數據庫連接池  -->

    <bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource"

                   destroy-method="close">

                   <property   name="driverClassName"   value="${jdbc.driver}"/>

                   <property   name="url"          value="${jdbc.url}"/>

                   <property   name="username"    value="${jdbc.username}"/>

                   <property   name="password"    value="${jdbc.password}"/>

                   <property   name="maxActive"   value="10"/>

                   <property   name="maxIdle"     value="5"/>

         </bean>

</beans>

 

 

2、事務管理(暫時可以不配置)

 

3、sqlsessionFactory對象,配置到spring容器中

<!--mapper配置  -->

   <!-- 讓spring管理sqlsessionFactory,使用mybatis-spring整合包中的SqlSessionFactoryBean-->

   <bean  id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

  

    <!-- 數據庫連接池 -->

     <propertyname="dataSource" ref="dataSource"  />

     <!-- 加載mybatis的配置文件-->

     <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>

    

   </bean>

 

4、mapeer代理對象或者是dao實現類配置到spring容器中。

 

  <!-- 配置mapper掃描包 ······推薦使用 -->

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!--value:mapper接口所在的包的全路徑  -->

    <propertyname="basePackage" value="com.itcast.mybatis.mapper" />

  </bean>


 

 

四:mapper文件

   package com.itcast.mybatis.mapper;

 

import java.util.List;

import com.itcast.mybatis.po.User;

public interface UserMapper {

         public User  findUserById(int id )  throws Exception;

         public  List<User>  findUserByUsername(String  username) throws  Exception;

         public  void  insertUser(User user) throws Exception;

         public  void  updateUser(User  user) throws  Exception;

         public  void  deleteUser(int  id )  throws Exception;

}

 

 

 

 

五、測試

 

package com.itcast.mybatis.test;

importjava.util.List;

importorg.junit.Before;

importorg.junit.Test;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importcom.itcast.mybatis.mapper.UserMapper;

importcom.itcast.mybatis.po.User;

publicclass UserMapperTest {

private ApplicationContextapplicationContext;

         //初始化applicationContext

         @Before

         public void init()  throws Exception{

                   applicationContext=         new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");

         }

 

//使用包掃描實現模糊查詢

         @Test

         public voidtestFindUserByUsername() throws Exception {

      UserMapper userMapper =applicationContext.getBean(UserMapper.class);

      List<User> list =userMapper.findUserByUsername("%張%");


      for (User user : list) {

                     System.out.println(user);

          }

         }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章