Spring 整合Mybatis的几种方式

Spring整合mybstis

步骤:

       1导入mybatis jar包与Spring jar包与Spring整合Mybatis jar包和数据库驱动包

 ant-1.9.6.jar

 ant-launcher-1.9.6.jar

 asm-5.2.jar

 cglib-3.2.5.jar

 commons-logging-1.1.1.jar

 commons-logging-1.2.jar

 javassist-3.22.0-GA.jar

 log4j-1.2.17.jar

 log4j-api-2.3.jar

 log4j-core-2.3.jar

 mybatis-3.4.6.jar

 mybatis-spring-1.3.2.jar

 ognl-3.1.16.jar

 slf4j-api-1.7.25.jar

 slf4j-log4j12-1.7.25.jar

 spring-aop-4.1.6.RELEASE.jar

 spring-aspects-4.1.6.RELEASE.jar

 spring-beans-4.1.6.RELEASE.jar

 spring-context-4.1.6.RELEASE.jar

 spring-context-support-4.1.6.RELEASE.jar

 spring-core-4.1.6.RELEASE.jar

 spring-expression-4.1.6.RELEASE.jar

 spring-jdbc-4.1.6.RELEASE.jar

 spring-orm-4.1.6.RELEASE.jar

 spring-tx-4.1.6.RELEASE.jar

 spring-web-4.1.6.RELEASE.jar

 spring-webmvc-4.1.6.RELEASE.jar

 sqljdbc4-2.0.jar

       2 XML配置

             

<!-- 配置数据源 -->

   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

      <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>

      <property name="url" value="jdbc:sqlserver://127.0.0.1;databaseName=test"></property>

      <property name="username" value="sa"></property>

      <property name="password" value="root"></property>

   </bean>

  

   <!-- 配置sqlsessionFactory -->

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

      <property name="dataSource" ref="dataSource"></property>

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

   </bean>

   <bean id="SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">

      <constructor-arg index="0" ref="sqlsessionFactory"></constructor-arg>

   </bean>

注:配置dataSource与qlsessionFactory时在class为org下的包的property的ID一定要与上面的相同,否则会报错,没有相应的set方法。(上面的红色字体)

 

3.创建业务类(实体类略)

      

private SqlSessionTemplate sqlsession;

  

   public User selectUser(String s_id) {

      User u=sqlsession.selectOne("com.user.UserMapper.selectUser", s_id);

      return u;

   }

  

   public void setSqlsession(SqlSessionTemplate sqlsession) {

      this.sqlsession = sqlsession;

   }

4.mybatisconfig

<mappers>

      <mapper resource="com/user/User.xml" />

   </mappers>

5.SQL语句映射

<select id="selectUser" resultType="com.user.User">

      select*from students where s_id=#{s_id}

   </select>

6.Test

public static void main(String[] args) {

      ApplicationContext ac=new ClassPathXmlApplicationContext("Beans.xml");

      UserDaoImpl udi=(UserDaoImpl)ac.getBean("userDao");

      System.out.println(udi.selectUser("001").getS_id());

      System.out.println(udi.selectUser("001").getS_name());

      System.out.println(udi.selectUser("001").getS_sex());

   }

运行结果:

 

整合方式2

与上面的配置对比去掉了SqlSessionTemplate, userDaoref变为sqlSessionFactory

<!-- 配置sqlsessionFactory -->

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

      <property name="dataSource" ref="dataSource"></property>

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

   </bean>

   <bean id="userDao" class="com.dao.impl.UserDaoImpl">

      <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

   </bean>

 

业务类userDao继承SqlSessionDaoSupport

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

  

   public User selectUser(String s_id) {

 

      return getSqlSession().selectOne("com.user.UserMapper.selectUser", s_id);

   }

 

整合方式3

使用注解

1.usermapper接口

public interface UserMapper {

   @Select("select*from students where s_id=#{s_id}")

   public User selectUser(String id);

}

实现类

public class UserService implements UserMapper {

   private UserMapper usermapper;

   @Override

   public User selectUser(String id) {     

      return usermapper.selectUser(id);

   }

   public void setUsermapper(UserMapper usermapper) {

      this.usermapper = usermapper;

   }

}

 

Xml配置

到Sqlsessionfactory与前面一样

<!-- 配置usermapper -->

   <bean id="usermapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

      <property name="mapperInterface" value="com.service.UserMapper" ></property>

      <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

   </bean>

   <bean id="userService" class="com.serviceImp.UserService">

      <property name="usermapper" ref="usermapper"></property>

   </bean>

 

猜你喜欢

转载自blog.csdn.net/u012777599/article/details/88700037
今日推荐